-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.java
More file actions
28 lines (23 loc) · 795 Bytes
/
Main.java
File metadata and controls
28 lines (23 loc) · 795 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class Main {
private static int N;
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int answer = 0;
public static void main(String[] args) throws IOException {
PriorityQueue<Long> q = new PriorityQueue<>();
N = Integer.parseInt(br.readLine());
for(int i = 0; i < N; i ++){
q.offer(Long.parseLong(br.readLine()));
}
while(q.size() > 1){
long deck1 = q.poll();
long deck2 = q.poll();
answer += (deck1 + deck2);
q.offer(deck1 + deck2);
}
System.out.println(answer);
}
}