Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- typedef long long ll;
- ll sum[100005];
- int main() {
- int n, p;
- cin >> n >> p;
- multiset<int> ms;
- ll zbir = 0;
- for(int i = 0; i < n; i++) {
- int x;
- cin >> x;
- zbir += x;
- ms.insert(x);
- }
- sum[0] = zbir;
- for(int i = 0; i < n - 1; i++) {
- int max_element = *ms.rbegin();
- int min_element = *ms.begin();
- ms.erase(ms.find(max_element));
- ms.erase(ms.find(min_element));
- zbir -= (max_element + min_element);
- zbir += (max_element - min_element);
- ms.insert(max_element - min_element);
- sum[i + 1] = zbir;
- }
- for(int i = 0; i < p; i++) {
- int x;
- cin >> x;
- cout << sum[x] << " ";
- }
- return 0;
- }
- // 2 1 3 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement