fn main() { let mut input = String::with_capacity(1024); std::io::Read::read_to_string(&mut std::io::stdin(), &mut input).unwrap(); let mut tokens = input.split_whitespace(); let n = tokens.next().unwrap().parse::().unwrap(); let [a, b, c] = [ tokens.next().unwrap().parse::().unwrap(), tokens.next().unwrap().parse::().unwrap(), tokens.next().unwrap().parse::().unwrap(), ]; let mut dp = vec![0u64; n + 1]; let mut result = 0; for i in 0..=n { match i { 0..=2 => {} 3..=4 => { result.chmax(dp[i - 3] + a); } 5..=9 => { result.chmax(dp[i - 3] + a); result.chmax(dp[i - 5] + b); } _ => { result.chmax(dp[i - 3] + a); result.chmax(dp[i - 5] + b); result.chmax(dp[i - 10] + c); } } dp[i] = result; } println!("{}", result); } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl Change for T { fn chmax(&mut self, x: T) { if *self < x { *self = x; } } fn chmin(&mut self, x: T) { if *self > x { *self = x; } } }