题解 | 搬水果

#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,直接算即可

全部评论

相关推荐

大佬们考的怎么样?看样子不能只刷acm编程题了,机器学习题也要刷下。😭
牛客561236133号:绷不住了,皮什么玩意来着
投递美团等公司10个岗位 >
点赞 评论 收藏
分享
02-22 21:16
已编辑
门头沟学院 运营
牛客928043833号:离了你谁还拿我当个宝
点赞 评论 收藏
分享
双非本科小鼠:27兄弟,不应该还在享受校园吗哈哈😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务