use std::cmp; use std::io::{self, Read}; #[derive(Debug)] struct Input { n: usize, vs: Vec, } fn next_token(cin_lock: &mut io::StdinLock) -> String { cin_lock .by_ref() .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::() } fn read_input(cin_lock: &mut io::StdinLock) -> Input { let n = next_token(cin_lock).parse().unwrap(); let vs = (0..n).map(|_| next_token(cin_lock).parse().unwrap()).collect(); Input { n, vs, } } fn solve(input: Input, _cin_lock: &mut io::StdinLock) { let answer = solve_dp(input); println!("{}", answer); } fn solve_dp(input: Input) -> i32 { if input.n <= 2 { let first = *input.vs.get(0).unwrap(); let second = input.vs.get(1).map(|x| *x).unwrap_or(0); return cmp::max(first, second); } let mut dp = vec![0; input.n]; dp[0] = input.vs[0]; dp[1] = cmp::max(input.vs[0], input.vs[1]); for (i, v) in input.vs.into_iter().enumerate().skip(2) { let take = *dp.get(i - 2).unwrap() + v; let skip = *dp.get(i - 1).unwrap(); dp[i] = cmp::max(take, skip); } let mut it = dp.into_iter().rev(); cmp::max(it.next().unwrap(), it.next().unwrap()) } fn main() { let cin = io::stdin(); let mut cin_lock = cin.lock(); let input = read_input(&mut cin_lock); solve(input, &mut cin_lock); }