1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int N=1e5+10;
int a[N];
int main() {
int n;
cin>>n;
priority_queue<int,vector<int>,greater<int> > q;
for (int i=0;i<n;++i) cin>>a[i];
for (int i=0;i<n;++i) q.push(a[i]);
long long res=0;
while (q.size()>1) {
int _h1 = q.top();q.pop();
int _h2 = q.top();q.pop();
q.push(_h1+_h2);
res += _h1 + _h2;
}
cout << res;
}
|