const b64str = 'AGFzbQEAAAABDAFgB35+fn5+fn4BfgMCAQAFAwEAEQcSAgZtZW1vcnkCAAVzb2x2ZQAACvsCAfgCAgF/Cn4CQCACIAN+IAEgBH5ZBEAgBCEJIAMhCCACIQQgASEDDAELIAIhCSABIQgLAkAgBCAFfiADIAZ+WQRAIAYhCiAFIQIgBCEGIAMhBQwBCyAEIQogAyECCyAFQgFTBEBCAA8LQgAhBCAAIAV/IAIgCHx9IgFCACABQgBVGyENQv////8/IAWAIQMCQCAFIAl+Ig5CAVMNACAAIAUgDX4iAX0iC0IAUw0AIAUgCn4iD0IAVwRAIAAgAX0gCH0hAyAJIQUDQCAFIA5ZDQIgBSAJfCEFIANCf1UgAyAIfSEDDQALDAELIANCAXwhECAAIAF9IAJ9IQxCACEAA0AgDCEBIAshA0IAIQUDQAJAIAQgACAFfCADIBB+QiKHIAZ+fCIRIAQgEVUbIQQgBSAKfCIFIA9ZDQAgAyACfSEDIAFCf1UgASACfSEBDQELCyAAIAl8IgAgDlkNASAMIAh9IQwgCyAIfSILQn9VDQALCyAEIAYgDX58CwBvCXByb2R1Y2VycwIIbGFuZ3VhZ2UBBFJ1c3QADHByb2Nlc3NlZC1ieQMFcnVzdGMdMS43MS4wICg4ZWRlM2FhZTIgMjAyMy0wNy0xMikGd2FscnVzBjAuMTkuMAx3YXNtLWJpbmRnZW4GMC4yLjg3ACwPdGFyZ2V0X2ZlYXR1cmVzAisPbXV0YWJsZS1nbG9iYWxzKwhzaWduLWV4dA=='; 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-pack build --release --target nodejs --no-typescript #[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^34 / a1) = floor((2^34 - 1) / a1) + 1 // 2^32 / a1 <= a1inv < 2^32 / a1 + 1 let a1inv = ((1i64 << 34) - 1) / a1 + 1; let (a1b2, a1b3) = (a1 * b2, a1 * b3); // w = max(floor(n / a1) - (a2 + a3), 0) let w = ((n / a1) - a2 - a3).max(0); let (mut r, mut p, mut s) = (0, 0, n - w * a1); while p < a1b2 && s >= 0 { let (mut q, mut t) = (0, s); while q < a1b3 && t >= 0 { // 準定数を除数とする除算の定数倍高速化 // 0 < {a1,a2,a3} < 2^15, 0 <= t < 2000*(2*2000+1) // a1 * t < 2^34 --> quot = floor(ceil(2^34 / a1) * t / 2^34) = floor(t / a1) let quot = t * a1inv >> 34; debug_assert_eq!(t / a1, quot); r.chmax(quot * b1 + p + q); t -= a3; q += b3; } s -= a2; p += b2; } r + w * b1 } 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; } } } */