Problem:
Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

2 \leq N \leq 2\times 10^5

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define asc(i,a,n) for (I i=a;i<n;i++)
#define dsc(i,a,n) for (I i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define P auto
typedef vector<long long int> VI;
typedef long long int I;
typedef pair<long long int,long long int> PII;
typedef double D;
typedef set<long long int> OSI;
typedef unordered_set<long long int> USI;
struct node{
  I pre;
  I suf;
  I sum;
  I mxsum;
  I l;
  I r;
};
I rootmax(node tree[],I n,I a[],I m){
  dsc(i,0,n/2){
    I k=(tree[2*i+1].l+tree[2*i+2].r)/2;
    if(k>=m){
      k=0;
    }else{
      k=a[k];
    }
    tree[i].l=tree[2*i+1].l;
    tree[i].r=tree[2*i+2].r;
    tree[i].sum=tree[2*i +1].sum+tree[2*i +2].sum +k;
    tree[i].pre=max(tree[2*i+1].pre,tree[2*i+1].sum+tree[2*i+2].pre+k);
    tree[i].suf=max(tree[2*i+2].suf,tree[2*i+2].sum+tree[2*i+1].suf+k);
    tree[i].mxsum=max(tree[2*i+1].suf+tree[2*i+2].pre+k,max(tree[2*i+1].mxsum,tree[2*i+2].mxsum));
  }
  return tree[0].mxsum;
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    I n;
    cin>>n;
    I a[n];
    I sum=0;
    asc(i,0,n){
      cin>>a[i];
      sum+=a[i];
    }
    if(!(n&1)){
      I ev=0;
      asc(i,0,n){
        ev+=a[i];
        i++;
      }
      I b[n];
      I c[n];
      b[0]=a[0];
      b[1]=a[1];
      asc(i,2,n){
        b[i]=b[i-2]+a[i];
      }
      c[n-1]=a[n-1];
      c[n-2]=a[n-2];
      dsc(i,0,n-2){
        c[i]=c[i+2]+a[i];
      }
      I mx=INT_MIN;
      asc(i,1,n-2){
        mx=max(b[i-1]+c[i+2], mx);
      }
      mx=max(ev,mx);
      mx=max(sum-ev,mx);
      cout<<INT_MAX;
    }else{
      I od=0;
      asc(i,0,n){
        od+=a[i];
        i++;
      }
      I d[n];
      asc(i,0,n){
        if(i%2){
          d[i]=a[i];
        }else{
          d[i]=-a[i];
        }
      }
      I m=n/2;
      m++;
      I sz=pow(2,ceil(log(m)/log(2)));
      node tree[2*sz -1];
      asc(i,0,m){
        tree[sz-1+i].sum=tree[sz-1+i].pre=tree[sz-1+i].suf=tree[sz-1+i].mxsum=d[2*i];
        tree[sz-1+i].l=tree[sz-1+i].r=2*i;
      }
      asc(i,m,sz){
        tree[sz-1+i].sum=tree[sz-1+i].pre=tree[sz-1+i].suf=tree[sz-1+i].mxsum=INT_MIN;
        tree[sz-1+i].l=tree[sz-1+i].r=2*i;
      }
      I difsum=rootmax(tree,2*sz-1,d,n);
      cout<<od+difsum;
    }
    return 0;
}