import sequtils,strutils,strscans,algorithm,math,future,macros template get*():string = stdin.readLine() #.strip() 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]() proc check(i:int,bs:seq[tuple[vol:int,use:bool]]):void = if i == A.len(): ans &= bs.mapIt(it.use.int).sum() if ans.len() >= 1000 : 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