結果

問題 No.914 Omiyage
ユーザー AEnAEn
提出日時 2022-05-16 16:31:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 829 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 87,232 KB
最終ジャッジ日時 2023-10-12 09:46:33
合計ジャッジ時間 3,442 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 153 ms
86,732 KB
testcase_01 AC 160 ms
85,040 KB
testcase_02 AC 122 ms
84,672 KB
testcase_03 AC 153 ms
87,232 KB
testcase_04 AC 123 ms
85,008 KB
testcase_05 AC 72 ms
71,160 KB
testcase_06 AC 73 ms
71,480 KB
testcase_07 AC 72 ms
71,632 KB
testcase_08 AC 73 ms
71,480 KB
testcase_09 AC 72 ms
71,528 KB
testcase_10 AC 72 ms
71,452 KB
testcase_11 AC 71 ms
71,364 KB
testcase_12 AC 89 ms
76,748 KB
testcase_13 AC 159 ms
84,676 KB
testcase_14 AC 146 ms
84,912 KB
testcase_15 AC 74 ms
71,416 KB
testcase_16 AC 73 ms
71,596 KB
testcase_17 AC 86 ms
76,692 KB
testcase_18 AC 72 ms
71,260 KB
testcase_19 AC 70 ms
71,412 KB
testcase_20 AC 72 ms
71,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product
import bisect

N, M, K = map(int, input().split())
su = [list(map(int, input().split())) for _ in range(N)]

s1, s2 = [], []
for v in product(range(M), repeat = N//2):
    sm = 0
    for i in range(N//2):
        sm += su[i][v[i]]
    s1.append(sm)

for v in product(range(M), repeat=(N+1)//2):
    sm = 0
    for i in range((N+1)//2):
        sm += su[-1-i][v[i]]
    s2.append(sm)
s2.sort()

ans = float('inf')
for s in s1:
    if s > K:
        continue
    idx = bisect.bisect_left(s2, K-s)
    if idx == 0:
        if K-s-s2[0]>=0:
            ans = min(ans, K-s-s2[0])
        else:
            continue
    else:
        if idx < len(s2) and s2[idx] == K-s:
            ans = 0
        else:
            ans = min(ans, K-s-s2[idx-1])
if ans == float('inf'):
    print(-1)
else:
    print(ans)
0