#include<iostream>
#include<string>
#include<queue>
using namespace std;
int main(){
int n, m, x, y;
while(cin>>n){
priority_queue<int,vector<int>,greater<int> > q;
for(int i=0;i<n;i++){
cin>>m;
q.push(m);
}
long long sum = 0;
if(q.size()==1){//只有一段
sum+=q.top();
q.pop();
}
while(q.size()>1){
x = q.top();
q.pop();
y = q.top();
q.pop();
sum+=(x+y);
q.push(x+y);
}
cout<<sum<<endl;
}
}
poj3253 Fence Repair 哈夫曼树
最新推荐文章于 2021-07-26 11:36:28 发布