https://atcoder.jp/contests/abc369/tasks/abc369_d
単なるDPです。
// Bonus EXP #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// process //////////////////// fn read_input() -> Vec<i64> { let _N: usize = read(); let A: Vec<i64> = read_vec(); A } type DP = (i64, i64); use std::cmp::max; fn update_DP(a: i64, dp: DP) -> DP { (max(dp.0, dp.1 + a * 2), max(dp.0 + a, dp.1)) } fn F(A: Vec<i64>) -> i64 { let N = A.len(); let mut dp: DP = (0, A[0]); for i in 1..N { dp = update_DP(A[i], dp) } max(dp.0, dp.1) } fn main() { let A = read_input(); println!("{}", F(A)) }