#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> >Q;
int main()
{
int n;
while(cin>>n)
{
while(!Q.empty()) Q.pop();
int a;
for(int i=0;i<n;i++)
{
cin>>a;
Q.push(a);
}
int ans=0;
while(Q.size()>1)
{
int x=Q.top();
Q.pop();
int y=Q.top();
Q.pop();
ans+=x+y;
Q.push(x+y);
}
cout<<ans<<endl;
}
return 0;
}
哈夫曼树
最新推荐文章于 2023-04-18 18:41:12 发布