結果

問題 No.2364 Knapsack Problem
ユーザー chankei271828chankei271828
提出日時 2023-06-30 22:14:51
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,865 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 69,760 KB
最終ジャッジ日時 2024-07-07 10:03:23
合計ジャッジ時間 5,058 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 4 TLE * 1 -- * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
    while True:
        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
    if test0==[] and test1==[] and 0<=w_now<=W:
        ans=max(ans,tmp_ans)
print(ans)
0