#include using namespace std; using i64 = int64_t; void chmax(i64& a, i64 b) {if(a < b) a = b; } i64 solve() { i64 N; cin >> N; vector> A(3); for(auto& [a, b] : A) cin >> a >> b; sort(A.begin(), A.end(), [&](pair a, pair b) { return a.first * b.second < a.second * b.first; }); const i64 MX = 2e6; vector dp(MX + 1); for(i64 i = 0; i < MX; i++) for(auto [a, b] : A) if(i + a <= MX) chmax(dp[i + a], dp[i] + b); if(N <= MX) return dp[N]; const auto [a, b] = A[0]; for(i64 i = MX; ; i--) if((N - i) % a == 0) return dp[i] + (N - i) / a * b; } int main() { cout << solve() << endl; }