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AQgAhDSAPIQkgACEKA0ACQCAIIBMgE0I/hyAJIAlCP4cQASAMIA0gDnwgBiAHKQMAIgsgBSALfiAKVa19fnwiCyAMIAtVGyEMIAQgDXwiDSAUWQ0AIAogF3wgCSAVfSEJIAogA30hCkJ/VQ0BCwsgAiAOfCIOIBJZDQEgDyAWfSEPIAAgAX0iAEJ/VQ0ACwsgCEEwaiQAIBEgECARIBBVGyIAIAwgACAMVRsLbgEGfiAAIANC/////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, a1: i64, b1: i64, a2: i64, b2: i64, a3: i64, b3: i64) -> i64 { co_solve(n as i64, [(a1, b1), (a2, b2), (a3, b3)]) .max(co_solve(n as i64, [(a2, b2), (a3, b3), (a1, b1)])) .max(co_solve(n as i64, [(a3, b3), (a1, b1), (a2, b2)])) } fn co_solve(n: i64, ab: [(i64, i64); 3]) -> i64 { let [(ap, bp), (aq, bq), (ar, br)] = ab; if ap == 0 { return 0; } // a_p_inv = ceil(2^62 / a_p) = floor((2^62 - 1) / a_p) + 1 let apinv = ((1i64 << 62) - 1) / ap + 1; let (imax, jmax) = (ap * bq, ap * br); let (mut r, mut i, mut di) = (0, 0, n); while i < imax && di >= 0 { let (mut j, mut dj) = (0, di); while j < jmax && dj >= 0 { // 準定数を除数とする除算の定数倍高速化 // 0 < a_p <= 10^5, 0 <= d_j <= n <= 10^13 < 2^62/10^5 <= 2^62 / a_p <= ceil(2^62 / a_p) = a_p_inv < ((2^62 / a_p) + 1) // quot = floor((n - (i * a_q + j * a_r)) / a_p) = floor(d / a_p) = floor((d * 4) * ceil(2^62 / a_p) / 2^64) - (0 または 1) let qsim = ((((dj * 4) as u128) * (apinv as u128)) >> 64) as i64; let quot = qsim - i64::from(qsim * ap > dj); r.chmax(quot * bp + i + j); dj -= ar; j += br; } di -= aq; i += bq; } r } 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; } } } */