forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.java
30 lines (24 loc) Β· 824 Bytes
/
4.java
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
import java.util.*;
public class Main {
public static int n, result;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
PriorityQueue<Integer> pq = new PriorityQueue<>();
// ν(Heap)μ μ΄κΈ° μΉ΄λ λ¬Άμμ λͺ¨λ μ½μ
for (int i = 0; i < n; i++) {
pq.offer(sc.nextInt());
}
// ν(Heap)μ μμκ° 1κ° λ¨μ λκΉμ§
while (pq.size() != 1) {
// κ°μ₯ μμ 2κ°μ μΉ΄λ λ¬Άμ κΊΌλ΄κΈ°
int one = pq.poll();
int two = pq.poll();
// μΉ΄λ λ¬Άμμ ν©μ³μ λ€μ μ½μ
int summary = one + two;
result += summary;
pq.offer(summary);
}
System.out.println(result);
}
}