题解 | #搬水果#做法同哈夫曼树#优先队列
搬水果
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; }