結果

問題 No.2364 Knapsack Problem
ユーザー miya145592miya145592
提出日時 2023-06-30 22:57:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,653 ms / 3,000 ms
コード長 1,094 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 86,920 KB
実行使用メモリ 505,748 KB
最終ジャッジ日時 2023-09-21 17:13:31
合計ジャッジ時間 19,289 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
75,768 KB
testcase_01 AC 72 ms
75,736 KB
testcase_02 AC 90 ms
76,560 KB
testcase_03 AC 77 ms
76,124 KB
testcase_04 AC 108 ms
77,836 KB
testcase_05 AC 116 ms
82,672 KB
testcase_06 AC 102 ms
78,896 KB
testcase_07 AC 300 ms
160,080 KB
testcase_08 AC 78 ms
76,264 KB
testcase_09 AC 265 ms
150,008 KB
testcase_10 AC 170 ms
95,308 KB
testcase_11 AC 108 ms
81,136 KB
testcase_12 AC 1,651 ms
504,916 KB
testcase_13 AC 1,629 ms
505,748 KB
testcase_14 AC 1,626 ms
505,212 KB
testcase_15 AC 1,649 ms
504,480 KB
testcase_16 AC 1,642 ms
505,036 KB
testcase_17 AC 1,638 ms
505,344 KB
testcase_18 AC 1,638 ms
504,972 KB
testcase_19 AC 1,653 ms
504,392 KB
testcase_20 AC 1,649 ms
504,884 KB
testcase_21 AC 1,646 ms
504,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M, W = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))
INF = 10**15
dp = [[-INF for _ in range(W+1)] for _ in range(1<<(N+M))]
dp[0][0] = 0
for i in range(N+M):
    ndp = [[-INF for _ in range(W+1)] for _ in range(1<<(N+M))]
    for j in range(1<<(N+M)):
        for w in range(W+1):
            if dp[j][w]==-INF:
                continue
            for k in range(N+M):
                if (j>>k)&1:
                    continue
                ndp[j][w] = max(ndp[j][w], dp[j][w])
                if k<N:
                    a = A[k]
                    b = B[k]
                    if 0<=w+a<=W:
                        ndp[j|(1<<k)][w+a] = max(ndp[j|(1<<k)][w+a], dp[j][w]+b)
                else:
                    c = C[k-N]
                    d = D[k-N]
                    if 0<=w-c<=W:
                        ndp[j|(1<<k)][w-c] = max(ndp[j|(1<<k)][w-c], dp[j][w]-d)
    dp = ndp

ans = 0
for j in range(1<<(N+M)):
    ans = max(ans, max(dp[j]))
print(ans)
0