結果

問題 No.198 キャンディー・ボックス2
ユーザー kou6839kou6839
提出日時 2015-04-29 18:54:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 1,000 ms
コード長 904 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 77,064 KB
実行使用メモリ 46,960 KB
最終ジャッジ日時 2024-04-28 01:07:56
合計ジャッジ時間 7,068 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
46,768 KB
testcase_01 AC 132 ms
46,616 KB
testcase_02 AC 118 ms
45,256 KB
testcase_03 AC 129 ms
46,820 KB
testcase_04 AC 129 ms
46,212 KB
testcase_05 AC 123 ms
45,492 KB
testcase_06 AC 120 ms
45,316 KB
testcase_07 AC 131 ms
46,280 KB
testcase_08 AC 132 ms
46,200 KB
testcase_09 AC 131 ms
46,436 KB
testcase_10 AC 131 ms
46,460 KB
testcase_11 AC 129 ms
46,296 KB
testcase_12 AC 131 ms
46,560 KB
testcase_13 AC 118 ms
45,188 KB
testcase_14 AC 129 ms
46,308 KB
testcase_15 AC 115 ms
45,076 KB
testcase_16 AC 129 ms
46,380 KB
testcase_17 AC 130 ms
46,300 KB
testcase_18 AC 130 ms
46,960 KB
testcase_19 AC 129 ms
46,616 KB
testcase_20 AC 117 ms
46,596 KB
testcase_21 AC 131 ms
46,344 KB
testcase_22 AC 122 ms
45,396 KB
testcase_23 AC 131 ms
46,436 KB
testcase_24 AC 130 ms
46,364 KB
testcase_25 AC 128 ms
46,460 KB
testcase_26 AC 132 ms
46,880 KB
testcase_27 AC 129 ms
46,488 KB
testcase_28 AC 134 ms
46,396 KB
testcase_29 AC 132 ms
46,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main {
	public static void main(String[] args){
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		long B = sc.nextLong();
		long N = sc.nextLong();
		long[] can = new long[(int)N];
		for(int i=0;i<N;i++){
			can[i]=sc.nextInt();
		}
		long l = -1;long r=1000000000000L;
		while(r-l>2){
			long lm=(2*l+r)/3, rm=(l+2*r)/3;
			long lsum=0,rsum=0;
			for(int i=0;i<N;i++){
				lsum+=lm-can[i];
			}

			for(int i=0;i<N;i++){
				rsum+=rm-can[i];
			}
			long ll=0,rr=0;
			for(int i=0;i<N;i++){
				ll+=Math.abs(lm-can[i]);
			}
			for(int i=0;i<N;i++){
				rr+=Math.abs(rm-can[i]);
			}
			if(lsum>B){
				r=lm;
			}else if(rsum>B){
				r=rm;
			}else if(ll<=rr){
				r=rm;
			}else{
				l=lm;
			}
		}
		long ans=0;
		for(int i=0;i<N;i++){
			ans+=Math.abs((r+l)/2-(long)can[i]);
		}
		System.out.println(ans);
	}
}
0