题解 | #搬水果#做法同哈夫曼树#优先队列

搬水果

https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a

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

int main() {
    int n,m;
    priority_queue<int> Myqueue;
    scanf("%d",&n);
    int wpl=0;
    for(int i=0;i<n;i++){
        scanf("%d",&m);
        Myqueue.push(-1*m);
    }
    while(Myqueue.size()>=2){
        int s1,s2;
        s1 = Myqueue.top();
        Myqueue.pop();
        s2 = Myqueue.top();
        Myqueue.pop();
        wpl+=(s1+s2);
        Myqueue.push(s1+s2);
    }
    printf("%d",-1*wpl);
    return 0;
}

全部评论

相关推荐

09-25 11:39
已编辑
北京航空航天大学 Java
我的代码出BUG了:@美团@腾讯@字节跳动@阿里巴巴。你们好好看看吧,你们就挂我吧,到时候被人家鸽穿还得录取我
点赞 评论 收藏
分享
大专境巅峰电子狗:头一次看到这种简历,学术与技术学习,直接用技能概括就好了呀,实习经历要写丰富一点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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