結果

問題 No.527 ナップサック容量問題
ユーザー rlangevinrlangevin
提出日時 2023-02-17 12:46:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 748 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 86,564 KB
実行使用メモリ 159,212 KB
最終ジャッジ日時 2023-09-26 12:57:08
合計ジャッジ時間 8,249 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
79,144 KB
testcase_01 AC 83 ms
80,028 KB
testcase_02 AC 68 ms
71,284 KB
testcase_03 AC 89 ms
80,104 KB
testcase_04 AC 77 ms
77,428 KB
testcase_05 AC 69 ms
71,124 KB
testcase_06 AC 70 ms
71,380 KB
testcase_07 AC 257 ms
132,856 KB
testcase_08 AC 67 ms
71,356 KB
testcase_09 AC 68 ms
71,384 KB
testcase_10 AC 298 ms
147,628 KB
testcase_11 AC 67 ms
71,244 KB
testcase_12 AC 67 ms
71,244 KB
testcase_13 AC 69 ms
71,356 KB
testcase_14 AC 130 ms
102,344 KB
testcase_15 AC 159 ms
118,132 KB
testcase_16 AC 87 ms
80,188 KB
testcase_17 AC 150 ms
110,484 KB
testcase_18 AC 154 ms
115,520 KB
testcase_19 AC 90 ms
80,884 KB
testcase_20 AC 129 ms
104,000 KB
testcase_21 AC 238 ms
159,212 KB
testcase_22 AC 179 ms
131,128 KB
testcase_23 AC 226 ms
156,732 KB
testcase_24 AC 107 ms
92,532 KB
testcase_25 AC 178 ms
129,624 KB
testcase_26 AC 170 ms
123,676 KB
testcase_27 AC 224 ms
124,624 KB
testcase_28 AC 156 ms
117,828 KB
testcase_29 AC 328 ms
158,480 KB
testcase_30 AC 67 ms
71,308 KB
testcase_31 AC 138 ms
111,360 KB
testcase_32 AC 159 ms
119,688 KB
testcase_33 AC 141 ms
112,980 KB
testcase_34 AC 155 ms
121,120 KB
testcase_35 AC 166 ms
121,320 KB
testcase_36 AC 82 ms
79,184 KB
testcase_37 AC 141 ms
109,756 KB
testcase_38 AC 160 ms
118,740 KB
testcase_39 AC 155 ms
115,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
M = 105 * 1000
inf = 10 ** 18
v, w = [0] * N, [0] * N
for i in range(N):
    v[i], w[i] = map(int, input().split())
V = int(input())
if V == sum(v):
    print(sum(w))
    print("inf")
    exit()

pre = [-inf] * M
pre[0] = 0
for i in range(N):
    dp = [-inf] * M
    for j in range(M):
        dp[j] = pre[j]
        if j - w[i] >= 0:
            dp[j] = max(dp[j], pre[j - w[i]] + v[i])
    pre, dp = dp, pre

for i in range(1, M):
    pre[i] = max(pre[i], pre[i - 1])

ans1, ans2 = 0, 0
if V == 0:
    print(1)
    print(min(w) - 1)
    exit()

for i in range(M):
    if pre[i] == V and ans1 == 0:
        ans1 = i
    if pre[i] != V and ans1 and ans2 == 0:
        ans2 = i - 1
if ans1 == 0:
    ans1 = 1
print(ans1)
print(ans2)
0