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