結果

問題 No.1858 Gorgeous Knapsack
ユーザー norioc
提出日時 2025-03-26 02:58:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 447 ms / 2,000 ms
コード長 472 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 77,212 KB
最終ジャッジ日時 2025-03-26 02:58:24
合計ジャッジ時間 7,769 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
items = []
for i in range(N):
    V, W = map(int, input().split())
    items.append((V, W))


def knapsack():
    dp = [-1] * (M+1)
    dp[0] = 0
    res = 0
    for v, w in sorted(items, reverse=True):
        for i in reversed(range(M)):
            if dp[i] == -1: continue
            if i+w > M: continue
            dp[i+w] = max(dp[i+w], dp[i] + v)

        res = max(res, v * max(dp))

    return res


ans = knapsack()
print(ans)
0