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 proc check(i:int,bs:seq[tuple[vol:int,use:bool]]):void = if i == A.len(): echo bs.mapIt(it.use.int).sum() quit() 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))) echo -1