题解 | #搬水果#
搬水果
https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { int n; priority_queue<int> mq; while(scanf("%d",&n)!=EOF){ if(n==0){//n==0退出输入 break; } while(n--){ int m; scanf("%d",&m); mq.push(-m);//巧法:优先队列默认为大根堆,加入时前加个负号,将大根堆改成小根堆 } int res =0; while(mq.size()>1){ int leaf1 = mq.top();//haffuman的权值最小的两个之一 mq.pop(); int leaf2 = mq.top();//haffuman的权值最小的两个之一 mq.pop(); res = res + leaf1 + leaf2; mq.push(leaf1+leaf2); } printf("%d\n",-res);//加个负号输出 } }