-
Notifications
You must be signed in to change notification settings - Fork 1
/
1161.cpp
48 lines (47 loc) · 868 Bytes
/
1161.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 200005;
void solve() {
int n,u;
cin >> u >> n;
priority_queue<ll,vector<ll>,greater<ll>>pq;
ll ans = 0;
f(n) {
ll x;
cin >> x;
pq.push(x);
}
while (pq.size() > 1) {
ll x = pq.top();
pq.pop();
ll y = pq.top();
pq.pop();
pq.push(x+y);
ans += x + y;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c = 0;
int t;
if (!c) {
t = 1;
}
else {
cin >> t;
}
while (t--) {
solve();
}
}