結果

問題 No.198 キャンディー・ボックス2
ユーザー kou6839
提出日時 2015-04-29 18:28:20
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 904 bytes
コンパイル時間 1,723 ms
コンパイル使用メモリ 77,404 KB
実行使用メモリ 54,152 KB
最終ジャッジ日時 2024-07-05 16:42:25
合計ジャッジ時間 5,879 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 10 WA * 16
権限があれば一括ダウンロードができます

ソースコード

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