-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddAll.java
36 lines (34 loc) · 870 Bytes
/
AddAll.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
31
32
33
34
35
36
/**
* https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1895 tag:
* #heap #priority-queue
*/
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
class AddAll {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Long> resultArr = new ArrayList<>();
while (true) {
int n = sc.nextInt();
if (n == 0) {
break;
}
PriorityQueue<Long> pq = new PriorityQueue<>(n);
for (int i = 0; i < n; i++) {
int tmp = sc.nextInt();
pq.add((long) tmp);
}
long result = 0;
while (pq.size() > 1) {
long tmp = pq.poll() + pq.poll();
result += tmp;
pq.add(tmp);
}
resultArr.add(result);
}
for (long i : resultArr) {
System.out.println(i);
}
}
}