fn main() { let input = Input::read(); assert!(1 <= input.n && input.n <= 2000); assert!(1 <= input.m && input.m <= 2000); assert_eq!(input.a.len(), input.n); assert!(input .a .iter() .all(|&a| -1_000_000_000 <= a && a <= 1_000_000_000)); let s = { let mut v = vec![0; input.n + 1]; for (i, a) in input.a.iter().enumerate() { v[i + 1] = v[i] + a; } v }; let mut x = vec![vec![0; input.n + 1]; input.m]; let mut y = vec![vec![0; input.n + 1]; input.m]; let mut sign = 1; for i in 0..input.m { for j in 0..=input.n { x[i][j] = if i == 0 { 0 } else { y[i - 1][j] } + sign * s[j]; } for j in 1..=input.n { y[i][j] = y[i][j - 1].max(x[i][j]); } sign = -sign; } let mut index = input.n; for i in (0..input.m).rev() { for j in (0..=index).rev() { if x[i][j] == y[i][index] { println!("{}", j); index = j; break; } } } } struct Input { n: usize, m: usize, a: Vec, } impl Input { fn read() -> Input { let stdin = std::io::stdin(); let (n, m) = { let mut s = String::new(); stdin.read_line(&mut s).unwrap(); if let [n, m] = *my_split(&s).unwrap() { (n.parse().unwrap(), m.parse().unwrap()) } else { panic!(); } }; let a = { let mut s = String::new(); stdin.read_line(&mut s).unwrap(); let a = my_split(&s).unwrap(); assert_eq!(a.len(), n); a.iter().map(|x| x.parse().unwrap()).collect() }; assert_eq!(stdin.read_line(&mut String::new()).unwrap(), 0); Input { n: n, m: m, a: a } } } fn my_split(s: &str) -> Option> { enum State { Word(usize), Space, End, } let mut state = State::Word(0); let mut ret = Vec::new(); for (i, c) in s.char_indices() { let prev = match state { State::End => return None, State::Word(i) => i, State::Space => { state = State::Word(i); i } }; if c == ' ' || c == '\n' { ret.push(&s[prev..i]); state = if c == ' ' { State::Space } else { State::End }; } } match state { State::End => Some(ret), _ => None, } }