题解 | #搬水果#
搬水果
https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a
#include <iostream> #include <queue> #include <vector> using namespace std; //哈夫曼树应用 int main() { int n; while (cin >> n && n != 0) { priority_queue<int, vector<int>, greater<int>> q; while(n --){ int x; cin >> x; q.push(x); } int sum = 0; while(q.size() > 1){ int a = q.top(); q.pop(); int b = q.top(); q.pop(); q.push(a + b); sum += (a + b); } cout << sum << endl; } return 0; } // 64 位输出请用 printf("%lld")