题解 | 搬水果

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        if(n==0)break;
        priority_queue<int,vector<int>,greater<int>>q;
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            q.push(x);
        }
        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;
    }
}

本题看似复杂,其实本质上就是二叉哈夫曼树的构造过程,求WSL,直接算即可

全部评论

相关推荐

不愿透露姓名的神秘牛友
10-11 19:51
已编辑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务