Skip to content

Latest commit

ย 

History

History
49 lines (40 loc) ยท 1.29 KB

File metadata and controls

49 lines (40 loc) ยท 1.29 KB

๐Ÿšจ [2212] ์„ผ์„œ

๋‚œ์ด๋„: ๊ณจ๋“œ 5
์†Œ์š” ์‹œ๊ฐ„: 55๋ถ„
๋ฉ”๋ชจ๋ฆฌ: 15076KB
์‹œ๊ฐ„: 160ms

๋ฆฌ๋ทฐ

  • ์„ผ์„œ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ ์ˆœ์„œ๋กœ ํ•˜๋‚˜์”ฉ ๋ฌถ๋Š”๋‹ค.

์ „์ฒด ์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main_bj2212 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int K = Integer.parseInt(br.readLine());
        int[] arr = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(arr);

        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int s = 1; s < N; s++) {
            if(arr[s] - arr[s - 1] == 0) continue;
            pq.offer(arr[s] - arr[s - 1]);
        }

        int answer = 0;
        int size = pq.size()+1;
        while (size > K) {
            answer += pq.poll();
            size--;
        }
        System.out.println(answer);
    }
}