結果
問題 |
No.2364 Knapsack Problem
|
ユーザー |
![]() |
提出日時 | 2023-06-30 22:22:56 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,051 bytes |
コンパイル時間 | 334 ms |
コンパイル使用メモリ | 82,220 KB |
実行使用メモリ | 77,984 KB |
最終ジャッジ日時 | 2024-07-07 10:13:39 |
合計ジャッジ時間 | 4,904 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 13 WA * 7 |
ソースコード
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+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 (bit0>>(N+j))&1: tmp_ans-=D[j] d[-C[j]]+=1 test0=[] test1=[] for w in range(1,1+W): 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) dame=0 while True: if (now==0 and test0==[]) or (now==1 and test1==[]) or dame>=2: break if now==0: while True: if test0==[]: now=1 dame=0 break w=test0.pop() if w_now+w<=W: w_now+=w dame=0 else: test0.append(w) now=1 dame+=1 break else: while True: if test1==[]: now=0 dame=0 break w=test1.pop() if w_now+w>=0: w_now+=w dame=0 else: test1.append(w) now=0 dame+=1 break if test0==[] and test1==[] and 0<=w_now<=W: ans=max(ans,tmp_ans) print(ans)