#include #include using namespace std; int main() { int N; scanf("%d", &N); long long sum = 0; multiset ms; for (int i = 0; i < N; i++) { int x; scanf("%d", &x); ms.insert(x); sum += x; } int Q; scanf("%d", &Q); for (int i = 0; i < Q; i++) { int m; scanf("%d", &m); multiset::iterator it; while ((it = ms.lower_bound(m)) != ms.end()) { int val = *it; sum += (val % m) - val; ms.erase(it); ms.insert(val % m); } printf("%lld\n", sum); } return 0; }