C++ 代码
#include <bits/stdc++.h>
using namespace std;
int n,x,s=0,a,b;
int main()
{
cin>>n;
priority_queue<int, vector<int>, greater<int>> heap;
while (n--)
{
cin>>x;
heap.push(x);
}
while (heap.size() > 1)
{
a = heap.top();
heap.pop();
b = heap.top();
heap.pop();
s+=a+b;
heap.push(a+b);
}
cout<<s;
return 0;
}