原题:https://www.luogu.com.cn/problem/P1090
这道题虽然说是提高组,但只要运用一定的技巧,就一点儿也不难了。STL中,有一个优先队列(priority_queue),一般用于解决像这样一类的贪心问题。代码如下:
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;//如果要它从大到小出队列,直接定义就可以了,不需要在里面加上vector<int>,greater<int>
int n,g,ans,x,y;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> g;
q.push(g);
}
for(int i = 1; i < n; i++){
x = q.top();
q.pop();
y = q.top();
q.pop();
ans = ans + x + y;
q.push(x+y);
}
cout << ans;
return 0;
}