結果

問題 No.2364 Knapsack Problem
ユーザー shobonvipshobonvip
提出日時 2023-06-30 00:19:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,700 ms / 3,000 ms
コード長 926 bytes
コンパイル時間 1,278 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 142,584 KB
最終ジャッジ日時 2023-09-21 14:36:39
合計ジャッジ時間 19,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
76,520 KB
testcase_01 AC 79 ms
76,184 KB
testcase_02 AC 96 ms
76,464 KB
testcase_03 AC 89 ms
76,292 KB
testcase_04 AC 100 ms
76,160 KB
testcase_05 AC 126 ms
78,788 KB
testcase_06 AC 105 ms
76,364 KB
testcase_07 AC 226 ms
83,008 KB
testcase_08 AC 83 ms
76,348 KB
testcase_09 AC 225 ms
84,384 KB
testcase_10 AC 129 ms
78,972 KB
testcase_11 AC 116 ms
78,076 KB
testcase_12 AC 1,525 ms
142,432 KB
testcase_13 AC 1,435 ms
142,540 KB
testcase_14 AC 1,500 ms
142,344 KB
testcase_15 AC 1,700 ms
142,280 KB
testcase_16 AC 1,524 ms
142,584 KB
testcase_17 AC 1,567 ms
142,580 KB
testcase_18 AC 1,383 ms
142,428 KB
testcase_19 AC 1,547 ms
142,292 KB
testcase_20 AC 1,564 ms
142,516 KB
testcase_21 AC 1,426 ms
142,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# N, M が小さいことに注目して bit DP を行う
# bit DP でもつべき状態は [今の使ったものの集合][現在の重さ] の価値の最大値

# O(2^(N+M) W (N+M)) 間に合うかやや微妙

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()))

z = n + m
dp = [[- 10 ** 18] * (w+1) for i in range(1 << z)]

dp[0][0] = 0

for i in range(1 << z):
	for noww in range(w + 1):
		for j in range(z):
			if i >> j & 1:
				if j < n:
					# buy
					if noww - a[j] < 0:
						continue
					dp[i][noww] = max(dp[i][noww], dp[i^(1<<j)][noww-a[j]] + b[j])
				else:
					# use magic
					if noww + c[j-n] > w:
						continue
					dp[i][noww] = max(dp[i][noww], dp[i^(1<<j)][noww+c[j-n]] - d[j-n])

ans = 0
for i in range(1 << z):
	for j in range(w + 1):
		ans = max(ans, dp[i][j])

print(ans)
0