結果

問題 No.2364 Knapsack Problem
ユーザー ryohei22ryohei22
提出日時 2023-06-30 22:29:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 174 ms / 3,000 ms
コード長 1,425 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 81,408 KB
最終ジャッジ日時 2023-09-21 16:37:27
合計ジャッジ時間 4,332 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,748 KB
testcase_01 AC 89 ms
71,592 KB
testcase_02 AC 89 ms
71,640 KB
testcase_03 AC 89 ms
71,632 KB
testcase_04 AC 115 ms
77,684 KB
testcase_05 AC 99 ms
77,032 KB
testcase_06 AC 103 ms
77,680 KB
testcase_07 AC 127 ms
77,892 KB
testcase_08 AC 90 ms
71,728 KB
testcase_09 AC 119 ms
78,072 KB
testcase_10 AC 129 ms
77,924 KB
testcase_11 AC 101 ms
76,980 KB
testcase_12 AC 148 ms
79,772 KB
testcase_13 AC 146 ms
80,204 KB
testcase_14 AC 132 ms
78,776 KB
testcase_15 AC 163 ms
81,408 KB
testcase_16 AC 142 ms
80,328 KB
testcase_17 AC 139 ms
79,404 KB
testcase_18 AC 142 ms
80,360 KB
testcase_19 AC 169 ms
80,996 KB
testcase_20 AC 174 ms
81,172 KB
testcase_21 AC 142 ms
80,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


N, M, W = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A += list(map(lambda x: -int(x), input().split()))
B += list(map(lambda x: -int(x), input().split()))

# bitDP
# dp[i][j][k]: i回各操作を行うかを決め、行った操作の集合がjであり、重さがkである時の価値の最大値
# MLEでたから、dp[i-1]とdp[i]のみを、oldとnewwとして保持してみる
old = defaultdict(lambda: defaultdict(int))
old[0][0] = 0
for _ in range(N+M):
    neww = defaultdict(lambda: defaultdict(int))
    for j in old:
        for k in old[j]:
            if j not in neww or k not in neww[j]:
                neww[j][k] = old[j][k]
            else:
                neww[j][k] = max(neww[j][k], old[j][k])
            for l in range(N+M):
                # l番目の操作をまだ行っていない かつ 操作を行っても重さが条件を満たす
                if not j >> l & 1 and 0 <= k + A[l] <= W:
                    if j | 2**l not in neww or k + A[l] not in neww[j|2**l]:
                        neww[j|2**l][k+A[l]] = old[j][k] + B[l]
                    else:
                        neww[j|2**l][k+A[l]] \
                            = max(neww[j|2**l][k+A[l]], old[j][k] + B[l])
    old = neww

ans = -float('inf')
for j in old:
    for k in old[j]:
        ans = max(ans, old[j][k])
print(ans)
0