Advertisement
Josif_tepe

Untitled

Apr 4th, 2023
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. typedef long long ll;
  6. int main() {
  7.     ios_base::sync_with_stdio(false);
  8.     int n, p;
  9.     cin >> n >> p;
  10.     vector<int> v(n);
  11.     multiset<int> ms;
  12.     ll sum = 0;
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> v[i];
  15.         ms.insert(v[i]);
  16.         sum += v[i];
  17.     }
  18.     vector<ll> res;
  19.     res.push_back(sum);
  20.     for(int i = 0; i < n - 1; i++) {
  21.         int smallest_number = *ms.begin();
  22.         int biggest_number = *ms.rbegin();
  23.        
  24.         sum -= (smallest_number + biggest_number);
  25.         sum += biggest_number - smallest_number;
  26.         ms.erase(ms.find(smallest_number));
  27.         ms.erase(ms.find(biggest_number));
  28.        
  29.         ms.insert(biggest_number - smallest_number);
  30.         res.push_back(sum);
  31.     }
  32.     for(int i = 0; i < p; i++) {
  33.         int x;
  34.         cin >> x;
  35.         cout << res[x] << endl;
  36.     }
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement