题目链接:Acwing_148合并果子
AC代码:
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
priority_queue<int, vector<int>, greater<int>> heap;
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
heap.push(x);
}
int res = 0;
for (int i = 0; i < heap.size(); i++)
{
int a = heap.top();
heap.pop();
int b = heap.top();
heap.pop();
res += a + b;
heap.push(a + b);
}
printf("%d", res);
system("pause");
return 0;
}