优先队列的算法(快读快写模板)
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c;
unsigned long long res = 0;
priority_queue < int, vector < int >, greater < int > > tmp;
inline int read() {
char c = getchar();
int x = 0, s = 1;
while (c < '0' || c > '9') {
if (c == '-') s = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * s;
}
void write(unsigned long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int main() {
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n = read();
for (int i = 0; i < n; i++) {
a = read();
tmp.push(a);
}
while (tmp.size() > 1) {
b = tmp.top();
tmp.pop();
c = tmp.top();
tmp.pop();
res += b + c;
tmp.push(b + c);
}
write(res);
return 0;
}