let l = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"); let n = parseInt(l[0]); let [a1, b1] = l[1].split(" ").map(s => parseInt(s)); let [a2, b2] = l[2].split(" ").map(s => parseInt(s)); let [a3, b3] = l[3].split(" ").map(s => parseInt(s)); if (a2 * b1 < a1 * b2) { [a1, b1, a2, b2] = [a2, b2, a1, b1]; } if (a3 * b1 < a1 * b3) { [a1, b1, a3, b3] = [a3, b3, a1, b1]; } let r = 0, a1b2 = a1 * b2, a1b3 = a1 * b3; for (let p = 0, s = n; p < a1b2 && s >= 0; p += b2, s -= a2) { for (let q = 0, t = s; q < a1b3 && t >= 0; q += b3, t -= a3) { // ここでBigIntを使わないと精度不足になる //let u = BigInt(Math.floor(t / a1)) * BigInt(b1) + BigInt(p + q); let u = Math.floor(t / a1) * b1 + p + q; r = (r < u) ? u : r; } } console.log(r.toString());