const b64str = 'AGFzbQEAAAABFAJgBX9+fn5+AGAHfn5+fn5+fgF+AwMCAQAFAwEAEQYJAX8BQYCAwAALBxICBm1lbW9yeQIABXNvbHZlAAAKiwQCmQMCA38MfiMAQRBrIgckAAJAIAIgA34gASAEflkEQCAEIQsgAyEKIAIhBCABIQMMAQsgAiELIAEhCgsCQCAEIAV+IAMgBn5ZBEAgBiEOIAUhDSAEIQYgAyEFDAELIAQhDiADIQ0LQgAhAQJAIAVQDQAgBSALfiIRQgFTDQBC//////////8/IAV/IAUgDn4iE0IAVwRAIAshBANAQgAhASAAIAp9IgIgAFMgCkIAVXMNAiAEIBFTIAIhACAEIAt8IQQNAAsMAQtCAXwhEiANQgKGIRQgCkIChiEVIABCAoYhDyAHQQhqIQggDUIAVSEJQgAhAQNAQgAhAiAPIQMgACEEA0ACQCAHIBIgEkI/hyADIANCP4cQASABIAIgEHwgBiAIKQMAIgwgBSAMfiAEVa19fnwiDCABIAxVGyEBIAkgBCANfSIMIARTcw0AIAMgFH0hAyAMIQQgAiAOfCICIBNTDQELCyAAIAp9IgIgAFMgCkIAVXMNASAPIBV9IQ8gAiEAIAsgEHwiECARUw0ACwsgB0EQaiQAIAELbgEGfiAAIANC/////w+DIgUgAUL/////D4MiBn4iByAFIAFCIIgiCH4iCSAGIANCIIgiBn58IgVCIIZ8Igo3AwAgACAKIAdUrSAGIAh+IAUgCVStQiCGIAVCIIiEfHwgASAEfiACIAN+fHw3AwgLAG8JcHJvZHVjZXJzAghsYW5ndWFnZQEEUnVzdAAMcHJvY2Vzc2VkLWJ5AwVydXN0Yx0xLjcwLjAgKDkwYzU0MTgwNiAyMDIzLTA1LTMxKQZ3YWxydXMGMC4xOS4wDHdhc20tYmluZGdlbgYwLjIuODcALA90YXJnZXRfZmVhdHVyZXMCKw9tdXRhYmxlLWdsb2JhbHMrCHNpZ24tZXh0'; let imports = {}; let wasm; //const bytes = Buffer.from(b64str, 'base64'); const bytes = Uint8Array.from([...atob(b64str)].map(s=>s.charCodeAt(0))); const wasmModule = new WebAssembly.Module(bytes); const wasmInstance = new WebAssembly.Instance(wasmModule, imports); wasm = wasmInstance.exports; let l = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"); let n = BigInt(l[0]); let [a1, b1] = l[1].split(" ").map(s => BigInt(s)); let [a2, b2] = l[2].split(" ").map(s => BigInt(s)); let [a3, b3] = l[3].split(" ").map(s => BigInt(s)); console.log(wasm.solve(n,a1,b1,a2,b2,a3,b3).toString()); /* // WebAssembly Source Code (Rust): use wasm_bindgen::prelude::*; #[wasm_bindgen] pub fn solve( n: i64, mut a1: i64, mut b1: i64, mut a2: i64, mut b2: i64, mut a3: i64, mut b3: i64, ) -> i64 { if a2 * b1 < a1 * b2 { std::mem::swap(&mut a1, &mut a2); std::mem::swap(&mut b1, &mut b2); } if a3 * b1 < a1 * b3 { std::mem::swap(&mut a1, &mut a3); std::mem::swap(&mut b1, &mut b3); } if a1 == 0 { return 0; } // a1inv = ceil(2^62 / a1) = floor((2^62 - 1) / a1) + 1 let a1inv = ((1i64 << 62) - 1) / a1 + 1; let (a1b2, a1b3) = (a1 * b2, a1 * b3); let (mut r, mut p, mut s) = (0, 0, n); while p < a1b2 { let (mut q, mut t) = (0, s); while q < a1b3 { // 準定数を除数とする除算の定数倍高速化 // 0 < a1 <= 10^5, 0 <= t <= n <= 10^13 < 2^62/10^5 <= 2^62 / a1 <= ceil(2^62 / a1) = a1inv < ((2^62 / a1) + 1) // quot = floor((n - (i * a2 + j * a3)) / a1) = floor(t / a1) = floor((t * 4) * ceil(2^62 / a1) / 2^64) - (0 または 1) let qsim = ((((t * 4) as u128) * (a1inv as u128)) >> 64) as i64; let quot = qsim - i64::from(qsim * a1 > t); debug_assert_eq!(t / a1, quot); r.chmax(quot * b1 + p + q); if let Some(d) = t.checked_sub(a3) { t = d; q += b3; } else { break; } } if let Some(d) = s.checked_sub(a2) { s = d; p += b2; } else { break; } } r } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl Change for T { #[inline] fn chmax(&mut self, x: T) { if *self < x { *self = x; } } #[inline] fn chmin(&mut self, x: T) { if *self > x { *self = x; } } } */