結果

問題 No.545 ママの大事な二人の子供
ユーザー tentententen
提出日時 2021-03-09 08:21:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 1,702 bytes
コンパイル時間 3,797 ms
コンパイル使用メモリ 78,808 KB
実行使用メモリ 59,444 KB
最終ジャッジ日時 2024-04-19 06:30:15
合計ジャッジ時間 10,696 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
54,256 KB
testcase_01 AC 133 ms
54,028 KB
testcase_02 AC 129 ms
53,744 KB
testcase_03 AC 130 ms
54,016 KB
testcase_04 AC 128 ms
54,100 KB
testcase_05 AC 116 ms
53,036 KB
testcase_06 AC 131 ms
54,152 KB
testcase_07 AC 128 ms
54,108 KB
testcase_08 AC 130 ms
54,032 KB
testcase_09 AC 128 ms
54,068 KB
testcase_10 AC 136 ms
53,820 KB
testcase_11 AC 134 ms
54,096 KB
testcase_12 AC 136 ms
54,128 KB
testcase_13 AC 125 ms
52,824 KB
testcase_14 AC 140 ms
53,952 KB
testcase_15 AC 107 ms
52,220 KB
testcase_16 AC 142 ms
54,180 KB
testcase_17 AC 149 ms
53,928 KB
testcase_18 AC 179 ms
54,156 KB
testcase_19 AC 193 ms
54,768 KB
testcase_20 AC 194 ms
57,048 KB
testcase_21 AC 125 ms
41,124 KB
testcase_22 AC 298 ms
48,856 KB
testcase_23 AC 449 ms
54,936 KB
testcase_24 AC 313 ms
48,884 KB
testcase_25 AC 408 ms
55,120 KB
testcase_26 AC 390 ms
56,464 KB
testcase_27 AC 408 ms
54,332 KB
testcase_28 AC 379 ms
54,448 KB
testcase_29 AC 411 ms
54,440 KB
testcase_30 AC 459 ms
59,444 KB
testcase_31 AC 390 ms
54,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n == 1) {
            System.out.println(Math.min(sc.nextLong(), sc.nextLong()));
            return;
        }
        int n1 = n / 2;
        int n2 = n - n1;
        long[][] arr1 = new long[n1][2];
        for (int i = 0; i < n1; i++) {
            arr1[i][0] = sc.nextLong();
            arr1[i][1] = -sc.nextLong();
        }
        long[][] arr2 = new long[n2][2];
        for (int i = 0; i < n2; i++) {
            arr2[i][0] = sc.nextLong();
            arr2[i][1] = -sc.nextLong();
        }
        TreeSet<Long> result1 = new TreeSet<>();
        for (int i = 0; i < (1 << n1); i++) {
            long ans = 0;
            int x = i;
            for (int j = 0; j < n1; j++) {
                ans += arr1[j][x % 2];
                x >>= 1;
            }
            result1.add(ans);
        }
        TreeSet<Long> result2 = new TreeSet<>();
        for (int i = 0; i < (1 << n2); i++) {
            long ans = 0;
            int x = i;
            for (int j = 0; j < n2; j++) {
                ans += arr2[j][x % 2];
                x >>= 1;
            }
            result2.add(ans);
        }
        long ans = Long.MAX_VALUE;
        for (long x : result1) {
            Long ciel = result2.ceiling(-x);
            if (ciel != null) {
                ans = Math.min(ans, Math.abs(ciel + x));
            }
            Long floor = result2.floor(-x);
            if (floor != null) {
                ans = Math.min(ans, Math.abs(x + floor));
            }
        }
        System.out.println(ans);

    }
}
0