fork download
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int main() {
  6. priority_queue <long long> nums;
  7. long long n;
  8. cin >> n;
  9. long long number = 0;
  10. for (int i = 0; i < n; i++) {
  11. cin >> number;
  12. nums.push(-number);
  13. }
  14. long long min_sum = 0;
  15. long long a = 0;
  16. long long b = 0;
  17. while ( !nums.empty() )
  18. {
  19. a = nums.top();
  20. nums.pop();
  21. if (nums.empty()) break;
  22. b = nums.top();
  23. nums.pop();
  24. min_sum -= a+b;
  25. nums.emplace(a+b);
  26.  
  27. }
  28. cout << " " << min_sum;
  29. return 0;
  30. }
Success #stdin #stdout 0s 4372KB
stdin
6
1 2 3 4 5 6
stdout
 51