結果

問題 No.527 ナップサック容量問題
コンテスト
ユーザー brthyyjp
提出日時 2022-03-02 13:30:55
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 539 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 172 ms
コンパイル使用メモリ 85,248 KB
実行使用メモリ 81,152 KB
最終ジャッジ日時 2026-03-30 20:30:33
合計ジャッジ時間 3,514 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

n = int(input())
V = []
W = []
for i in range(n):
    v, w = map(int, input().split())
    V.append(v)
    W.append(w)
u = int(input())
if u == 0:
    print(1)
    print(min(W)-1)
    exit()
INF = 10**18
maxv = 10**5+1
dp = [INF]*(maxv+1)
dp[0] = 0
for v, w in zip(V, W):
    for i in reversed(range(maxv)):
        if i+v <= maxv:
            dp[i+v] = min(dp[i+v], dp[i]+w)
for i in reversed(range(maxv-1)):
    dp[i] = min(dp[i], dp[i+1])
if dp[u+1] != INF:
    print(dp[u])
    print(dp[u+1]-1)
else:
    print(dp[u])
    print('inf')
0