-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva10954.java
More file actions
35 lines (32 loc) · 1.03 KB
/
Copy pathUva10954.java
File metadata and controls
35 lines (32 loc) · 1.03 KB
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
33
34
35
import java.util.*;
import java.io.*;
public class Uva10954 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
while(true)
{
int n = Integer.parseInt(br.readLine());
if(n==0)
break;
PriorityQueue<Integer> pq = new PriorityQueue<>(n);
StringTokenizer st = new StringTokenizer(br.readLine());
int ind = 0 ;
while (st.hasMoreTokens()){
pq.add(Integer.parseInt(st.nextToken()));
}
long cost = 0 ;
while (pq.size()>1){
int x = pq.poll();
int y = pq.poll();
cost += x + y ;
pq.add(x+y);
}
sb.append(cost).append("\n");
}
pr.print(sb.toString());
pr.close();
br.close();
}
}