fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. vector<int> v={1,2,3} ;//{6,4,8};
  6.  
  7. priority_queue<int,vector<int>,greater<int>> pq; //min priority queue
  8. int ans=0;
  9. for(auto x:v)
  10. pq.push(x);
  11.  
  12. while(pq.size()>1)
  13. {
  14. int e1=pq.top();
  15. pq.pop();
  16. int e2=pq.top();
  17. pq.pop();
  18.  
  19. ans+=e1+e2;
  20.  
  21. cout<<e1<<" "<<e2<<endl;
  22.  
  23. pq.push(e1+e2);
  24. }
  25. cout<<ans<<endl;
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
1 2
3 3
9