#include #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define all(v) v.begin(), v.end() #define lint long long auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #define debug(...)\ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha; template std::istream& operator>> (std::istream& is, std::vector& v) { for (auto & x : v) is >> x; return is; } template std::ostream& operator<< (std::ostream& os, const std::vector& v) { auto n = v.size(); os << "{"; for (size_t i = 0; i < n; i++) {os << (i > 0 ? "," : "") << v.at(i);} return os << "}"; } template std::ostream& operator<< (std::ostream& os, const std::map& v) { for (auto p : v) { os << p; } return os << "}"; } template std::ostream& operator<< (std::ostream& os, const std::pair& pair) { return os << "(" << pair.first << "," << pair.second << ")"; } template std::istream& operator>> (std::iostream& is, std::pair& pair) { return is >> pair.first >> pair.second; } template class run_length_encoding { std::vector> rle; public: run_length_encoding( std::vector input ) : rle(0) { int cnt = 0; for (auto it = input.begin(); it != input.end(); it++) { auto jt = next(it); cnt++; if (jt == input.end() || *it != *jt) { rle.emplace_back(cnt, *it); cnt = 0; } } } auto const& code () const {return rle;} }; int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n; std::cin >> n; std::vector< int > a(n); std::cin >> a; int q; std::cin >> q; auto now = std::accumulate(all(a), 0LL); std::sort(all(a)); auto rle = run_length_encoding< int >(a).code(); std::map< int, int, std::greater< int > > map; for (auto p : rle) { map[p.second] = p.first; } loop(q) { int x; std::cin >> x; while (map.begin() -> first >= x) { auto p = *map.begin(); map.erase(map.begin()); int cnt, key; std::tie(key, cnt) = p; auto q = key / x; auto r = key % x; now -= q * cnt * x; auto new_cnt = cnt; auto new_key = r; auto it = map.find(new_key); if (it != map.end()) { new_cnt += it->second; map.erase(it); } map.insert({new_key, new_cnt}); } std::cout << now << std::endl; } return 0; }