import sequtils,strutils,strscans,algorithm,math,future,macros template get*():string = stdin.readLine() #.strip() import times let N = get().parseInt() A = get().split().map(parseInt).sorted(cmp,Descending) # toy <20 M = get().parseInt() B = get().split().map(parseInt).sorted(cmp,Descending) # box <20 var ans = newSeq[int]() let start = cpuTime() proc check(i:int,bs:seq[tuple[vol:int,use:bool]]):void = if i == A.len(): ans &= bs.mapIt(it.use.int).sum() if cpuTime() - start > 0.003: echo ans.min() quit() return for j,b in bs: if b.vol < A[i] : if not b.use : break else: continue #! var nbs = bs nbs[j].vol -= A[i] nbs[j].use = true check(i+1,nbs) return check(0,B.mapIt((it,false))) if ans.len() > 0: echo ans.min() else: echo -1