結果

問題 No.615 集合に分けよう
ユーザー tentententen
提出日時 2023-02-15 15:17:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,965 bytes
コンパイル時間 2,846 ms
コンパイル使用メモリ 87,724 KB
実行使用メモリ 57,536 KB
最終ジャッジ日時 2023-09-25 00:18:30
合計ジャッジ時間 8,149 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,604 KB
testcase_01 AC 44 ms
49,652 KB
testcase_02 AC 43 ms
49,484 KB
testcase_03 AC 42 ms
49,524 KB
testcase_04 AC 42 ms
48,084 KB
testcase_05 AC 44 ms
49,700 KB
testcase_06 AC 44 ms
49,820 KB
testcase_07 AC 45 ms
49,532 KB
testcase_08 AC 45 ms
49,640 KB
testcase_09 AC 45 ms
49,536 KB
testcase_10 AC 45 ms
49,588 KB
testcase_11 AC 45 ms
49,596 KB
testcase_12 AC 44 ms
49,600 KB
testcase_13 AC 50 ms
49,736 KB
testcase_14 AC 60 ms
51,548 KB
testcase_15 AC 124 ms
53,504 KB
testcase_16 AC 213 ms
56,416 KB
testcase_17 AC 209 ms
54,708 KB
testcase_18 AC 212 ms
56,920 KB
testcase_19 AC 210 ms
56,764 KB
testcase_20 AC 211 ms
56,664 KB
testcase_21 AC 213 ms
57,536 KB
testcase_22 AC 213 ms
57,036 KB
testcase_23 AC 164 ms
56,508 KB
testcase_24 AC 195 ms
56,588 KB
testcase_25 AC 42 ms
49,592 KB
testcase_26 AC 42 ms
49,484 KB
testcase_27 AC 122 ms
55,804 KB
testcase_28 AC 124 ms
55,772 KB
testcase_29 AC 169 ms
55,832 KB
testcase_30 AC 171 ms
55,580 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 ans = values[n - 1] - values[0];
        long[] diffs = new long[n - 1];
        for (int i = 0; i < n - 1; i++) {
            diffs[i] = values[i + 1] - values[i];
        }
        Arrays.sort(diffs);
        for (int i = 0; i < k - 1; i++) {
            ans -= diffs[n - i - 2];
        }
        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