-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCTDL044.cpp
44 lines (43 loc) · 919 Bytes
/
SCTDL044.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
/**
* @file SCTDL044.cpp
* @author Long ([email protected])
* @brief Rope binding implementation
* Given the value of n ropes.
* The cost that needs to bind 2 ropes together
* is sum of those 2 ropes's value. Find the minimum cost to bind all ropes.
* @version 0.1
* @date 2023-04-13
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, x;
cin >> n;
long long s = 0;
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 0; i < n; i++)
{
cin >> x;
q.push(x);
}
while (q.size() > 1)
{
int s1 = q.top();
q.pop();
int s2 = q.top();
q.pop();
int k = s1 + s2;
s += k;
q.push(k);
}
cout << s << "\n";
}
}