結果

問題 No.615 集合に分けよう
ユーザー tentententen
提出日時 2023-04-14 14:22:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 1,929 bytes
コンパイル時間 3,435 ms
コンパイル使用メモリ 91,652 KB
実行使用メモリ 45,448 KB
最終ジャッジ日時 2024-04-18 13:00:14
合計ジャッジ時間 8,898 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,308 KB
testcase_01 AC 53 ms
37,304 KB
testcase_02 AC 54 ms
37,236 KB
testcase_03 AC 53 ms
37,308 KB
testcase_04 AC 53 ms
37,332 KB
testcase_05 AC 53 ms
37,284 KB
testcase_06 AC 54 ms
37,308 KB
testcase_07 AC 54 ms
37,376 KB
testcase_08 AC 55 ms
37,172 KB
testcase_09 AC 55 ms
36,928 KB
testcase_10 AC 55 ms
37,112 KB
testcase_11 AC 55 ms
36,920 KB
testcase_12 AC 54 ms
37,036 KB
testcase_13 AC 58 ms
37,296 KB
testcase_14 AC 81 ms
38,032 KB
testcase_15 AC 137 ms
40,384 KB
testcase_16 AC 218 ms
45,448 KB
testcase_17 AC 224 ms
45,312 KB
testcase_18 AC 213 ms
45,172 KB
testcase_19 AC 217 ms
45,404 KB
testcase_20 AC 217 ms
45,444 KB
testcase_21 AC 222 ms
45,432 KB
testcase_22 AC 222 ms
45,240 KB
testcase_23 AC 167 ms
44,764 KB
testcase_24 AC 215 ms
45,308 KB
testcase_25 AC 53 ms
37,296 KB
testcase_26 AC 53 ms
37,036 KB
testcase_27 AC 139 ms
42,280 KB
testcase_28 AC 132 ms
43,680 KB
testcase_29 AC 182 ms
44,576 KB
testcase_30 AC 177 ms
44,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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();
        }
        Arrays.sort(values);
        long[] diff = new long[n - 1];
        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 Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0