fork download
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. uint32_t n;
  8. cin >> n;
  9. priority_queue<uint64_t, vector<uint64_t>, greater<void>> q;
  10. for (uint32_t i = 0; i < n; ++i) {
  11. uint32_t number;
  12. cin >> number;
  13. q.push(number);
  14. }
  15. uint64_t cost = 0;
  16. while (q.size() >= 2) {
  17. uint64_t const first = q.top();
  18. q.pop();
  19. uint64_t const second = q.top();
  20. q.pop();
  21. auto const sum = first + second;
  22. cost += sum;
  23. q.push(sum);
  24. }
  25. cout << cost << endl;
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 4332KB
stdin
5
1 1 1 1 1
stdout
12