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MCKw9tdXRhYmxlLWdsb2JhbHMrCHNpZ24tZXh0'; 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; } 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 { r.chmax(dj / ap * 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; } } } */