-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVitosFamily.java
44 lines (36 loc) · 1.29 KB
/
VitosFamily.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
37
38
39
40
41
42
43
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
public class VitosFamily {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private static int solve(List<Integer> input) {
Collections.sort(input);
int median = 0;
if (input.size() % 2 == 0) {
int p = input.size() / 2 - 1;
median = (input.get(p) + input.get(p + 1)) / 2;
} else {
int p = input.size() / 2;
median = (input.get(p));
}
int sum = 0;
for (Integer v : input) {
sum += Math.abs(v - median);
}
return sum;
}
public static void main(String[] args) throws IOException {
int cases = Integer.parseInt(reader.readLine().trim());
for (int i = 0; i < cases; ++i) {
List<Integer> input = stream(reader.readLine().trim().split(" "))
.filter(x -> !x.equals("")).map(Integer::parseInt)
.collect(toList());
System.out.println(solve(input.subList(1, input.size())));
}
}
}