#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; auto range(int n) { return views::iota(0, n); } template ostream& operator<<(ostream& os, const pair& p){ return os << "{" << p.first << ", " << p.second << "}"; } template ostream& operator<<(ostream& os, const vector& obj) { os << "{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const set& obj) { os << "set{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } template ostream& operator<<(ostream& os, const map& obj) { os << "map{"; for (const auto& e : obj) os << e << ", "; return os << "}"; } #ifdef ONLINE_JUDGE #define dump(expr) ; #else #define dump(expr) { cerr << "\033[33m#L" << __LINE__ << ": " << expr << "\033[39m" << endl; } #endif void solve() { int n; cin >> n; multiset st; for (int i : range(n)) { ll x; cin >> x; st.insert(x); } ll tot = accumulate(st.begin(), st.end(), 0LL); int q; cin >> q; while (q--) { ll m; cin >> m; auto it0 = st.lower_bound(m); vector bk; dump("QUERY " << m) for (auto it = it0; it != st.end(); ++it) { dump(*it); ll y = *it % m; tot -= (*it) - y; bk.push_back(y); } st.erase(it0, st.end()); for (ll b : bk) st.insert(b); cout << tot << endl; } } int main() { cout << fixed << setprecision(12); solve(); }