-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13975.cpp
More file actions
57 lines (44 loc) ยท 862 Bytes
/
13975.cpp
File metadata and controls
57 lines (44 loc) ยท 862 Bytes
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
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"
#define MAX 1e8
struct coordinate {
int x;
int y;
};
int dx[] = {0, 1, -1, 0, 1, -1, -1, 1};
int dy[] = {1, 0, 0, -1, -1, 1, -1, 1};
int T, K;
priority_queue<ll, vector<ll>, greater<ll>> pq;
void solve() {
ll result = 0;
while(pq.size() > 1) {
ll a = pq.top();
pq.pop();
ll b = pq.top();
pq.pop();
result += a + b;
pq.push(a + b);
}
cout << result << endl;
}
void input() {
cin >> T;
while(T--) {
cin >> K;
pq = {};
for(int i = 0; i < K; i++) {
int a;
cin >> a;
pq.push(a);
}
solve();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
input();
}