結果

問題 No.198 キャンディー・ボックス2
ユーザー uafr_cs
提出日時 2015-09-02 23:11:41
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 863 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 75,248 KB
実行使用メモリ 41,696 KB
最終ジャッジ日時 2024-07-18 21:47:02
合計ジャッジ時間 6,953 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		final long B = sc.nextLong();
		final int N = sc.nextInt();
		
		long[] arr = new long[N];
		for(int i = 0; i < N; i++){
			final long c = sc.nextLong();
		
			arr[i] = c;
		}
		
		Arrays.sort(arr);
		
		long score = Long.MAX_VALUE;
		for(int pos = 0; pos < N; pos++){
			long benefit = B, current_score = 0;
			
			for(int i = 0; i < N; i++){
				benefit += (arr[i] - arr[pos]);
				current_score += Math.abs(arr[pos] - arr[i]);
			}
			
			if(benefit < 0){ continue; }
			
			score = Math.min(score, current_score);
			//System.out.println(pos + " " + arr[pos] + " " + current_score);
		}
		
		System.out.println(score);

	}

}
0