結果

問題 No.198 キャンディー・ボックス2
ユーザー kou6839kou6839
提出日時 2015-04-29 18:28:20
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 904 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 74,376 KB
実行使用メモリ 56,372 KB
最終ジャッジ日時 2023-09-19 04:07:20
合計ジャッジ時間 7,772 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 131 ms
55,676 KB
testcase_03 AC 130 ms
55,500 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 130 ms
55,612 KB
testcase_08 WA -
testcase_09 AC 129 ms
55,832 KB
testcase_10 AC 127 ms
55,428 KB
testcase_11 AC 131 ms
55,616 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 127 ms
55,424 KB
testcase_15 AC 129 ms
55,512 KB
testcase_16 AC 171 ms
55,664 KB
testcase_17 AC 129 ms
55,500 KB
testcase_18 AC 129 ms
55,656 KB
testcase_19 AC 133 ms
55,516 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 130 ms
55,420 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main {
	static int[] tatesum,yokosum;
	public static void main(String[] args){
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int B = sc.nextInt();
		int N = sc.nextInt();
		int[] can = new int[N];
		for(int i=0;i<N;i++){
			can[i]=sc.nextInt();
		}
		int l = -1,r=1000000000;
		while(r-l>2){
			int lm=(2*l+r)/3, rm=(l+2*r)/3;
			int 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];
			}
			int 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;
			}
		}
		int ans=0;
		for(int i=0;i<N;i++){
			ans+=Math.abs((r+l)/2-can[i]);
		}
		System.out.println(ans);
	}
}
0