import sequtils,strutils,strscans,algorithm,math,future,macros import sets,tables,intsets template get*():string = stdin.readLine() #.strip() template `max=`*(x,y:typed):void = x = max(x,y) template optPow{`^`(2,n)}(n:int) : int = 1 shl n template If*(ex:untyped):untyped = (if not(ex) : continue) proc enumerate[T](arr:seq[T]): seq[tuple[i:int,val:T]] = result = @[]; for i,a in arr: result &= (i,a) let N = get().parseInt() A = get().split().map(parseInt).sorted(cmp,Descending) M = get().parseInt() B = get().split().map(parseInt).sorted(cmp,Descending) var state = newSeqWith(2^N,0) for n in 0 ..< 2^N: for ia in 0 ..< N: If: (n and 2^ia) == 2^ia state[n] += A[ia] # 各状態の体積の和 proc getOKState(b:int): IntSet = result = initIntSet() for i in state.enumerate().filterIt(b-it.val >= 0).mapIt(it.i): result.incl(i) var ans = B[0].getOKState() if 2^N-1 in ans: echo 1 quit() for i,b in B[1..^1]: var n_ans : IntSet n_ans.assign(ans) for x in b.getOKState().items: for y in ans.items: If: (x and y) == 0 n_ans.incl(x xor y) ans = n_ans if 2^N-1 in ans: echo i + 2 quit() echo -1