let l = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"); let n = BigInt(l[0]); let [a0, b0] = l[1].split(" ").map(s => BigInt(s)); let [a1, b1] = l[2].split(" ").map(s => BigInt(s)); let [a2, b2] = l[3].split(" ").map(s => BigInt(s)); function co_solve(n, ar, br, as, bs, at, bt) { let r = 0n; for (let i = 0n, di = n; i < ar && di >= 0n; i += 1n, di -= as) { for (let j = 0n, dj = di; j < ar && dj >= 0n; j += 1n, dj -= at) { let t = (dj / ar) * br + i * bs + j * bt; r = (r < t) ? t : r; } } return r; } let r1 = co_solve(n, a0, b0, a1, b1, a2, b2); let r2 = co_solve(n, a1, b1, a2, b2, a0, b0); let r3 = co_solve(n, a2, b2, a0, b0, a1, b1); let r = r1; r = (r < r2) ? r2 : r; r = (r < r3) ? r3 : r; console.log(r.toString());