結果

問題 No.198 キャンディー・ボックス2
ユーザー thorikawa
提出日時 2015-04-29 00:27:20
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 882 bytes
コンパイル時間 1,977 ms
コンパイル使用メモリ 74,404 KB
実行使用メモリ 41,736 KB
最終ジャッジ日時 2024-07-05 05:24:18
合計ジャッジ時間 6,684 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by poly on 11/29/14.
 */
public class Main {
    public static void main(String[] argv) {
        Scanner scanner = new Scanner(System.in);
        int b = scanner.nextInt();
        int n = scanner.nextInt();
        int[] c = new int[n];
        int total = 0;
        for (int i = 0; i < n; i++) {
            c[i] = scanner.nextInt();
            total += c[i];
        }
        total += b;
        int max = total / n;
        Arrays.sort(c);
        long ans = 0;
        for (int j = 0; j < n; j++) {
            ans += Math.abs(c[j] - max);
        }
        for (int i = 0; i < n; i++) {
            long tmp = 0;
            for (int j = 0; j < n; j++) {
                tmp += Math.abs(c[j] - c[i]);
            }
            ans = Math.min(tmp, ans);
        }
        System.out.println(ans);
    }
}
0