結果

問題 No.2364 Knapsack Problem
ユーザー ryohei22ryohei22
提出日時 2023-07-01 09:20:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,759 ms / 3,000 ms
コード長 1,092 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 87,216 KB
実行使用メモリ 165,136 KB
最終ジャッジ日時 2023-09-22 03:26:41
合計ジャッジ時間 27,508 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
76,584 KB
testcase_01 AC 85 ms
76,668 KB
testcase_02 AC 107 ms
76,900 KB
testcase_03 AC 87 ms
76,808 KB
testcase_04 AC 106 ms
77,596 KB
testcase_05 AC 146 ms
78,996 KB
testcase_06 AC 106 ms
77,620 KB
testcase_07 AC 318 ms
84,596 KB
testcase_08 AC 90 ms
76,548 KB
testcase_09 AC 314 ms
86,780 KB
testcase_10 AC 182 ms
80,188 KB
testcase_11 AC 125 ms
78,416 KB
testcase_12 AC 2,344 ms
158,684 KB
testcase_13 AC 2,195 ms
157,568 KB
testcase_14 AC 2,336 ms
151,372 KB
testcase_15 AC 2,759 ms
162,948 KB
testcase_16 AC 2,304 ms
155,688 KB
testcase_17 AC 2,564 ms
151,432 KB
testcase_18 AC 2,107 ms
157,176 KB
testcase_19 AC 2,713 ms
165,136 KB
testcase_20 AC 2,733 ms
163,640 KB
testcase_21 AC 2,186 ms
155,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
reference:
	https://yukicoder.me/submissions/886509

[解法]
まず、魔法は重さ-w、価値-vのものの購入と考えることで、操作は一通りにしておける。
操作は順番が違っても同じ重さと価値を得ることに気づきたい。
そうすると、操作の並び替えは必要なく、操作の小さい方から確定させていくことで、回答できる。
(これはbitDP。)
"""

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[s][w]: 行った操作の集合がsで重さがwである時の価値の最大値
dp = [[-float('inf')] * (W + 1) for _ in range(2**(N+M))]
dp[0][0] = 0
for s in range(2**(N+M)):
	for w in range(W+1):
		for i in range(N+M):
			if s >> i & 1 and 0 <= w - A[i] <= W and dp[s-2**i][w-A[i]] != -float('inf'):
				dp[s][w] = max(dp[s][w], dp[s-2**i][w-A[i]] + B[i])

ans = -float('inf')
for i in dp:
	ans = max(ans, max(i))
print(ans)
0