// ---------- begin chmin, chmax ---------- trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ---------- //https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // fn run() { input! { n: usize, k: usize, a: [i64; n], } let ini = std::i64::MIN / 10; if k == 1 { for i in 0..n { let mut ans = ini; if i > 0 { ans.chmax(a[i - 1]); } if i + 1 < n { ans.chmax(a[i + 1]); } println!("{}", ans); } return; } let mut dp = vec![vec![vec![ini; n]; n]; 2]; for i in 0..n { let mut sum = 0; for j in (i..n).take(k) { sum += (k - (j - i)) as i64 * a[j]; if j > i { let rem = k - (j - i); let x = (j ^ rem) & 1; let l = std::cmp::max(i, j.saturating_sub(rem)); dp[x][l][j - (rem & 1)].chmax(sum); } } } for i in 0..n { let mut sum = 0; for j in (0..=i).rev().take(k) { sum += (k - (i - j)) as i64 * a[j]; if j < i { let rem = k - (i - j); let x = (j ^ rem) & 1; let r = std::cmp::min(i, j + rem); dp[x][j + (rem & 1)][r].chmax(sum); } } } for mid in 0..n { let mut sum = 0; for l in (0..=mid).rev() { sum += (k - (mid - l)) as i64 * a[l]; let mut sum = sum; for r in (mid + 1)..n { if r - mid + 2 * (mid - l) > k { break; } sum += (k - 2 * (mid - l) - (r - mid)) as i64 * a[r]; let rem = k - 2 * (mid - l) - (r - mid); let x = (r ^ rem) & 1; let l = std::cmp::max(l, r.saturating_sub(rem)); dp[x][l][r - (rem & 1)].chmax(sum); } } } for mid in 0..n { let mut sum = 0; for r in mid..n { sum += (k - (r - mid)) as i64 * a[r]; let mut sum = sum; for l in (0..mid).rev() { if 2 * (r - mid) + mid - l > k { break; } sum += (k - 2 * (r - mid) - (mid - l)) as i64 * a[l]; let rem = k - 2 * (r - mid) - (mid - l); let x = (r ^ rem) & 1; let r = std::cmp::min(r, l + rem); dp[x][l + (rem & 1)][r].chmax(sum); } } } for dp in dp.iter_mut() { for i in 0..n { for j in (i..n).rev() { let v = dp[i][j]; if i < j { dp[i + 1][j].chmax(v); dp[i][j - 1].chmax(v); } } } } for i in 0..n { println!("{}", dp[i & 1][i][i]); } } fn main() { run(); }