結果

問題 No.615 集合に分けよう
ユーザー takeya_okinotakeya_okino
提出日時 2019-08-02 20:11:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 544 ms / 2,000 ms
コード長 608 bytes
コンパイル時間 2,803 ms
コンパイル使用メモリ 72,256 KB
実行使用メモリ 62,904 KB
最終ジャッジ日時 2023-09-18 18:26:27
合計ジャッジ時間 13,023 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,676 KB
testcase_01 AC 122 ms
56,464 KB
testcase_02 AC 122 ms
55,464 KB
testcase_03 AC 123 ms
56,204 KB
testcase_04 AC 123 ms
55,464 KB
testcase_05 AC 121 ms
55,900 KB
testcase_06 AC 124 ms
56,052 KB
testcase_07 AC 136 ms
55,796 KB
testcase_08 AC 134 ms
55,616 KB
testcase_09 AC 134 ms
55,984 KB
testcase_10 AC 132 ms
56,140 KB
testcase_11 AC 130 ms
55,620 KB
testcase_12 AC 133 ms
55,936 KB
testcase_13 AC 183 ms
55,960 KB
testcase_14 AC 190 ms
56,076 KB
testcase_15 AC 294 ms
60,688 KB
testcase_16 AC 535 ms
60,372 KB
testcase_17 AC 534 ms
62,904 KB
testcase_18 AC 523 ms
60,724 KB
testcase_19 AC 513 ms
60,940 KB
testcase_20 AC 524 ms
60,796 KB
testcase_21 AC 515 ms
60,880 KB
testcase_22 AC 544 ms
60,624 KB
testcase_23 AC 397 ms
60,196 KB
testcase_24 AC 518 ms
60,920 KB
testcase_25 AC 123 ms
55,916 KB
testcase_26 AC 123 ms
55,608 KB
testcase_27 AC 359 ms
60,344 KB
testcase_28 AC 378 ms
60,552 KB
testcase_29 AC 468 ms
60,828 KB
testcase_30 AC 465 ms
60,628 KB
権限があれば一括ダウンロードができます

ソースコード

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