#include<bits/stdc++.h>
using namespace std;
int bb[1000];
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
int n,i,lc,m;
scanf("%d",&n);
memset(bb,0,sizeof(bb));
for(i=0; i<n; i++)
{
scanf("%d",&m);
q.push(m);
}
lc=0;
while(!q.empty())
{
int num=q.top();
q.pop();
if(!q.empty())
{
int m=q.top();
q.pop();
int t=num+m;
q.push(t);
lc+=t;
}
}
printf("%d\n",lc);
return 0;
}
树-堆结构练习——合并果子之哈夫曼树
最新推荐文章于 2023-12-06 17:14:58 发布
本文介绍了一种使用优先队列实现的简单合并算法。该算法通过不断从两个最小元素中选择并合并来减少总成本,适用于需要频繁合并操作的场景。

7289

被折叠的 条评论
为什么被折叠?



