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 w = Math.max(Math.floor(n / a1) - a2 - a3, 0); let m = n - w * a1; let dp = Array.from({length: m + 1}); dp[0] = 0; let r = 0; for (let i = 1; i <= m; i++) { dp[i] = r = Math.max( i >= a1 ? dp[i - a1] + b1 : 0, i >= a2 ? dp[i - a2] + b2 : 0, i >= a3 ? dp[i - a3] + b3 : 0, r ); } // ここでBigIntを使わないと精度不足になる console.log(`${BigInt(r) + BigInt(w) * BigInt(b1)}`);