https://www.luogu.com.cn/problem/P1090
优先队列
小根堆
priority_queue<int,vector<int>,greater<int> >q;
大根堆
priority_queue <int > q;
#include<bits/stdc++.h> using namespace std; //priority_queue <int > q; 这是大根堆 priority_queue<int,vector<int>,greater<int> >q; int main() { int n; cin>>n; int temp; int a1,a2,a3; int ans=0; for(int i=0;i<n;i++){ scanf("%d",&temp); q.push(temp); } while(q.size()>1){ a1=q.top(); q.pop(); a2=q.top(); q.pop(); a3=a1+a2; q.push(a3); ans+=a3; } cout<<ans; return 0; }