- 思路:动态的获取队列中最小的两个 然后加在一起呗——要不每次都搬多的得累死
优先队列!
STL里的优先队列 : priority_queue
- 长什么样呢?
priority_queue<int>q;
//从小到大 小根堆
priority_queue<int,vector<int>,greater<int> >q;
//从大到小 大根堆
priority_queue<int,vector<int>,less<int> >q;
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define INF 0x3f3f3f3f
using namespace std;
const int N = 100010;
int n, x,num,ans;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
priority_queue<int, vector<int>,greater<int>> pq;
cin>>n;
while(n--){
cin>>num;
pq.push(num);
}
while(pq.size()>1){
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
pq.push(a+b);
ans =ans+ a+b;
}
cout<<ans;
}