結果

問題 No.2364 Knapsack Problem
ユーザー Akijin_007
提出日時 2023-06-30 23:00:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 70 ms / 3,000 ms
コード長 909 bytes
コンパイル時間 194 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 67,668 KB
最終ジャッジ日時 2024-07-07 10:56:27
合計ジャッジ時間 2,448 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#int(input())
#map(int, input().split())
#list(map(int, input().split()))

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()))

import heapq

vs = [0] * (1 << (N+M))
ws = [0] * (1 << (N+M))

for i in range(1 << (N+M)):
    v = 0
    w = 0
    for j in range(N+M):
        if i & (1 << j):
            if j < N:
                w += A[j]
                v += B[j]
            else:
                w -= C[j-N]
                v -= D[j-N]
    vs[i] = v
    ws[i] = w

d = [0] * (1 << (N+M))
d[0] = 1

q = [0]
ans = 0
while q:
    t = q.pop()
    for i in range(N+M):
        if not t & (1 << i):
            x = t + (1 << i)
            if d[x] == 0 and ws[x] <= W and ws[x] >= 0:
                q.append(x)
                d[x] = 1
                ans = max(ans, vs[x])

print(ans)
0