//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: [u64; n], } let mut b = vec![]; b.extend_from_slice(&a); b.extend_from_slice(&a); let b = b; let valid = |m: u64| -> bool { let mut dp = vec![2 * n; 2 * n + 1]; let mut r = 0; let mut sum = 0; for i in 0..(2 * n) { while r < 2 * n && sum < m { sum += b[r]; r += 1; } dp[i] = r; sum -= b[i]; } let mul = |a: &[usize], b: &[usize]| -> Vec { let mut c = vec![0; a.len()]; for (i, a) in a.iter().enumerate() { c[i] = b[*a]; } c }; let mut t = (0..=(2 * n)).collect::>(); let mut s = dp; let mut k = k; while k > 0 { if k & 1 == 1 { t = mul(&t, &s); } s = mul(&s, &s); k >>= 1; } let mut ok = false; for (i, t) in t.iter().enumerate().take(n) { ok |= *t - i <= n; } ok }; let mut ok = 1; let mut ng = a.iter().sum::() + 1; while ng - ok > 1 { let mid = (ok + ng) / 2; if valid(mid) { ok = mid; } else { ng = mid; } } println!("{}", ok); } fn main() { run(); }