keymasterviriya1150

addall83

Jul 14th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include<iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int N,x;
  10.      while(1)
  11.      {
  12.         priority_queue<int,vector<int>,greater<int> > q;
  13.         int sumcost=0;
  14.         cin >> N;
  15.         if(N==0)
  16.             break;
  17.         for(int i=0;i<N;i++)
  18.         {
  19.             cin >> x;
  20.             q.push(x);
  21.         }
  22.         for(int i=0;i<N-1;i++)
  23.         {
  24.             int temp=q.top();
  25.        
  26.             q.pop();
  27.             int temp2=q.top();
  28.      
  29.             q.pop();
  30.             int cost=temp+temp2;
  31.        
  32.             q.push(cost);
  33.             sumcost=sumcost+cost;
  34.        
  35.      
  36.         }
  37.         cout << sumcost << endl;
  38.        
  39.      }
  40.     exit(EXIT_SUCCESS);
  41. }
Add Comment
Please, Sign In to add comment