Naxocist

AddAll

Mar 31st, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endll '\n'
  3. using namespace std;
  4.  
  5.  
  6. int main() {
  7.     long long n, cost=0; cin >> n;
  8.  
  9.     vector<int> v;
  10.     for(int i=0; i<n; ++i){
  11.         int x; scanf("%d", &x);
  12.         v.push_back(x);
  13.     }
  14.  
  15.     priority_queue<int, vector<int>, greater<int>> pq(begin(v), end(v));
  16.  
  17.     while(1){
  18.  
  19.         int a = pq.top();
  20.         pq.pop();
  21.         int b = pq.top();
  22.         pq.pop();
  23.         int k = a + b;
  24.         cost += k;
  25.         if(pq.empty()) break;
  26.         pq.push(k);
  27.     }
  28.  
  29.     cout << cost;
  30.     // cout << k + sum;
  31.  
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment