题解 | 搬水果
搬水果
https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a
#include<iostream> #include<queue> using namespace std; int main() { priority_queue<int,vector<int>,greater<int>>heap;//小根堆 int n; while(cin>>n){ if(n==0)break; for(int i=1;i<=n;i++){ int x; cin>>x; heap.push(x); } int res = 0; while(!heap.empty()){ int x=heap.top(); heap.pop(); int y=0; if(!heap.empty()){ y=heap.top(); heap.pop(); } else break; heap.push(x+y); res+=x+y; } cout<<res<<endl; } return 0; }