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++ (Clang 10.0.0) (judge verdict: Wrong Answer)

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

ll n;
ll a[200100];

ll sum[2][200100];

ll calc(int l, int r, int k) {
    //cerr << l << " " << r << " " << k << endl;
    if (k == 0 || l >= r || k+k-1 > r-l) return 0;
    if (r - l == 1) {
        return a[l];
    }
    if (r - l == 2) {
        return (k == 1 ? max(a[l], a[l+1]) : 0);
    }
    if ((r-l)%2 == 0) {
        int m = (l + r) / 2;
        if (k % 2 == 0) {
            ll c0 = calc(l, m-1, k/2);
            ll c01 = calc(l, m-1, k/2-1);
            ll c1 = calc(m+1, r, k/2);
            ll c11 = calc(m+1, r, k/2-1);
            ll c2 = calc(m+2, r, k/2-1);
            ll c3 = calc(m+2, r, k/2);
            ll c4 = calc(l, m-2, k/2-1);
            ll c5 = calc(l, m-2, k/2);
            ll res = c0 + c1;
            res = max(res, a[m] + c0 + c2);
            res = max(res, a[m] + c01 + c3);
            res = max(res, a[m-1] + c4 + c1);
            res = max(res, a[m-1] + c5 + c11);
            return res;
        } else {
            ll c0 = calc(l, m-1, k/2);
            ll c1 = calc(l, m-1, k/2+1);
            ll c2 = calc(m+1, r, k/2);
            ll c3 = calc(m+1, r, k/2+1);
            ll c4 = calc(m+2, r, k/2);
            ll c5 = calc(l, m-2, k/2);
            ll res = c0 + c3;
            res = max(res, c1 + c2);
            res = max(res, a[m] + c0 + c4);
            res = max(res, a[m-1] + c5 + c2);
            return res;
        }
    } else if ((r - l) / 2 < k) {
        if (l % 2 == 0) {
            return sum[0][r] - sum[0][l];
        } else {
            return sum[1][r] - sum[1][l];
        }
    } else {
        int m = (l + r) / 2;
        if (k % 2 == 1) {
            ll c0 = calc(l, m-1, k/2);
            ll c1 = calc(m+2, r, k/2);
            ll c2 = calc(l, m, k/2);
            ll c3 = calc(l, m, k/2+1);
            ll c4 = calc(m+1, r, k/2);
            ll c5 = calc(m+1, r, k/2+1);
            ll res = a[m] + c0 + c1;
            res = max(res, c2+c5);
            res = max(res, c3+c4);
            return res;
        } else {
            ll c0 = calc(l, m-1, (k-1)/2);
            ll c1 = calc(l, m-1, (k-1)/2+1);
            ll c2 = calc(m+2, r, (k-1)/2);
            ll c3 = calc(m+2, r, (k-1)/2+1);
            ll c4 = calc(l, m, k/2);
            ll c5 = calc(m+1, r, k/2);
            ll res = a[m] + c0 + c3;
            res = max(res, a[m] + c1 + c2);
            res = max(res, c4+c5);
            return res;
        }
    }
}

ll solve() {
    for (int i = 1; i <= n; i++) {
        sum[0][i] = sum[0][i-1];
        sum[1][i] = sum[1][i-1];
        sum[(i-1)%2][i] += a[i-1];
    }

    return calc(0, n, n/2);
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}