結果

問題 No.615 集合に分けよう
ユーザー takeya_okino
提出日時 2019-08-02 20:11:47
言語 Java
(openjdk 23)
結果
AC  
実行時間 611 ms / 2,000 ms
コード長 608 bytes
コンパイル時間 4,172 ms
コンパイル使用メモリ 77,852 KB
実行使用メモリ 48,816 KB
最終ジャッジ日時 2024-07-05 08:16:24
合計ジャッジ時間 14,866 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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[] a = new long[n];
    for(int i = 0; i < n; i++) {
      a[i] = sc.nextLong();
    }
    Arrays.sort(a);
    long ans = a[n - 1] - a[0];
    long[] b = new long[n - 1];
    for(int i = 1; i < n; i++) {
      b[i - 1] = a[i] - a[i - 1];
    }
    Arrays.sort(b);
    if(n == k) {
      ans = 0; 
    } else {
      for(int i = n - 2; i >= n - k; i--) {
        ans -= b[i];
      }
    }
    System.out.println(ans);
  }
}
0