from collections import * from heapq import * N = int(input()) A = list(map(int, input().split())) Q = int(input()) X = list(map(int, input().split())) h = [-e for e in A] heapify(h) c = Counter(A) r = sum(A) for x in X: while -h[0] >= x: v = -heappop(h) r -= v * c[v] c[v] = 0 v %= x if c[v] == 0: heappush(h, -v) c[v] += 1 r += v print(r)