eolymp
bolt
Try our new interface for solving problems
Problems

Add All

published at 3/25/24, 7:17:23 pm

include <iostream>

include <queue>

using namespace std;

int main() { int n; scanf("%d", &n);

priority_queue<long long, vector<long long>, greater<long long>> pq;

long long num, res = 0;
for (int i = 0; i < n; i++) {
    scanf("%lld", &num);
    pq.push(num);
}
while (pq.size() > 1) {
    long long a = pq.top(); pq.pop();
    long long b = pq.top(); pq.pop();
    pq.push(a + b);
    res += a + b;
}
cout << res << endl;
return 0;

}