#include // fill_n, max #include // cin, cout, endl #include // numeric_limits #include // swap 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 dp[2048], w = std::max(n / a1 - a2 - a3, 0LL), m = n - w * a1, r = w * b1; std::fill_n(dp, 2048, std::numeric_limits::min()); for (ll i = 0; i <= m; i++) { dp[i & 2047] = r = std::max({ dp[(i - a1) & 2047] + b1, dp[(i - a2) & 2047] + b2, dp[(i - a3) & 2047] + b3, r, }); } std::cout << r << std::endl; return 0; }