結果

問題 No.198 キャンディー・ボックス2
ユーザー thorikawathorikawa
提出日時 2015-04-29 00:29:01
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 929 bytes
コンパイル時間 1,972 ms
コンパイル使用メモリ 71,852 KB
実行使用メモリ 56,372 KB
最終ジャッジ日時 2023-09-18 14:35:42
合計ジャッジ時間 6,959 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 127 ms
55,992 KB
testcase_03 AC 126 ms
55,740 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 125 ms
55,864 KB
testcase_10 AC 124 ms
55,948 KB
testcase_11 AC 123 ms
56,132 KB
testcase_12 AC 124 ms
55,952 KB
testcase_13 WA -
testcase_14 AC 123 ms
56,088 KB
testcase_15 AC 121 ms
55,948 KB
testcase_16 AC 122 ms
55,544 KB
testcase_17 AC 122 ms
55,792 KB
testcase_18 AC 125 ms
55,636 KB
testcase_19 AC 125 ms
56,164 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 128 ms
55,832 KB
testcase_25 AC 127 ms
55,996 KB
testcase_26 AC 127 ms
55,888 KB
testcase_27 AC 125 ms
55,952 KB
testcase_28 WA -
testcase_29 AC 126 ms
55,584 KB
権限があれば一括ダウンロードができます

ソースコード

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 = b;
        for (int i = 0; i < n; i++) {
            c[i] = scanner.nextInt();
            total += c[i];
        }
        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++) {
            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