1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <bits/stdc++.h> using namespace std; int n, res; int main() { priority_queue<int, vector<int>, greater<int>> p; scanf("%d", &n); while(n--) { int a; scanf("%d", &a); p.push(a); } while(p.size() > 1) { int a = p.top(); p.pop(); int b = p.top(); p.pop(); res += (a + b); p.push(a+b); } printf("%d", res); return 0; }
|