#include #include using namespace std; using namespace atcoder; #define rep(i, n) REP(i, 0, n) #define REP(i, s, e) for (ll i = (s); i < (ll)(e); i++) #define repr(i, n) REPR(i, n, 0) #define REPR(i, s, e) for (ll i = (ll)(s - 1); i >= (ll)(e); i--) #define all(r) r.begin(), r.end() #define rall(r) r.rbegin(), r.rend() typedef long long ll; typedef vector vi; typedef vector vl; template T chmax(T& a, const U& b) { if (a >= b) return false; a = b; return true; } template T chmin(T& a, const U& b) { if (a <= b) return false; a = b; return true; } void yes_no(bool f, string yes = "Yes", string no = "No") { cout << (f ? yes : no) << "\n"; } void solve() { vi a(3); rep(i, 3) cin >> a[i]; vi d(2); vector b(2, vi(3)); rep(i, 2) { cin >> d[i]; rep(j, 3) cin >> b[i][j]; } map mp; using T = tuple; priority_queue q; q.emplace(a[0] * 1000 + a[1] * 100 + a[2], 0, a); mp[a] = 0; int ans = 0; while (!q.empty()) { auto [x, y, z] = q.top(); q.pop(); if (mp[z] != y) continue; rep(i, 2) if (x >= d[i]) { int nx, ny; vi nz = z; nx = x - d[i] + b[i][0] * 1000 + b[i][1] * 100 + b[i][2]; ny = y + 1; int r = d[i]; if (r >= 1000) { int k = min(nz[0], r / 1000); nz[0] -= k; r -= k * 1000; } if (r >= 100) { int k = min(nz[1], r / 100); nz[1] -= k; r -= k * 100; } if (r >= 0) { int k = min(nz[2], r); nz[2] -= k; r -= k; } if (r != 0) continue; rep(j, 3) nz[j] += b[i][j]; if (mp.count(nz) == 0 || mp[nz] < ny) { mp[nz] = ny; q.emplace(nx, ny, nz); chmax(ans, ny); } } } cout << ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); int t = 1; // multi-testcase // cin >> t; rep(ti, t) solve(); return 0; }