結果

問題 No.545 ママの大事な二人の子供
ユーザー lloyzlloyz
提出日時 2022-10-17 00:11:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 852 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 95,020 KB
最終ジャッジ日時 2023-09-10 01:15:41
合計ジャッジ時間 6,284 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,104 KB
testcase_01 AC 75 ms
71,092 KB
testcase_02 AC 77 ms
71,152 KB
testcase_03 AC 76 ms
70,884 KB
testcase_04 AC 74 ms
71,020 KB
testcase_05 AC 73 ms
71,036 KB
testcase_06 AC 75 ms
71,200 KB
testcase_07 AC 76 ms
71,032 KB
testcase_08 AC 73 ms
71,080 KB
testcase_09 AC 76 ms
70,972 KB
testcase_10 AC 73 ms
70,948 KB
testcase_11 AC 76 ms
71,068 KB
testcase_12 AC 75 ms
71,112 KB
testcase_13 AC 79 ms
71,032 KB
testcase_14 AC 105 ms
76,132 KB
testcase_15 AC 75 ms
71,104 KB
testcase_16 AC 88 ms
76,196 KB
testcase_17 AC 93 ms
76,324 KB
testcase_18 AC 103 ms
76,844 KB
testcase_19 AC 105 ms
77,108 KB
testcase_20 AC 111 ms
77,376 KB
testcase_21 AC 79 ms
71,204 KB
testcase_22 AC 141 ms
88,108 KB
testcase_23 AC 196 ms
95,020 KB
testcase_24 AC 137 ms
88,028 KB
testcase_25 AC 192 ms
95,016 KB
testcase_26 AC 196 ms
94,776 KB
testcase_27 AC 192 ms
95,016 KB
testcase_28 AC 190 ms
94,752 KB
testcase_29 AC 191 ms
94,904 KB
testcase_30 AC 193 ms
94,928 KB
testcase_31 AC 194 ms
94,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left
import sys
input = sys.stdin.readline

n = int(input())
AB = [list(map(int, input().split())) for _ in range(n)]

n1 = n // 2
P1 = []
for bit in range(1 << n1):
    res = 0
    for i in range(n1):
        if (bit >> i) & 1:
            res += AB[i][0]
        else:
            res -= AB[i][1]
    P1.append(res)
P1 = sorted(set(P1))
m1 = len(P1)

n2 = n - n1
P2 = []
for bit in range(1 << n2):
    res = 0
    for i in range(n2):
        if (bit >> i) & 1:
            res += AB[n1 + i][0]
        else:
            res -= AB[n1 + i][1]
    P2.append(res)
P2 = sorted(set(P2))
m2 = len(P2)

ans = 10**18
for i in range(m1):
    p1 = P1[i]
    idx = bisect_left(P2, -p1)
    if idx == m2:
        ans = min(ans, abs(p1 + P2[idx - 1]))
    else:
        ans = min(ans, abs(p1 + P2[idx - 1]), abs(p1 + P2[idx]))
print(ans)
0