結果

問題 No.615 集合に分けよう
ユーザー tenten
提出日時 2020-09-05 11:51:32
言語 Java
(openjdk 23)
結果
AC  
実行時間 641 ms / 2,000 ms
コード長 575 bytes
コンパイル時間 2,201 ms
コンパイル使用メモリ 74,568 KB
実行使用メモリ 48,836 KB
最終ジャッジ日時 2024-11-28 01:02:57
合計ジャッジ時間 14,134 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int k = sc.nextInt();
    	long[] arr = new long[n];
    	for (int i = 0; i < n; i++) {
    	    arr[i] = sc.nextLong();
    	}
    	Arrays.sort(arr);
    	long[] dif = new long[n - 1];
    	for (int i = 0; i < n - 1; i++) {
    	    dif[i] = arr[i + 1] - arr[i];
    	}
    	Arrays.sort(dif);
    	long ans = 0;
    	for (int i = 0; i < n - k; i++) {
    	    ans += dif[i];
    	}
    	System.out.println(ans);
	}
}
0