Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int main() {
- ios_base::sync_with_stdio(false);
- int n, p;
- cin >> n >> p;
- vector<int> v(n);
- multiset<int> ms;
- ll sum = 0;
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- ms.insert(v[i]);
- sum += v[i];
- }
- vector<ll> res;
- res.push_back(sum);
- for(int i = 0; i < n - 1; i++) {
- int smallest_number = *ms.begin();
- int biggest_number = *ms.rbegin();
- sum -= (smallest_number + biggest_number);
- sum += biggest_number - smallest_number;
- ms.erase(ms.find(smallest_number));
- ms.erase(ms.find(biggest_number));
- ms.insert(biggest_number - smallest_number);
- res.push_back(sum);
- }
- for(int i = 0; i < p; i++) {
- int x;
- cin >> x;
- cout << res[x] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement