結果

問題 No.2741 Balanced Choice
ユーザー norioc
提出日時 2024-04-25 00:42:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 718 ms / 2,000 ms
コード長 754 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2024-11-07 10:08:23
合計ジャッジ時間 6,903 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

def knapsack(ss: list):
    dp = [-INF] * (W+1)
    dp[0] = 0
    for w, v in ss:
        for i in reversed(range(W+1)):
            if dp[i] == -INF: continue
            if i+w > W: continue
            dp[i+w] = max(dp[i+w], dp[i] + v)

    return dp


INF = 1 << 60
N, W, D = map(int, input().split())
ss0 = []
ss1 = []
for _ in range(N):
    t, w, v = map(int, input().split())
    if t == 0:
        ss0.append((w, v))
    else:
        ss1.append((w, v))

dp0 = knapsack(ss0)
dp1 = knapsack(ss1)
ans = 0
for i in range(W+1):
    lo = max(0, i-D)
    hi = min(i+D, W)
    for j in range(lo, hi+1):
        if i+j > W: break
        if dp0[i] == -INF: continue
        if dp1[j] == -INF: continue
        ans = max(ans, dp0[i] + dp1[j])

print(ans)
0