#include<iostream>
#include<queue>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
priority_queue<int,vector<int>,greater<int>> heap;
for(int i=1;i<=n;i++){
int x;
cin>>x;
heap.push(x);
}
int res=0;
while(heap.size()>1){
int a=heap.top();
heap.pop();
int b=heap.top();
heap.pop();
res = res+a+b;
heap.push(a+b);
}
cout<<res<<endl;
}
贪心求解合并果子问题(c++实现)
最新推荐文章于 2024-08-30 08:07:50 发布