import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.regex, std.conv, std.stdio, std.typecons; void main() { auto rd1 = readln.split.map!(to!int); auto a3 = rd1[0], a2 = rd1[1], a0 = rd1[2]; auto db = readln.chomp.to!int; auto rd2 = readln.split.map!(to!int); auto b3 = rd2[0], b2 = rd2[1], b0 = rd2[2]; auto dc = readln.chomp.to!int; auto rd3 = readln.split.map!(to!int); auto c3 = rd3[0], c2 = rd3[1], c0 = rd3[2]; auto at = total(a3, a2, a0), maxA3 = at / 1000, maxA2 = at / 100; auto bt = total(b3, b2, b0); auto ct = total(c3, c2, c0); auto memo = new int[][][](at + 1, maxA3 + 1, maxA2 + 1); memo[total(a3, a2, a0)][a3][a2] = 1; void pay(int d, int i, int j, int k, int et, int e3, int e2) { if (canPay(d, i, j, k)) { auto i2 = i - d; auto jp = min(d / 1000, j); auto j2 = j - jp; auto k2 = k - min((d - jp * 1000) / 100, k); memo[i2 + et][j2 + e3][k2 + e2] = max(memo[i2 + et][j2 + e3][k2 + e2], memo[i][j][k] + 1); } } foreach_reverse (i; 1..at + 1) foreach (j; 0..maxA3 + 1) foreach (k; 0..maxA2 + 1) if (memo[i][j][k]) { pay(db, i, j, k, bt, b3, b2); pay(dc, i, j, k, ct, c3, c2); } auto maxC = 0; foreach (i; 1..at + 1) foreach (j; 0..maxA3 + 1) foreach (k; 0..maxA2 + 1) maxC = max(maxC, memo[i][j][k]); writeln(maxC - 1); } int total(int a3, int a2, int a0) { return a3 * 1000 + a2 * 100 + a0; } bool canPay(int d, int i, int j, int k) { auto a3 = j, a2 = k, a0 = i - j * 1000 - k * 100; if (d > i) return false; if (d % 100 > a0) return false; a0 -= d % 100; a2 += a0 / 100; d /= 100; if (d % 10 > a2) return false; return true; }