forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.cpp
More file actions
32 lines (26 loc) Β· 680 Bytes
/
4.cpp
File metadata and controls
32 lines (26 loc) Β· 680 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
#include <bits/stdc++.h>
using namespace std;
int n, result;
priority_queue<int> pq;
int main(void) {
cin >> n;
// ν(Heap)μ μ΄κΈ° μΉ΄λ λ¬Άμμ λͺ¨λ μ½μ
for (int i = 0; i < n; i++) {
int x;
cin >> x;
pq.push(-x);
}
// ν(Heap)μ μμκ° 1κ° λ¨μ λκΉμ§
while (pq.size() != 1) {
// κ°μ₯ μμ 2κ°μ μΉ΄λ λ¬Άμ κΊΌλ΄κΈ°
int one = -pq.top();
pq.pop();
int two = -pq.top();
pq.pop();
// μΉ΄λ λ¬Άμμ ν©μ³μ λ€μ μ½μ
int summary = one + two;
result += summary;
pq.push(-summary);
}
cout << result << '\n';
}