// O(A^2) DP解法 // 入力制約: 0 <= n <= 10^13, 0 < a_i < 2048, 0 <= b_i, b_max * floor(n / a_min) <= 2^61 fn solve(n: u64, mut ab: [(u64, u64); 3]) -> u64 { if ab[1].0 * ab[0].1 < ab[0].0 * ab[1].1 { ab.swap(0, 1); } if ab[2].0 * ab[0].1 < ab[0].0 * ab[2].1 { ab.swap(0, 2); } let [(a1, b1), (a2, b2), (a3, b3)] = ab; // w = max(floor(n / a1) - (a2 + a3), 0) let w = (n / a1).saturating_sub(a2 + a3); let mut dp = [i64::MIN; 2048]; let mut v = (w * b1) as i64; for i in 0..=(n - w * a1) { v = v .max(dp[(i.wrapping_sub(a1) & 2047) as usize] + (b1 as i64)) .max(dp[(i.wrapping_sub(a2) & 2047) as usize] + (b2 as i64)) .max(dp[(i.wrapping_sub(a3) & 2047) as usize] + (b3 as i64)); dp[(i & 2047) as usize] = v; } v as u64 } fn main() { let mut stdinlock = std::io::stdin().lock(); let mut lines = std::io::BufRead::lines(&mut stdinlock).map_while(Result::ok); let n = lines.next().unwrap().parse::().unwrap(); let mut ab = [(0u64, 0u64); 3]; for e in ab.iter_mut() { let s = lines.next().unwrap(); let mut t = s.split_whitespace(); *e = ( t.next().unwrap().parse::().unwrap(), t.next().unwrap().parse::().unwrap(), ); } println!("{}", solve(n, ab)); }