結果

問題 No.2364 Knapsack Problem
ユーザー ryohei22ryohei22
提出日時 2023-06-30 22:06:52
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,318 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 589,212 KB
最終ジャッジ日時 2023-09-21 16:08:33
合計ジャッジ時間 15,812 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
76,352 KB
testcase_01 AC 77 ms
76,216 KB
testcase_02 AC 94 ms
76,180 KB
testcase_03 AC 74 ms
75,936 KB
testcase_04 AC 89 ms
76,968 KB
testcase_05 AC 101 ms
80,940 KB
testcase_06 AC 90 ms
76,684 KB
testcase_07 AC 214 ms
130,840 KB
testcase_08 AC 75 ms
76,064 KB
testcase_09 AC 205 ms
130,784 KB
testcase_10 AC 133 ms
86,732 KB
testcase_11 AC 99 ms
80,920 KB
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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 = [[-1] * (W+1) for _ in range(2**(N+M))]
old[0][0] = 0
for _ in range(1, N+M+1):
    neww = [[-1] * (W+1) for _ in range(2**(N+M))]
    for j in range(2**(N+M)):
        for k in range(W):
            if old[j][k] == -1:
                continue
            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 neww[j|2**l][k+A[l]] == -1:
                        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 range(2**(N+M)):
    ans = max(ans, max(old[j]))
print(ans)
0