-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.java
More file actions
37 lines (28 loc) · 779 Bytes
/
Main.java
File metadata and controls
37 lines (28 loc) · 779 Bytes
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.util.Arrays;
import java.util.Scanner;
public class Main {
private static int N;
private static int K;
private static int[] data;
private static int[] term;
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
N = sc.nextInt();
K = sc.nextInt();
data = new int[N];
term = new int[N - 1];
int sum = 0;
for(int i = 0; i < N; i ++){
data[i] = sc.nextInt();
}
Arrays.sort(data);
for(int i = 0; i < N - 1; i ++){
term[i] = data[i + 1] - data[i];
}
Arrays.sort(term);
for(int i = 0; i < N - K; i ++){
sum += term[i];
}
System.out.println(sum);
}
}