-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyess.java
More file actions
30 lines (27 loc) · 782 Bytes
/
yess.java
File metadata and controls
30 lines (27 loc) · 782 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Main {
public static void main(String [] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
PriorityQueue <Integer> pq = new PriorityQueue<Integer>();
for(int i = 0 ; i < N ; i ++) {
pq.add(Integer.parseInt(br.readLine()));
}
int answer = 0 ;
if (pq.size() == 0 ) {
System.out.println(0);
return ;
}
while (pq.size() >= 2) {
int num1 = pq.poll();
int num2 = pq.poll();
answer += num1 + num2;
pq.add(num1 + num2);
}
System.out.println(answer);
}
}