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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    int[] nums;
    int N;
    long[] preSum;

    public Main(int[] nums){
        this.nums=nums;
        N=nums.length;
    }
    long getMaxSum(){
        if(N<4){
            Arrays.sort(nums);
            return nums[N-1];
        }

        long max=0;
//      结尾一定要被取到
        long[] ext0max=new long[N];
        Arrays.fill(ext0max, -1000000000000000L);
        long[] ext1max=new long[N];
        Arrays.fill(ext1max, -1000000000000000L);
        long[] ext2max=new long[N];
        Arrays.fill(ext2max, -1000000000000000L);
        ext0max[0]=nums[0];
        ext0max[2]=nums[0]+nums[2];
        ext1max[1]=nums[1];
        ext1max[3]=nums[3]+Math.max(nums[0],nums[1]);
        ext2max[2]=nums[2];
        for (int i = 4; i <N ; i++) {
            ext0max[i]=ext0max[i-2]+nums[i];
            ext1max[i]=Math.max(ext1max[i-2]+nums[i],ext0max[i-3]+nums[i]);
            ext2max[i]=Math.max(ext2max[i-2]+nums[i],Math.max(ext1max[i-3]+nums[i],ext0max[i-4]+nums[i]));
        }
        if (N%2==0){
            max=Math.max(ext1max[N-1],ext0max[N-2]);
        }else{
            max=Math.max(ext2max[N-1],Math.max(ext1max[N-2],ext0max[N-3]));
        }
        return max;
    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int N=scan.nextInt();
        int[] nums =new int[N];
        for (int i = 0; i <N ; i++) {
            nums[i]=scan.nextInt();
        }
        Main solve=new Main(nums);
        System.out.println(solve.getMaxSum());
    }
}