#include // max #include // cin, cout, endl #include // swap #include // vector using ll = long long; // O(A^2) DP解法 // 入力制約: 0 <= n <= 10^13, 0 < a_i < 2048, 0 <= b_min, floor(n / a_min) * b_max <= 2^60 int main() { ll n, a1, b1, a2, b2, a3, b3; std::cin >> n >> a1 >> b1 >> a2 >> b2 >> a3 >> b3; if (a2 * b1 < a1 * b2) { std::swap(a1, a2); std::swap(b1, b2); } if (a3 * b1 < a1 * b3) { std::swap(a1, a3); std::swap(b1, b3); } ll w = std::max(n / a1 - a2 - a3, 0LL), m = n - w * a1, r = w * b1; std::vector dp(m + 1, 0LL); for (ll i = 0; i <= m; i++) { dp[i] = r = std::max({ i >= a1 ? dp[i - a1] + b1 : 0, i >= a2 ? dp[i - a2] + b2 : 0, i >= a3 ? dp[i - a3] + b3 : 0, r, }); } std::cout << r << std::endl; return 0; }