#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; vector> a(3); for(int i = 0; i < 3; i++){ cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end(), [&](pair lhs, pair rhs){ return lhs.second * rhs.first < lhs.first * rhs.second; }); int lcmv = 2000000; vector dp(lcmv + 1); for(int i = 0; i < 3; i++){ ll v1, v2; tie(v1, v2) = a[i]; for(int j = 0; j + v1 <= lcmv; j++){ dp[j + v1] = max(dp[j + v1], dp[j] + v2); } } ll ans = 0; for(int j = 0; j <= lcmv && j <= n; j++){ for(int k = 0; k < 3; k++){ ans = max(ans, dp[j] + ((n - j) / a[k].first) * a[k].second); } } cout << ans << '\n'; }