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