#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; }); ll lcmv = a[0].first * a[1].first / __gcd(a[0].first, a[1].first); 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++){ ans = max(ans, dp[j] + ((n - j) / a[2].first) * a[2].second); } cout << ans << '\n'; }