-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
30 lines (23 loc) · 767 Bytes
/
Main.java
File metadata and controls
30 lines (23 loc) · 767 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.PriorityQueue;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for(int i=0; i<N; i++){
q.offer(Integer.parseInt(br.readLine()));
}
int sum = 0;
while(q.size() >= 2){
int num1 = q.poll();
int num2 = q.poll();
q.offer(num1+num2);
sum += (num1+num2);
}
System.out.println(sum);
br.close();
}
}