結果

問題 No.2364 Knapsack Problem
ユーザー chankei271828chankei271828
提出日時 2023-06-30 22:04:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,176 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 81,976 KB
最終ジャッジ日時 2023-09-21 16:05:25
合計ジャッジ時間 5,795 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
75,704 KB
testcase_01 AC 98 ms
71,844 KB
testcase_02 AC 109 ms
77,068 KB
testcase_03 AC 101 ms
77,060 KB
testcase_04 AC 117 ms
77,580 KB
testcase_05 AC 133 ms
78,000 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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):
    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)
0