分析:
这就是一个优先队列的题目了。
每次优先取值最小的两个进行合并。
#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
typedef long long ll;
int n;
ll a[10010];
int main()
{
scanf("%d",&n);
priority_queue<ll,vector<ll>,greater<ll> > Q;
for(int i = 1; i <= n; i ++)
{
scanf("%lld",&a[i]);
Q.push(a[i]);
}
ll ans = 0;
while(Q.size() > 1)
{
ll x = Q.top();
Q.pop();
ll y = Q.top();
Q.pop();
ans += x + y;
Q.push(x + y);
}
printf("%lld\n",ans);
}