結果

問題 No.527 ナップサック容量問題
ユーザー qibqib
提出日時 2023-05-04 22:16:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 731 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 138,408 KB
最終ジャッジ日時 2024-05-02 02:22:58
合計ジャッジ時間 6,699 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
65,536 KB
testcase_01 AC 57 ms
65,280 KB
testcase_02 AC 53 ms
65,920 KB
testcase_03 AC 54 ms
65,536 KB
testcase_04 AC 53 ms
65,408 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 54 ms
66,048 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 59 ms
67,328 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 59 ms
67,712 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 69 ms
71,424 KB
testcase_36 AC 56 ms
65,536 KB
testcase_37 WA -
testcase_38 AC 69 ms
70,528 KB
testcase_39 AC 69 ms
71,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 1 << 60

n = int(input())
items = [list(map(int, input().split())) for _ in range(n)]

v = int(input())
dp = {}
dp[0] = 0
for vi, wi in items:
  ndp = {}
  for value, weight in dp.items():
    ndp[value] = min(ndp.get(value, INF), weight)
    if value <= v:
      ndp[value + vi] = min(ndp.get(value + vi, INF), weight + wi)
  dp = ndp

cands = list(dp.items())
cands.sort(key=lambda x: - x[1])

m = 100001

score = [0]
cands.pop()
for _ in range(m):
  score.append(score[-1])
  if len(cands) > 0 and len(score) - 1 == cands[-1][1]:
    score[-1], _ = cands.pop()

lb = INF
ub = - INF
for x in range(1, m + 1):
  if score[x] == v:
    lb = min(lb, x)
    ub = max(ub, x)

print(lb)
if ub < m:
  print(ub)
else:
  print("inf")
0