結果

問題 No.2918 Divide Applicants Fairly
ユーザー 高橋ゆに高橋ゆに
提出日時 2024-09-18 19:56:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,340 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,340 KB
実行使用メモリ 80,868 KB
最終ジャッジ日時 2024-10-07 21:05:57
合計ジャッジ時間 7,188 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,972 KB
testcase_01 AC 45 ms
54,712 KB
testcase_02 AC 42 ms
55,044 KB
testcase_03 AC 42 ms
54,784 KB
testcase_04 AC 43 ms
53,864 KB
testcase_05 AC 42 ms
54,900 KB
testcase_06 AC 46 ms
59,744 KB
testcase_07 AC 41 ms
54,540 KB
testcase_08 AC 43 ms
54,592 KB
testcase_09 AC 98 ms
76,936 KB
testcase_10 AC 42 ms
54,864 KB
testcase_11 AC 43 ms
54,332 KB
testcase_12 AC 108 ms
76,300 KB
testcase_13 AC 51 ms
63,616 KB
testcase_14 AC 46 ms
59,692 KB
testcase_15 AC 43 ms
55,048 KB
testcase_16 AC 42 ms
54,860 KB
testcase_17 AC 105 ms
76,876 KB
testcase_18 AC 43 ms
54,244 KB
testcase_19 AC 43 ms
55,180 KB
testcase_20 AC 43 ms
54,284 KB
testcase_21 AC 108 ms
76,436 KB
testcase_22 AC 43 ms
54,308 KB
testcase_23 AC 99 ms
76,216 KB
testcase_24 AC 43 ms
54,716 KB
testcase_25 AC 43 ms
55,340 KB
testcase_26 AC 44 ms
55,384 KB
testcase_27 AC 225 ms
80,868 KB
testcase_28 AC 47 ms
60,472 KB
testcase_29 AC 52 ms
62,584 KB
testcase_30 AC 42 ms
55,704 KB
testcase_31 AC 69 ms
71,556 KB
testcase_32 AC 41 ms
54,072 KB
testcase_33 AC 42 ms
54,800 KB
testcase_34 AC 41 ms
54,644 KB
testcase_35 AC 41 ms
55,104 KB
testcase_36 AC 194 ms
80,180 KB
testcase_37 AC 161 ms
76,992 KB
testcase_38 AC 123 ms
76,888 KB
testcase_39 AC 42 ms
54,624 KB
testcase_40 AC 106 ms
76,464 KB
testcase_41 AC 139 ms
76,736 KB
testcase_42 AC 151 ms
80,108 KB
testcase_43 AC 44 ms
55,604 KB
testcase_44 AC 46 ms
54,864 KB
testcase_45 AC 43 ms
54,328 KB
testcase_46 AC 43 ms
54,400 KB
testcase_47 AC 43 ms
54,204 KB
testcase_48 AC 99 ms
76,156 KB
testcase_49 AC 167 ms
77,260 KB
testcase_50 AC 44 ms
53,936 KB
testcase_51 AC 95 ms
76,604 KB
testcase_52 AC 131 ms
77,452 KB
testcase_53 AC 98 ms
76,280 KB
testcase_54 AC 98 ms
76,268 KB
testcase_55 AC 113 ms
76,612 KB
testcase_56 AC 114 ms
76,284 KB
testcase_57 AC 223 ms
80,220 KB
testcase_58 AC 122 ms
76,128 KB
testcase_59 AC 119 ms
76,368 KB
testcase_60 AC 53 ms
64,684 KB
testcase_61 AC 115 ms
76,200 KB
testcase_62 AC 114 ms
76,676 KB
testcase_63 AC 111 ms
76,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left
from collections import defaultdict

N = int(input())
    
if N > 22:
    print(0)
    exit()

rates = list(map(int, input().split()))
rate_sums = defaultdict(lambda: [])

def frontexh(i, dif, rate_sum):
    if i == N // 2:
        rate_sums[dif].append(rate_sum)
    else:
        frontexh(i + 1, dif, rate_sum)
        frontexh(i + 1, dif + 1, rate_sum + rates[i])
        frontexh(i + 1, dif - 1, rate_sum - rates[i])

frontexh(0, 0, 0)

if rate_sums[0]: rate_sums[0].pop(0)
for dif in range(-N, N + 1): rate_sums[dif].sort()

ans = 2 ** 30

for rate_sum in rate_sums[0]:
    ans = min(ans, abs(rate_sum))

has_anyone = False

def backexh(i, dif, rate_sum):
    global ans, has_anyone
    if i == N - N // 2:
        if dif == 0:
            if has_anyone:
                ans = min(ans, min(ans, abs(rate_sum)))
            else:
                has_anyone = True
        it = bisect_left(rate_sums[-dif], -rate_sum)
        if it < len(rate_sums[dif]):
            ans = min(ans, abs(rate_sum + rate_sums[-dif][it]))
        if it > 0:
            ans = min(ans, abs(rate_sum + rate_sums[-dif][it - 1]))
    else:
        backexh(i + 1, dif, rate_sum)
        backexh(i + 1, dif + 1, rate_sum + rates[N // 2 + i])
        backexh(i + 1, dif - 1, rate_sum - rates[N // 2 + i])

backexh(0, 0, 0)
print(ans)
0