結果

問題 No.198 キャンディー・ボックス2
ユーザー thorikawathorikawa
提出日時 2015-04-29 00:35:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 140 ms / 1,000 ms
コード長 934 bytes
コンパイル時間 2,202 ms
コンパイル使用メモリ 74,956 KB
実行使用メモリ 54,876 KB
最終ジャッジ日時 2024-04-28 01:05:33
合計ジャッジ時間 7,102 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
54,140 KB
testcase_01 AC 131 ms
54,276 KB
testcase_02 AC 130 ms
53,996 KB
testcase_03 AC 131 ms
54,400 KB
testcase_04 AC 132 ms
54,152 KB
testcase_05 AC 130 ms
53,856 KB
testcase_06 AC 135 ms
54,268 KB
testcase_07 AC 129 ms
54,300 KB
testcase_08 AC 140 ms
53,892 KB
testcase_09 AC 140 ms
54,000 KB
testcase_10 AC 129 ms
54,464 KB
testcase_11 AC 131 ms
54,000 KB
testcase_12 AC 129 ms
54,032 KB
testcase_13 AC 133 ms
54,140 KB
testcase_14 AC 129 ms
54,296 KB
testcase_15 AC 131 ms
54,264 KB
testcase_16 AC 131 ms
54,440 KB
testcase_17 AC 126 ms
54,188 KB
testcase_18 AC 128 ms
54,080 KB
testcase_19 AC 129 ms
52,940 KB
testcase_20 AC 131 ms
54,400 KB
testcase_21 AC 130 ms
54,380 KB
testcase_22 AC 118 ms
53,460 KB
testcase_23 AC 130 ms
53,996 KB
testcase_24 AC 129 ms
53,896 KB
testcase_25 AC 131 ms
54,288 KB
testcase_26 AC 135 ms
54,876 KB
testcase_27 AC 132 ms
53,996 KB
testcase_28 AC 131 ms
54,228 KB
testcase_29 AC 131 ms
54,344 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();
        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