1. 程式人生 > 實用技巧 >D. Omkar and Circle

D. Omkar and Circle

Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!

You are given \(n\) nonnegative integers \(a1,a2,…,an\) arranged in a circle, where nn must be odd (ie. \(n−1\) is divisible by \(2\)). Formally, for all \(i\) such that \(2≤i≤n\)

, the elements \(ai−1\) and \(ai\) are considered to be adjacent, and \(an\) and \(a1\) are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.

Help Danny find the maximum possible circular value after some sequences of operations.

Input

The first line contains one odd integer \(n\) (\(1≤n<2⋅10^5\), \(n\) is odd) — the initial size of the circle.

The second line contains nn integers \(a1,a2,…,an\) (\(0≤ai≤10^9\)) — the initial numbers in the circle.

Output

Output the maximum possible circular value after applying some sequence of operations to the given circle.

Examples

input

3
7 10 2

output

17

input

1
4

output

4

Note

For the first test case, here's how a circular value of 1717 is obtained:

Pick the number at index 33. The sum of adjacent elements equals 1717. Delete 77 and 1010 from the circle and replace 22 with 1717.

Note that the answer may not fit in a 3232-bit integer.

題解

第一感覺,這個題和合並果子的那道題很像,可以嘗試使用優先佇列來寫,合併的時候,優先合併當前位置元素小的,結果 \(wa\) 了。

優化一下需要求解的東西,

\(n\) 個物品,需要合併 \(n/2\)

每次選擇合併的位置不能是相鄰的,

\(namo\) 就可以選擇以求字首和的方式來求解不相鄰的 \(n/2\) 個的最小值,拿總之減一下,或者是求一下減之後最大值。

程式碼

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 4e5+55;
ll a[maxn];
ll c[maxn];
ll d[maxn];
int main(){
    int n;
    ll sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    for(int i=0;i<n;++i){
        if(i>=2)c[i]=c[i-2]+a[i];
        else c[i]=a[i];
    }
    for(int i=n-1;i>=0;--i){
        if(i<=n-3)d[i]=d[i+2]+a[i];
        else d[i]=a[i];
    }
    ll maxx=c[0];
    for(int i=0;i<n;++i){
        maxx=max(maxx,c[i]+d[i+1]);
    }
    printf("%lld\n",maxx);
    return 0;
}