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: Runtime Error)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // int64_t

#define ARR_MAX 200000
#define BUF_SIZE (11*ARR_MAX+100)

int get_int(void) {
  int num;
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return 0;
  sscanf(line, "%d", &num);
#else
#error
#endif
  return num;
}

// <arr[0]> <arr[1]> .. <arr[size-1]>
int fget_array(int64_t *arr, int size) {
#ifdef BUF_SIZE
    char line[BUF_SIZE];
    char *tmpbuf = line;
    int i;
    if(!fgets(line, BUF_SIZE, stdin)) return -1;
    for(i = 0; i < size; i++) {
        char *tmp = strtok(tmpbuf, " ");
        arr[i] = strtoll(tmp, NULL, 10);
        tmpbuf = NULL;
    }
#else
#error
#endif
    return 0;
}

#define max(a,b) ((a) > (b) ? (a) : (b))

int main(void) {
    int num = get_int();
    int64_t arr[ARR_MAX];
    int i, j;
    int64_t ans = 0;

    fget_array(arr, num);
    if(!(num%2)) {
        int64_t sum1 = 0;
        int64_t sum2 = 0;
        for(i = 0; i < num; i+=2) {
            sum1 += arr[i];
        }
        for(i = 1; i < num; i+=2) {
            sum2 += arr[i];
        }
        if(!(num%2)) {
            ans = max(sum1, sum2);
            goto finish;
        }
    }
    // assume that num is odd number
    // sum2: move edge
    // static int64_t cum[ARR_MAX+1];
    // static int64_t cum2[ARR_MAX+1];
    // for(i = 1; i <= num; i++) {
    //     int64_t elem = arr[i-1];
    //     cum[i] = cum[i-1] + elem;
    //     cum2[i] = cum2[i-1] + (i%2==0)*elem; // even sum
    // }
    // 1-indexed
    static int64_t dp[ARR_MAX+1][3];
    static int64_t dp_rev[ARR_MAX+1][3];
    for(i = 2; i < num; i+=2) {
        dp[i][0] = dp[i-2][0] + arr[i-2];
        dp[i][1] = max(dp[i-2][0], dp[i-2][1])+arr[i-1];
        dp[i][2] = max(dp[i][0], dp[i][1]);
    }
    for(i = 2; i < num; i+=2) {
        dp_rev[i][0] = dp_rev[i-2][0] + arr[num-i+1];
        dp_rev[i][1] = max(dp[i-2][0], dp_rev[i-2][1])+arr[num-i];
        dp_rev[i][2] = max(dp_rev[i][0], dp_rev[i][1]);
    }

    for(i = 0; i < num; i+=2) {
        // split [0, i), i, [i+1, num)
        int64_t sum1 = dp[i][2];
        int64_t sum2 = dp_rev[num-1-i][2];
#ifdef DEBUG
        printf("[pivot: %lld]: %lld+%lld\n", arr[i], sum1, sum2);
#endif
        ans = max(ans, sum1+sum2);
    }

finish:
    printf("%llu\n", ans);
    return 0;
}