結果

問題 No.198 キャンディー・ボックス2
ユーザー kou6839
提出日時 2015-04-29 18:45:19
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 907 bytes
コンパイル時間 2,013 ms
コンパイル使用メモリ 76,860 KB
実行使用メモリ 54,436 KB
最終ジャッジ日時 2024-07-05 16:42:55
合計ジャッジ時間 7,107 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14 WA * 12
権限があれば一括ダウンロードができます

ソースコード

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