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.conv, std.stdio, std.typecons; struct used { size_t boxes; int volume; int opCmp(used rhs) { if (boxes < rhs.boxes) return -1; else if (boxes > rhs.boxes) return 1; else if (volume < rhs.volume) return -1; else if (volume > rhs.volume) return 1; else return 0; } } void main() { auto n = readln.chomp.to!size_t; auto ai = readln.split.map!(to!int).array; auto m = readln.chomp.to!size_t; auto bi = readln.split.map!(to!int).array; bi.sort!("a > b"); auto memo = new Nullable!used[](1 << n); memo[0] = used(0, 0); foreach (size_t i; 1..1 << n) { Nullable!used minU; foreach (size_t j; 0..n) { if (bt(&i, j)) { auto a = ai[j]; auto k = i; btc(&k, j); auto u = memo[k]; if (u.isNull) continue; if (bi[u.boxes] - u.volume < a) { if (u.boxes == n - 1 || bi[u.boxes + 1] < a) continue; u.boxes += 1; u.volume = a; } else { u.volume += a; } if (minU.isNull) minU = u; else minU = min(minU, u); } } memo[i] = minU; } auto u = memo[$ - 1]; if (u.isNull) writeln(-1); else writeln(u.boxes + 1); }