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.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class STRJOIN {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
while (cases-- > 0) {
int n = sc.nextInt();
ArrayList list = new ArrayList();
for(int i = 0;n>i;i++)
list.add(sc.nextInt());
int tmp=0,cost=0;
for(int i = 0;n-1>i;i++){
Collections.sort(list);
tmp=(int)list.get(0)+(int)list.get(1);
cost+=tmp;
list.add(tmp);
list.remove(0);
list.remove(0);
}
System.out.println(cost);
}
}
}
|