結果

問題 No.198 キャンディー・ボックス2
ユーザー uafr_csuafr_cs
提出日時 2015-09-03 00:15:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 114 ms / 1,000 ms
コード長 1,043 bytes
コンパイル時間 2,174 ms
コンパイル使用メモリ 80,656 KB
実行使用メモリ 41,640 KB
最終ジャッジ日時 2024-04-28 01:24:09
合計ジャッジ時間 6,133 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
41,280 KB
testcase_01 AC 111 ms
41,296 KB
testcase_02 AC 103 ms
40,272 KB
testcase_03 AC 110 ms
41,536 KB
testcase_04 AC 112 ms
41,640 KB
testcase_05 AC 113 ms
41,344 KB
testcase_06 AC 114 ms
40,408 KB
testcase_07 AC 104 ms
40,572 KB
testcase_08 AC 111 ms
41,344 KB
testcase_09 AC 101 ms
40,164 KB
testcase_10 AC 113 ms
41,248 KB
testcase_11 AC 106 ms
41,200 KB
testcase_12 AC 112 ms
41,372 KB
testcase_13 AC 105 ms
40,532 KB
testcase_14 AC 106 ms
41,376 KB
testcase_15 AC 107 ms
41,092 KB
testcase_16 AC 110 ms
41,120 KB
testcase_17 AC 106 ms
40,788 KB
testcase_18 AC 102 ms
40,316 KB
testcase_19 AC 103 ms
40,440 KB
testcase_20 AC 112 ms
41,424 KB
testcase_21 AC 109 ms
41,296 KB
testcase_22 AC 102 ms
40,236 KB
testcase_23 AC 113 ms
41,600 KB
testcase_24 AC 104 ms
40,480 KB
testcase_25 AC 113 ms
41,216 KB
testcase_26 AC 113 ms
41,096 KB
testcase_27 AC 107 ms
40,532 KB
testcase_28 AC 95 ms
39,824 KB
testcase_29 AC 107 ms
40,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		final long B = sc.nextLong();
		final int N = sc.nextInt();
		
		long[] arr = new long[N];
		for(int i = 0; i < N; i++){
			final long c = sc.nextLong();
		
			arr[i] = c;
		}
		
		Arrays.sort(arr);
		
		long score = Long.MAX_VALUE;
		for(int pos = 0; pos < N; pos++){
			long benefit = B, current_score = 0;
			
			for(int i = 0; i < N; i++){
				benefit += (arr[i] - arr[pos]);
				current_score += Math.abs(arr[i] - arr[pos]);
			}
			if(benefit < 0){ continue; }
			
			score = Math.min(score, current_score);
		}
		
		{
			final long max = (Arrays.stream(arr).sum() + B) / N;
			
			long current_score = 0;
			for(int i = 0; i < N; i++){
				current_score += Math.abs(arr[i] - max);
			}
			
			score = Math.min(score , current_score);
		}
		
		System.out.println(score);

	}

}
0