문제링크
https://www.acmicpc.net/problem/2212
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int K = Integer.parseInt(br.readLine());
int arr[] = new int[N];
int diff[] = new int[N-1];
st = new StringTokenizer(br.readLine());
for(int i=0;i<N;i++) arr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(arr);
for(int i=0;i<N-1;i++) diff[i] = arr[i+1]-arr[i];
Arrays.sort(diff);
int res = 0;
for(int i=0;i<N-K;i++) res += diff[i];
System.out.println(res);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[JAVA]백준_2564_경비원 (0) | 2021.04.14 |
---|---|
[JAVA]백준 _11404_플로이드 (0) | 2021.04.09 |
[JAVA]백준_13164_행복유치원 (0) | 2021.04.07 |
[JAVA]백준_2141_우체국 (0) | 2021.04.05 |
[JAVA]백준_1755_숫자놀이 (0) | 2021.03.29 |