Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <set>
- using namespace std;
- long long sumi[100001];
- int main() {
- int n, p;
- cin >> n >> p;
- vector<int> v;
- multiset<int> ms;
- long long sum = 0;
- for(int i = 0; i < n; i++) {
- int x;
- cin >> x;
- v.push_back(x);
- ms.insert(x);
- sum += x;
- }
- sumi[0] = sum;
- for(int i = 1; i < n; i++) {
- int najmal_element = *ms.begin();
- int najgolem_element = *ms.rbegin();
- int razlika = najgolem_element - najmal_element;
- ms.erase(ms.find(najmal_element));
- ms.erase(ms.find(najgolem_element));
- ms.insert(razlika);
- sum -= (najmal_element + najgolem_element);
- sum += razlika;
- sumi[i] = sum;
- }
- for(int i = 0; i < p; i++) {
- int x;
- cin >> x;
- cout << sumi[x] << endl;
- }
- return 0;
- }
- // 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement