-
Notifications
You must be signed in to change notification settings - Fork 14
/
2019_RoundA_Training.java
40 lines (32 loc) · 1.09 KB
/
2019_RoundA_Training.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
import java.util.*;
public class Solution {
public static String solve(Scanner scanner) {
int N = scanner.nextInt();
int P = scanner.nextInt();
int[] S = new int[N];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++) {
S[i] = scanner.nextInt();
map.put(S[i], map.getOrDefault(S[i], 0) + 1);
}
Arrays.sort(S);
for (int i = 0; i < N; i++) if (map.get(S[i]) >= P) return "0";
long ans = Long.MAX_VALUE;
long sum = 0;
for (int i = 0; i < P - 1; i++) sum += S[i];
for (int i = P - 1; i < N; i++) {
sum += S[i];
long tmp_ans = P * S[i] - sum;
ans = Math.min(tmp_ans, ans);
sum -= S[i-P+1];
}
return String.valueOf(ans);
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int caseNum = input.nextInt();
for (int ks = 1; ks <= caseNum; ks++) {
System.out.println(String.format("Case #%d: %s", ks, solve(input)));
}
}
}