#include #include #include #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) using namespace std; template void setmax(T & a, T const & b) { if (a < b) a = b; } const int inf = 1e9+7; const int N0 = 1000; const int N1 = 100; const int N2 = 1; struct wallet_t { int n0, n1, n2; }; int sum(wallet_t const & a) { return N0*a.n0 + N1*a.n1 + N2*a.n2; } bool valid(wallet_t const & a) { return a.n0 >= 0 and a.n1 >= 0 and a.n2 >= 0; } wallet_t operator + (wallet_t const & a, wallet_t const & b) { return (wallet_t) { a.n0 + b.n0, a.n1 + b.n1, a.n2 + b.n2 }; } wallet_t operator - (wallet_t const & a, wallet_t const & b) { return (wallet_t) { a.n0 - b.n0, a.n1 - b.n1, a.n2 - b.n2 }; } bool operator < (wallet_t const & a, wallet_t const & b) { return sum(a) < sum(b); } // weak istream & operator >> (istream & in, wallet_t & a) { return in >> a.n0 >> a.n1 >> a.n2; } int main() { wallet_t wa, wb, wc; int db, dc; cin >> wa >> db >> wb >> dc >> wc; int ans = - inf; map dp; priority_queue que; que.push(wa); while (not que.empty()) { wallet_t w = que.top(); que.pop(); setmax(ans, dp[w]); repeat (i0,w.n0+1) { repeat (i1,w.n1+1) { auto foo = [&](int d, wallet_t wd) { int i2 = (d - N0*i0 - N1*i1) / N2; if (0 <= i2 and i2 <= w.n2) { wallet_t nw = w - ((wallet_t) { i0, i1, i2 }) + wd; if (not dp.count(nw)) que.push(nw); setmax(dp[nw], dp[w]+1); } }; foo(db, wb); foo(dc, wc); } } } cout << ans << endl; return 0; }