import sequtils,strutils,math type priorityQue[I:static[int];T:tuple] = array[I + 1,T] item = tuple[cost: int] proc push[T](A:var priorityQue; b: T;)= const key = 0 A[0][0] += 1 A[A[0][0]] = b # A[0][0]はsize、最大のIndex var index = A[0][0] while index > 1 and A[index][key] > A[index div 2][key]: (A[index],A[index div 2]) = (A[index div 2],A[index]) index = index div 2 proc pop(A: var priorityQue):item = const key = 0 result = A[1] A[1] = A[A[0][0]] var a : item A[A[0][0]] = a A[0][0] -= 1 var index = 1 while index * 2 <= A[0][0]: if index * 2 + 1<= A[0][0]: if A[index][key] < A[index * 2][key] and A[index * 2 + 1][key] <= A[index * 2][key]: (A[index],A[index * 2]) = (A[index * 2],A[index]) index = index * 2 elif A[index][key] < A[index * 2 + 1][key]: (A[index],A[index * 2 + 1]) = (A[index * 2 + 1],A[index]) index = index * 2 + 1 else: break elif index * 2 <= A[0][0]: if A[index][key] < A[index * 2][key]: (A[index],A[index * 2]) = (A[index * 2],A[index]) break else: break proc size(A : priorityQue):int= return A[0][0] var N = stdin.readline.parseInt A = stdin.readline.split.map(parseInt) Q = stdin.readline.parseInt X = stdin.readline.split.map(parseInt) pq : priorityQue[200010, item] v = newSeq[item](0) S = sum(A) for i,a in A: var p : item = (a,) pq.push(p) for i,x in X: while pq.size > 0 and pq[1][0] >= x: var p = pq.pop S -= p.cost p.cost = p.cost mod x S += p.cost if p.cost > 0: pq.push(p) echo S