結果
問題 | No.2364 Knapsack Problem |
ユーザー | Shirotsume |
提出日時 | 2023-06-30 22:20:47 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,951 bytes |
コンパイル時間 | 237 ms |
コンパイル使用メモリ | 82,284 KB |
実行使用メモリ | 87,824 KB |
最終ジャッジ日時 | 2024-07-07 10:11:28 |
合計ジャッジ時間 | 19,588 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 739 ms
85,516 KB |
testcase_01 | AC | 740 ms
78,208 KB |
testcase_02 | AC | 739 ms
79,360 KB |
testcase_03 | AC | 738 ms
78,208 KB |
testcase_04 | AC | 739 ms
78,504 KB |
testcase_05 | AC | 738 ms
80,084 KB |
testcase_06 | AC | 739 ms
78,140 KB |
testcase_07 | AC | 757 ms
78,592 KB |
testcase_08 | AC | 740 ms
78,464 KB |
testcase_09 | AC | 750 ms
79,468 KB |
testcase_10 | AC | 745 ms
78,440 KB |
testcase_11 | AC | 764 ms
79,688 KB |
testcase_12 | AC | 745 ms
79,624 KB |
testcase_13 | AC | 791 ms
80,396 KB |
testcase_14 | WA | - |
testcase_15 | AC | 754 ms
79,388 KB |
testcase_16 | AC | 772 ms
80,380 KB |
testcase_17 | AC | 760 ms
79,688 KB |
testcase_18 | TLE | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
ソースコード
import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 import time t1 = time.time() n, m, w = mi() a = li() b = li() c = [v for v in li()] d = [v for v in li()] ans = 0 import itertools WV = [] for x, y in zip(a, b): WV.append((x, y)) for x, y in zip(c, d): WV.append((-x, -y)) def solve(WV, w): n = len(WV) dp = [[-inf] * (w + 1) for _ in range(n + 1)] dp[0][0] = 0 for i in range(n): x, y = WV[i] for j in range(w + 1): if 0 <= j + x <= w: dp[i + 1][j + x] = max(dp[i + 1][j + x], dp[i][j] + y) dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]) return max(dp[n]) ans = 0 while time.time() - t1 <= 0.7: random.shuffle(WV) ans = max(ans, solve(WV, w)) for bit1 in range(1 << n): for bit2 in range(1 << m): WV = [] p = 0 for i in range(n): if 1 & (bit1 >> i): WV.append((a[i], b[i])) p += b[i] if ans >= p: break WIS = [] for i in range(m): if 1 & (bit2 >> i): WIS.append((c[i], d[i])) p -= d[i] if ans >= p: break for WV in itertools.permutations(WV): for WIS in itertools.permutations(WIS): j = 0 noww = 0 nowv = 0 for x, y in WV: noww += x nowv += y if noww > w: nowv = -inf break if j < len(WIS) and WIS[j][0] <= noww: noww -= WIS[j][0] nowv -= WIS[j][1] j += 1 ans = max(ans, nowv) print(ans)