結果

問題 No.615 集合に分けよう
ユーザー tentententen
提出日時 2022-10-06 14:55:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 208 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 74,800 KB
実行使用メモリ 57,376 KB
最終ジャッジ日時 2023-08-31 01:10:47
合計ジャッジ時間 6,772 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
49,540 KB
testcase_01 AC 36 ms
49,768 KB
testcase_02 AC 36 ms
49,544 KB
testcase_03 AC 37 ms
49,508 KB
testcase_04 AC 36 ms
49,460 KB
testcase_05 AC 37 ms
49,476 KB
testcase_06 AC 36 ms
49,440 KB
testcase_07 AC 37 ms
49,520 KB
testcase_08 AC 37 ms
49,860 KB
testcase_09 AC 38 ms
49,444 KB
testcase_10 AC 37 ms
47,648 KB
testcase_11 AC 38 ms
49,528 KB
testcase_12 AC 38 ms
49,460 KB
testcase_13 AC 42 ms
49,528 KB
testcase_14 AC 50 ms
51,412 KB
testcase_15 AC 106 ms
53,052 KB
testcase_16 AC 182 ms
56,624 KB
testcase_17 AC 208 ms
56,652 KB
testcase_18 AC 189 ms
56,692 KB
testcase_19 AC 185 ms
56,880 KB
testcase_20 AC 193 ms
56,804 KB
testcase_21 AC 186 ms
56,728 KB
testcase_22 AC 194 ms
56,984 KB
testcase_23 AC 168 ms
57,376 KB
testcase_24 AC 181 ms
56,452 KB
testcase_25 AC 36 ms
49,536 KB
testcase_26 AC 35 ms
49,484 KB
testcase_27 AC 109 ms
55,744 KB
testcase_28 AC 116 ms
54,792 KB
testcase_29 AC 184 ms
55,540 KB
testcase_30 AC 154 ms
55,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        long[] values = new long[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextLong();
        }
        long[] diff = new long[n - 1];
        Arrays.sort(values);
        for (int i = 0; i < n - 1; i++) {
            diff[i] = values[i + 1] - values[i];
        }
        Arrays.sort(diff);
        long ans = 0;
        for (int i = 0; i < n - k; i++) {
            ans += diff[i];
        }
        System.out.println(ans);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0