結果

問題 No.527 ナップサック容量問題
ユーザー brthyyjp
提出日時 2022-03-02 13:30:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 539 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 81,408 KB
実行使用メモリ 76,288 KB
最終ジャッジ日時 2024-07-16 06:45:18
合計ジャッジ時間 4,559 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

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