#include #include #include using ll = long long; using std::swap; int main() { ll n, a1, b1, a2, b2, a3, b3; std::cin >> n >> a1 >> b1 >> a2 >> b2 >> a3 >> b3; if (a2 * b1 < a1 * b2) { swap(a1, a2); swap(b1, b2); } if (a3 * b1 < a1 * b3) { swap(a1, a3); swap(b1, b3); } ll r = 0, a1b2 = a1 * b2, a1b3 = a1 * b3; for (ll i = 0, s = n; i < a1b2 && s >= 0; i += b2, s -= a2) { for (ll j = 0, t = s; j < a1b3 && t >= 0; j += b3, t -= a3) { r = std::max(r, t / a1 * b1 + i + j); } } std::cout << r << std::endl; return 0; }