結果
| 問題 |
No.2364 Knapsack Problem
|
| コンテスト | |
| ユーザー |
chankei271828
|
| 提出日時 | 2023-07-01 00:34:56 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 117 ms / 3,000 ms |
| コード長 | 1,233 bytes |
| コンパイル時間 | 148 ms |
| コンパイル使用メモリ | 82,536 KB |
| 実行使用メモリ | 80,092 KB |
| 最終ジャッジ日時 | 2024-07-07 11:47:36 |
| 合計ジャッジ時間 | 2,555 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
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()))
dp=[[-1]*(1<<(N+M)) for _ in range(N+M)]
w=[[-1]*(1<<(N+M)) for _ in range(N+M)]
for i in range(N+M):
dp[i][0]=0
w[i][0]=0
ans=0
s=set()
for i in range(20):
s.add(1<<i)
for bit in range(1,1<<(N+M)):
for i in range(N+M):
if (bit>>i)&1==0:
continue
if bit in s:
if i<=N-1:
w[i][bit]=A[i]
dp[i][bit]=B[i]
ans=max(ans,dp[i][bit])
continue
for j in range(N+M):
if i==j or (bit>>j)&1==0:
continue
if i<=N-1 and dp[j][bit^(1<<i)]!=-1 and 0<=w[j][bit^(1<<i)]+A[i]<=W:
dp[i][bit]=max(dp[i][bit],dp[j][bit^(1<<i)]+B[i])
w[i][bit]=w[j][bit^(1<<i)]+A[i]
if i>=N and dp[j][bit^(1<<i)]!=-1 and 0<=w[j][bit^(1<<i)]-C[i-N]<=W:
dp[i][bit]=max(dp[i][bit],dp[j][bit^(1<<i)]-D[i-N])
w[i][bit]=w[j][bit^(1<<i)]-C[i-N]
if ans<dp[i][bit]:
#print(i,bit,w[i][bit],dp[i][bit])
ans=max(ans,dp[i][bit])
print(ans)
#print(dp)
chankei271828