F.cpp 571 B

12345678910111213141516171819202122232425262728
  1. //
  2. // Created by jal on 2019-05-02.
  3. //
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. int main(){
  7. int n;
  8. while(cin >> n){
  9. priority_queue<int, vector<int>, greater<int>>Q;
  10. for(int i = 0; i < n; i++){
  11. int x;
  12. cin >> x;
  13. Q.push(x);
  14. }
  15. int cnt = 0;
  16. for(int i = 1; i < n; i++){
  17. int a = Q.top();
  18. Q.pop();
  19. int b = Q.top();
  20. Q.pop();
  21. int c = a + b;
  22. cnt += c;
  23. Q.push(c);
  24. }
  25. cout << cnt << endl;
  26. }
  27. }