import std.stdio; import std.array; import std.string; import std.conv; import std.algorithm; import std.typecons; import std.range; import std.random; import std.math; import std.container; import std.numeric; int T, N; int[] C, V; int[long] mem; int rec(long b, int t, int acm) { if (b in mem) return mem[b]; mem[b] = acm; foreach (i; iota(N)) { auto b2 = b / 10^^i % 10; if (t > C[i] && (V[i] >> b2) > 0 && b2 < 9) mem[b] = max(mem[b], rec(b+(b2+1)*(10^^i), t-C[i], acm+(V[i]>>b2))); } return mem[b]; } void main() { T = readln.chomp.to!int; N = readln.chomp.to!int; C = readln.split.map!(to!int).array; V = readln.split.map!(to!int).array; writeln(rec(0L, T, 0)); }