結果

問題 No.198 キャンディー・ボックス2
ユーザー thorikawa
提出日時 2015-04-29 00:35:36
言語 Java
(openjdk 23)
結果
AC  
実行時間 138 ms / 1,000 ms
コード長 934 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 74,288 KB
実行使用メモリ 49,840 KB
最終ジャッジ日時 2024-11-16 09:00:24
合計ジャッジ時間 6,730 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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();
        long[] c = new long[n];
        long total = b;
        for (int i = 0; i < n; i++) {
            c[i] = scanner.nextLong();
            total += c[i];
        }
        long 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++) {
            if (c[i] > max) {
                break;
            }
            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