#include #include #include using namespace std; typedef long long ll; typedef pair P; ll X[100000]; priority_queue

que; int main() { ll N; ll sum = 0; cin >> N; for (int i = 0; i < N; i++) { ll A; cin >> A; sum += A; que.push(P(A, i)); } int Q; cin >> Q; for (int i = 0; i < Q; i++) { int X; cin >> X; while (true) { P top = que.top(); if (top.first >= X) { que.pop(); sum -= top.first; sum += top.first % X; que.push(P(top.first % X, top.second)); } else { break; } } cout << sum << endl; } }