Advertisement
Josif_tepe

Untitled

Mar 22nd, 2024
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <set>
  5. using namespace std;
  6. typedef long long ll;
  7. ll sum[100005];
  8.  
  9.  
  10. int main() {
  11.     int n, p;
  12.     cin >> n >> p;
  13.     multiset<int> ms;
  14.     ll zbir = 0;
  15.     for(int i = 0; i < n; i++) {
  16.         int x;
  17.         cin >> x;
  18.         zbir += x;
  19.         ms.insert(x);
  20.     }
  21.     sum[0] = zbir;
  22.     for(int i = 0; i < n - 1; i++) {
  23.         int max_element = *ms.rbegin();
  24.         int min_element = *ms.begin();
  25.        
  26.         ms.erase(ms.find(max_element));
  27.         ms.erase(ms.find(min_element));
  28.         zbir -= (max_element + min_element);
  29.         zbir += (max_element - min_element);
  30.         ms.insert(max_element - min_element);
  31.         sum[i + 1] = zbir;
  32.        
  33.     }
  34.    
  35.     for(int i = 0; i < p; i++) {
  36.         int x;
  37.         cin >> x;
  38.         cout << sum[x] << " ";
  39.     }
  40.     return 0;
  41. }
  42. //   2 1 3 3
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement