-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrjoin.java
More file actions
37 lines (31 loc) · 1.14 KB
/
Strjoin.java
File metadata and controls
37 lines (31 loc) · 1.14 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
36
37
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class Strjoin {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int caseCount = Integer.parseInt(br.readLine());
for (int i = 0; i < caseCount; i++) {
br.readLine();
String[] inputs = br.readLine().split(" ");
System.out.println(strcat(inputs));
}
}
public static int strcat(String[] inputs) {
PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
for (int i = 0; i < inputs.length; i++) {
pQueue.add(Integer.parseInt(inputs[i]));
}
int mergeSum = 0, mergeCount = inputs.length - 1, smallSum = 0;
for (int i = 0; i < mergeCount; i++) {
smallSum = pQueue.peek();
pQueue.remove();
smallSum += pQueue.peek();
pQueue.remove();
pQueue.add(smallSum);
mergeSum += smallSum;
}
return mergeSum;
}
}