#include using namespace std; using LL = long long int; #define incID(i, l, r) for(int i = (l) ; i < (r); i++) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--) #define incII(i, l, r) for(int i = (l) ; i <= (r); i++) #define decII(i, l, r) for(int i = (r) ; i >= (l); i--) #define inc(i, n) incID(i, 0, n) #define dec(i, n) decID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec1(i, n) decII(i, 1, n) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define FI first #define SE second #define FR front() #define BA back() #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() auto setmin = [](auto & a, auto b) { return (b < a ? a = b, true : false); }; auto setmax = [](auto & a, auto b) { return (b > a ? a = b, true : false); }; auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); }; auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); }; #define SI(v) static_cast(v.size()) #define RF(e, v) for(auto & e: v) #define until(e) while(! (e)) #define if_not(e) if(! (e)) #define ef else if #define UR assert(false) // 以下、入出力関係のテンプレ案のテスト auto * IS = & cin; // input elements (as a tuple) template void in_(U & t) { } template void in_(U & t) { (* IS) >> get(t); in_(t); } template auto in() { tuple t; in_, 0, T ...>(t); return t; } // input a array template auto ain() { array a; inc(i, N) { (* IS) >> a[i]; } return a; } // input a (multi-dimensional) vector // template T vin() { return * istream_iterator(* IS); } template T vin() { T v; (* IS) >> v; return v; } template auto vin(N n, M ... m) { vector(m ...))> v(n); inc(i, n) { v[i] = vin(m ...); } return v; } // input multi-column (as a tuple of vector) template void colin_(U & t) { } template void colin_(U & t) { // get(t).emplace_back(* istream_iterator(* IS)); colin_(t); A a; (* IS) >> a; get(t).push_back(a); colin_(t); } template auto colin(int n) { tuple ...> t; inc(i, n) { colin_ ...>, 0, T ...>(t); } return t; } auto * OS = & cout; string SP = " ", LN = "\n"; // output elements void out() { (* OS) << LN << flush; } template void out(A && a ) { (* OS) << a ; out( ); } template void out(A && a, B && ... b) { (* OS) << a << SP; out(b ...); } // output a (multi-dimensional) vector template ostream & operator<<(ostream & os, vector const & v) { inc(i, v.size()) { os << (i == 0 ? "" : SP) << v[i]; } return os << flush; } template void vout_(T && v) { (* OS) << v; } template void vout_(T && v, A a, B ... b) { for(auto && w: v) { vout_(w, b ...); (* OS) << a; } } template void vout(T && v, A ... a) { vout_(v, a ...); out(); } // ---- ---- template T MV(T v) { return v; } template auto MV(T v, int a, U ... b) { return vector(a, MV(v, b ...)); } template istream & operator>>(istream & s, array & a) { RF(e, a) { s >> e; } return s; } template ostream & operator<<(ostream & s, array const & a) { inc(i, SI(a)) { s << (i == 0 ? "" : " ") << a[i]; } return s; } int main() { const int M = 10000; auto dp = MV(-1, M + 1, M/1000 + 1, M/100 + 1); auto sum = [](int x, int y, int z) { return 1000*x + 100*y + 1*z; }; auto one = [](int s, int x, int y) { return s - (1000*x + 100*y); }; { auto [x, y, z] = ain(); dp[sum(x, y, z)][x][y] = 0; } auto v = vin>(2); int ans = 0; decII(s, 0, M) { decII(x, 0, M/1000) { decII(y, 0, M/100) { int z = one(s, x, y); for(auto [ss, xx, yy, zz]: v) { int t = dp[s][x][y]; if(t == -1) { continue; } setmax(ans, t); if(s < ss) { continue; } int sss = s - ss + sum(xx, yy, zz); int X = min(x, ss/1000); ss -= X * 1000; int Y = min(y, ss/100); ss -= Y * 100; int Z = min(z, ss/1); ss -= Z * 1; if(ss != 0) { continue; } setmax(dp[sss][x - X + xx][y - Y + yy], t + 1); } } } } out(ans); }