使用STL中的priorityqueue
```cpp
#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <algorithm>
int main(int argc, char const *argv[]) {
std::priority_queue<int, std::vector<int>, std::greater<int> > q;
int n,work=0;std::cin >> n;
for(int i=0;i<n;i++){
int tmp;std::cin >> tmp;
q.push(tmp);
}
while(q.size()>1){
int a=q.top();q.pop();
int b=q.top();q.pop();
q.push(a+b);
work+=(a+b);
}
std::cout << work << '\n';
return 0;
}
```
by hehelego @ 2017-07-31 17:51:35
%%%%%%%dalao
by Niscet @ 2017-07-31 19:17:05
求大神说下每个函数的作用吧我懒得看。。。
by 高清重置版hy @ 2017-08-03 15:18:27
```cpp
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int main()
{
int n, s = 0, t, a, b;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> t;
q.push(t);
}
for(int i = 1; i <= n - 1; i++)
{
a = q.top();
q.pop();
b = q.top();
q.pop();
s = s + a + b;
q.push(a + b);
}
cout << s << endl;
system("pause");
return 0;
}
```
by 仍然 @ 2017-08-05 15:05:45