题解 | #搬水果#
搬水果
http://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a
采用优先队列解决问题
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
int N;
while (cin >> N) {
if (N == 0) break;
priority_queue<int, vector<int>, greater<int>> pq;
while (N--) {
int val;
cin >> val;
pq.push(val);
}
int result = 0;
while (pq.size() > 1) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
pq.push(a+b);
result += a + b;
}
cout << result << endl;
}
return 0;
}