結果

問題 No.527 ナップサック容量問題
ユーザー hanorverhanorver
提出日時 2017-06-09 23:08:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 835 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 11,816 KB
実行使用メモリ 17,196 KB
最終ジャッジ日時 2023-10-24 00:48:56
合計ジャッジ時間 26,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
11,504 KB
testcase_01 AC 199 ms
11,504 KB
testcase_02 AC 166 ms
11,504 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 WA -
testcase_09 AC 149 ms
11,504 KB
testcase_10 TLE -
testcase_11 WA -
testcase_12 AC 1,212 ms
12,296 KB
testcase_13 TLE -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

n = int(input())

knapsack = defaultdict(list)

for i in range(n):
    value, weight = map(int, input().split())
    knapsack[weight] += [value]

m = int(input())

weights = [0] * 200000
for weight, value in knapsack.items():
    mval = max(value)
    for i in range(len(weights) - 1, -1, -1):
        if weights[i] != 0:
            weights[i + weight] = max(weights[i + weight], weights[i] + mval)
    weights[weight] = mval

for i in range(len(weights) - 1):
    if weights[i + 1] == 0:
        weights[i + 1] = weights[i]

min_weight = 0
high_weight = 0

for i in range(len(weights)):
    if weights[i] == m:
        if min_weight == 0:
            min_weight = i
        else:
            high_weight = i

if high_weight == 199999:
    high_weight = "inf"

print(min_weight)
print(high_weight)
0