結果

問題 No.615 集合に分けよう
ユーザー aaaaasatoriaaaaasatori
提出日時 2018-02-15 17:18:38
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,088 bytes
コンパイル時間 3,747 ms
コンパイル使用メモリ 74,280 KB
実行使用メモリ 70,348 KB
最終ジャッジ日時 2023-08-27 00:07:55
合計ジャッジ時間 11,841 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
60,692 KB
testcase_01 AC 124 ms
55,772 KB
testcase_02 AC 122 ms
55,808 KB
testcase_03 AC 122 ms
55,704 KB
testcase_04 AC 124 ms
55,468 KB
testcase_05 AC 125 ms
56,056 KB
testcase_06 WA -
testcase_07 AC 133 ms
55,960 KB
testcase_08 AC 135 ms
56,356 KB
testcase_09 AC 135 ms
55,756 KB
testcase_10 AC 132 ms
56,032 KB
testcase_11 AC 132 ms
56,044 KB
testcase_12 AC 127 ms
55,840 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 292 ms
61,088 KB
testcase_16 AC 123 ms
55,876 KB
testcase_17 AC 500 ms
60,988 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class No615 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		
		if(N == K) {
			System.out.println(0);
		}else {
			long a[] = new long[N];
			for(int i = 0;i < N;i++) {
				a[i] = sc.nextLong();
			}
			Arrays.sort(a);
			if(K == 1) {
				System.out.println(a[N-1] - a[0]);
			}else {
			
				int b[] = new int[K-1]; //分割する位置を入れる配列(a[i] a[i+1]の間で分割するならiが入る)
				long max[] = new long[K-1];
				long sum = 0;
				for(int i = 0;i < K-1;i++) {
					max[i] = 0;
				}
				for(int i = 0;i < N-1;i++) {
					if(max[0] < a[i+1] - a[i]) {
						max[0] = a[i+1] - a[i];
						b[0] = i;
						Arrays.sort(max);
						Arrays.sort(b);
					}
				}
				
				for(int i = 0;i < K-1;i++) {
					if(i == 0) {
						sum += a[b[i]] - a[0];
					}else {
						sum += a[b[i]] - a[b[i-1]+1];
					}
					if(i + 1 == K - 1) {
						sum += a[N-1] - a[b[i]+1];
					}
				}
				
				System.out.println(sum);
			}
		}
	}
}
0