/* -*- coding: utf-8 -*- * * 885.cc: No.885 アマリクエリ - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; /* typedef */ typedef long long ll; typedef pair pii; template struct BIT { int n; T bits[MAX_N + 1]; BIT() {} void init(int _n) { n = _n; //memset(bits, 0, sizeof(bits)); } T sum(int x) { T s = 0; while (x > 0) { s += bits[x]; x -= (x & -x); } return s; } void add(int x, T v) { while (x <= n) { bits[x] += v; x += (x & -x); } } }; /* global variables */ BIT bit; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); bit.init(n); priority_queue q; for (int i = 1; i <= n; i++) { int ai; scanf("%d", &ai); q.push(pii(ai, i)); bit.add(i, ai); } int qn; scanf("%d", &qn); while (qn--) { int xi; scanf("%d", &xi); while (! q.empty() && q.top().first >= xi) { pii u = q.top(); q.pop(); int &ai = u.first, &i = u.second; int bi = ai % xi; if (bi > 0) q.push(pii(bi, i)); bit.add(i, bi - ai); } printf("%lld\n", bit.sum(n)); } return 0; }