题解 | #搬水果#哈夫曼树
搬水果
https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main() {
int n;
while (cin >> n) {
if (n == 0) break;
int wpl = 0;
priority_queue<int, vector<int>, greater<int> > heap;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
heap.push(x);
}
while (heap.size() > 1) {
int a = heap.top();
heap.pop();
int b = heap.top();
heap.pop();
int c = a + b;
wpl += c;
heap.push(c);
}
cout << wpl << endl;
}
return 0;
}
王道考研机试 文章被收录于专栏
包含考研机试打卡表题目
查看11道真题和解析
