Point:
优先队列定义
小根堆 priority_queue<int, vector<int>, greater<int>> heap;
大根堆 priority_queue<int, vector<int>, less<int>> heap;
基本操作
heap.push(); // 入队
heap.pop(); // 出队
heap.size(); // 元素个数
heap.top(); // 队首
heap.empty(); // 判空
C++ 代码
#include <iostream>
#include <queue>
using namespace std;
#define int long long
#define p_qi priority_queue<int, vector<int>, less<int>>
signed main()
{
int n;
cin >> n;
p_qi heap;
for(int i = 1; i <= n; ++ i)
{
int temp;
scanf("%lld",&temp);
heap.push(temp);
}
int res = 0;
while(heap.size() > 1)
{
int a = heap.top(); heap.pop();
int b = heap.top(); heap.pop();
res += a + b;
heap.push(a + b);
}
printf("%lld\n", res);
return 0;
}