#include #include using ll = long long; ll solve(ll n, ll ap, ll bp, ll aq, ll bq, ll ar, ll br) { ll r = 0; for (ll i = 0, di = n; i < ap && di >= 0; i++, di -= aq) { for (ll j = 0, dj = di; j < ap && dj >= 0; j++, dj -= ar) { r = std::max(r, dj / ap * bp + i * bq + j * br); } } return r; } int main() { ll n, a1, b1, a2, b2, a3, b3; std::cin >> n >> a1 >> b1 >> a2 >> b2 >> a3 >> b3; std::cout << std::max({ solve(n, a1, b1, a2, b2, a3, b3), solve(n, a2, b2, a3, b3, a1, b1), solve(n, a3, b3, a1, b1, a2, b2), }) << std::endl; return 0; }