借助stl的priority_queue实现哈夫曼树的思想。换成__int64后AC
#include<iostream>
#include<queue>
using namespace std;
int main(){
__int64 n,k;
cin>>n;
priority_queue<__int64,vector<__int64>,greater<__int64> > que;
for(__int64 i=0;i<n;++i){
cin>>k;
que.push(k);
}
__int64 cost=0;
while(que.size()>1){
__int64 min_a,min_b,amount;
min_a=que.top();que.pop();
min_b=que.top();que.pop();
amount=min_a+min_b;
cost+=amount;
que.push(amount);
}
cout<<cost;
return 0;
}