Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. priority_queue < int > q;
  7.  
  8. int main()
  9. {
  10. int i, n, x, y, cost;
  11. cin >> n;
  12. for(i=1; i<=n; i++)
  13. {
  14. cin >> x;
  15. q.push(-x);
  16. }
  17. cost = 0;
  18. for(i=1; i<n; i++)
  19. {
  20. x=-q.top();
  21. q.pop();
  22. y=-q.top();
  23. q.pop();
  24. cost+=x+y;
  25. q.push(-(x+y));
  26. }
  27. cout << cost;
  28.  
  29. return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement