fn run() { input! { n: usize, c: [(usize, i64); n], } let inf = 10i64.pow(9) * 50000 + 1; let mut dp = vec![vec![-inf; n + 1]; n + 1]; dp[0][0] = 0; for (i, &(c, v)) in c.iter().enumerate().rev() { let w = i + 1; let q = n / w; let mut c = c.min(q); let mut s = 1; while c > 0 { let a = c.min(s); c -= a; s <<= 1; let v = v * (a as i64); let w = w * a; for j in (a..=q).rev() { let src = std::mem::take(&mut dp[j - a]); let geta = (j - a) * (i + 1); for (dp, src) in dp[j][(w + geta)..].iter_mut().zip(src[geta..].iter()) { *dp = std::cmp::max(*dp, *src + v); } dp[j - a] = src; } } } use util::*; let ans = dp[1..].iter().map(|dp| dp.iter().max().unwrap()).join("\n"); println!("{}", ans); } fn main() { run(); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] 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_export] 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_export] 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") }; } // ---------- end input macro ---------- mod util { pub trait Join { fn join(self, sep: &str) -> String; } impl Join for I where I: Iterator, T: std::fmt::Display, { fn join(self, sep: &str) -> String { let mut s = String::new(); use std::fmt::*; for (i, v) in self.enumerate() { if i > 0 { write!(&mut s, "{}", sep).ok(); } write!(&mut s, "{}", v).ok(); } s } } }