import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.bitmanip; // BitArray void main() { auto n = readln.chomp.to!size_t; auto ai = readln.split.to!(int[]); auto m = readln.chomp.to!size_t; auto bi = readln.split.to!(int[]); bi.sort!"a > b"; auto ci = new int[](1 << n); foreach (p; 0..(1 << n)) ci[p] = ai.indexed(p.bitsSet).sum; auto calc() { auto dp1 = BitArray(); dp1.length(1 << n); dp1[0] = true; foreach (k; 0..m) { auto dp2 = BitArray(); dp2.length(1 << n); foreach (p1; 0..(1 << n)) if (dp1[p1]) foreach (p2; 0..(1 << n)) if ((p1 & p2) == 0 && ci[p2] <= bi[k]) dp2[p1 | p2] = true; if (dp2[1 << n]) return k.to!int + 1; dp1 = dp2; } return -1; } writeln(calc); }