-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2212.java
More file actions
48 lines (36 loc) · 1.04 KB
/
BOJ2212.java
File metadata and controls
48 lines (36 loc) · 1.04 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
38
39
40
41
42
43
44
45
46
47
48
package main.week1.BOJ2212;
import java.util.Arrays;
import java.util.Scanner;
/**
* 센서
* @author hazel
*/
public class BOJ2212 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //센서의 개수
int k = sc.nextInt(); //집중국의 개수
//센서
int[] arr = new int[n];
//센서 거리
int[] distance = new int[n - 1];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
//오름차순
Arrays.sort(arr);
//System.out.println(Arrays.toString(arr));
//두 센서 사이의 거리 구하기
for (int i = 0; i < n - 1; i++) {
distance[i] = arr[i + 1] - arr[i];
}
Arrays.sort(distance);
//System.out.println(Arrays.toString(distance));
//k-1개의 사이 빼고 합 구하기
int sum = 0;
for (int i = 0; i < n - k; i++) {
sum += distance[i];
}
System.out.println(sum);
}
}