题意
传送门 P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G
题解
体力耗费值对应一个二叉 H u f f m a n Huffman Huffman树问题,果子堆的重量对应叶子节点的权值,参与合并的次数即叶子到根的距离。
#include <bits/stdc++.h>
using namespace std;
int N;
int main()
{
scanf("%d", &N);
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 0, a; i < N; ++i)
scanf("%d", &a), q.push(a);
int res = 0;
while (q.size() > 1)
{
int x = q.top();
q.pop();
int y = q.top();
q.pop();
res += x + y;
q.push(x + y);
}
printf("%d\n", res);
return 0;
}