結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー brthyyjp
提出日時 2021-04-21 15:03:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,054 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 83,584 KB
最終ジャッジ日時 2024-07-04 05:58:11
合計ジャッジ時間 21,718 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():
    n, k = map(int, input().split())
    PD = []
    for i in range(n):
        p, d = map(int, input().split())
        PD.append((p,d))

    PD.sort(key=lambda x: -x[0])
    INF = 10**18
    dp0 = [-INF]*(k+1)
    dp1 = [-INF]*(k+1)
    dp0[0] = 0
    for p, d in PD:
        nx0 = [-INF]*(k+1)
        nx1 = [-INF]*(k+1)
        for i in range(k+1):
            nx0[i] = dp0[i]
        for i in range(k+1):
            nx1[i] = dp1[i]
        for i in range(k+1):
            if dp0[i] == -INF:
                continue
            if i+p <= k:
                nx1[i+p] = max(nx1[i+p], dp0[i]+d)
        for i in range(k+1):
            if dp1[i] == -INF:
                continue
            nx0[i] = max(nx0[i], dp1[i]+d)
        dp0 = nx0
        dp1 = nx1
    ans = -INF
    for i in range(k+1):
        ans = max(ans, dp0[i], dp1[i])
    print(ans)

if __name__ == '__main__':
    main()
0