#include #include #define rep2(i, k, n) for (i64 i = (i64)(k); i < (i64)(n); i++) #define rep(i, n) rep2(i, 0, n) #define all(x) begin(x), end(x) #ifdef ENV_LOCAL #define dump \ if (1) cerr #else #define dump \ if (0) cerr #endif using namespace std; using namespace std::string_literals; using i32 = int32_t; using i64 = int64_t; using f64 = double; using f80 = long double; using vi32 = vector; using vi64 = vector; // using namespace harudake; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i64 n; cin >> n; vector> vp; rep(i, 3) { i64 a, b; cin >> a >> b; vp.emplace_back(a, b); } i64 m = vp[0].first * vp[1].first * 2; vi64 dp1(m + 1); rep(i, 2) { rep(j, m + 1) { i64 jj = j + vp[i].first; if (jj > m) break; dp1[jj] = max(dp1[jj], dp1[j] + vp[i].second); } } rep(i, m) dp1[i + 1] = max(dp1[i + 1], dp1[i]); i64 mm = vp[0].first * vp[1].first; i64 ans = 0; rep(i, mm) { i64 rem = n - vp[2].first * i; if (rem < 0) break; i64 q = rem / mm; i64 r = rem % mm; if (q == 0) { ans = max(ans, dp1[r] + vp[2].second * i); continue; } i64 triple = mm * vp[2].first; i64 diff = mm * vp[2].second - dp1[mm] * vp[2].first; if (diff > 0) { i64 x = q / vp[2].first; i64 rem2 = rem - triple * x; i64 q2 = rem2 / mm; if (q2 == 0) { ans = max(ans, dp1[r] + x * mm * vp[2].second + vp[2].second * i); if (x > 0) { ans = max(ans, dp1[mm + r] + dp1[mm] * vp[2].first + (x - 1) * mm * vp[2].second + vp[2].second * i); } } else { ans = max(ans, dp1[mm + r] + dp1[mm] * (q2 - 1) + x * mm * vp[2].second + vp[2].second * i); } } else { ans = max(ans, dp1[r + mm] + dp1[mm] * (q - 1) + vp[2].second * i); } } cout << ans << endl; return 0; }