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 Rust (1.42.0) (judge verdict: Accepted)

use std::cmp::max;
use std::collections::BTreeMap;

const INF: i64 = 1e16 as i64;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let a: Vec<i64> = sc.vec(n);

    let total_select = n / 2;
    let total_jump = n - total_select;

    let mut dp = BTreeMap::new();
    dp.insert((1, true), 0);
    dp.insert((0, false), a[0]);
    for i in 1..n {
        let a = a[i];
        let remain = n - (i + 1);
        let max_remain = (remain + 1) / 2;

        let mut next = BTreeMap::new();
        for ((jump_count, prev_jump), cur_max) in dp.into_iter() {
            let cur_count = i - jump_count;
            let can_jump = max_remain + cur_count >= total_select;
            if can_jump {
                let cur = next.entry((jump_count + 1, true)).or_insert(cur_max);
                if *cur < cur_max {
                    *cur = cur_max;
                }
            }
            if prev_jump {
                let cur = next.entry((jump_count, false)).or_insert(cur_max + a);
                if *cur < cur_max + a {
                    *cur = cur_max + a;
                }
            }
        }
        dp = next;
    }

    let mut ans = std::i64::MIN;
    if let Some(a) = dp.get(&(total_jump, true)) {
        ans = max(ans, *a);
    }
    if let Some(a) = dp.get(&(total_jump, false)) {
        ans = max(ans, *a);
    }

    println!("{}", ans);
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}