結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 125 ms
55,632 KB
testcase_03 AC 127 ms
55,520 KB
testcase_04 AC 130 ms
55,324 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 128 ms
53,220 KB
testcase_08 WA -
testcase_09 AC 128 ms
55,576 KB
testcase_10 AC 127 ms
55,424 KB
testcase_11 AC 128 ms
55,848 KB
testcase_12 AC 129 ms
55,648 KB
testcase_13 WA -
testcase_14 AC 128 ms
55,468 KB
testcase_15 AC 130 ms
55,416 KB
testcase_16 AC 131 ms
55,496 KB
testcase_17 AC 130 ms
55,416 KB
testcase_18 AC 134 ms
55,268 KB
testcase_19 AC 128 ms
55,268 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 133 ms
55,832 KB
testcase_25 AC 133 ms
55,404 KB
testcase_26 AC 131 ms
55,284 KB
testcase_27 AC 130 ms
55,488 KB
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();
		}
		long l = -1,r=2000000000;
		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];
			}
			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