結果
| 問題 |
No.2364 Knapsack Problem
|
| コンテスト | |
| ユーザー |
chankei271828
|
| 提出日時 | 2023-06-30 22:04:28 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,176 bytes |
| コンパイル時間 | 271 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 70,912 KB |
| 最終ジャッジ日時 | 2024-07-07 09:51:48 |
| 合計ジャッジ時間 | 5,060 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 4 TLE * 1 -- * 15 |
ソースコード
from collections import defaultdict
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()))
ans=0
for bit0 in range(1<<N):
for bit1 in range(1<<M):
tmp_ans=0
d=defaultdict(int)
for i in range(N):
if (bit0>>i)&1:
tmp_ans+=B[i]
d[A[i]]+=1
for j in range(M):
if (bit1>>j)&1:
tmp_ans-=D[j]
d[-C[j]]+=1
test0=[]
test1=[]
for w in range(1,W+1):
if w in d and -w in d:
if d[w]>d[-w]:
for _ in range(d[w]-d[-w]):
test0.append(w)
else:
for _ in range(d[-w]-d[w]):
test1.append(-w)
elif w in d:
for _ in range(d[w]):
test0.append(w)
elif -w in d:
for _ in range(d[-w]):
test1.append(-w)
test0.sort()
test1.sort(reverse=True)
now=0
w_now=0
#print(test0,test1,bit0,bit1)
while (test0 or test1):
if (now==0 and test0==[]) or (now==1 and test1==[]):
break
if now==0:
while True:
if test0==[]:
now=1
break
w=test0.pop()
if w_now+w<=W:
w_now+=w
else:
test0.append(w)
now=1
break
else:
while True:
if test1==[]:
now=0
break
w=test1.pop()
if w_now+w>=0:
w_now+=w
else:
test1.append(w)
now=0
break
#print(test0,test1,bit0,bit1)
if test0==[] and test1==[] and 0<=w_now<=W:
ans=max(ans,tmp_ans)
print(ans)
chankei271828