Problem p02787 (python)

Fixed submission:
def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[inf for i in range(W+1)] for j in range(N+1)]
  dp[0][0]= 0
  #DP
  for i in range(N):
    for w in range(W+1):
      if 0 <= w-weight[i]: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=min(dp[i+1][w-weight[i]]+value[i], dp[i][w])
      else:
        dp[i+1][w]=dp[i][w]
  return dp
 
H,N = map(int,input().split())
 
weight=[]
value=[]
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
MAX = max(weight)
dp = knapsack(N,H+MAX,weight,value)
 
print(min(dp[N][H:H+MAX]))

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
for i in range(N):
  n,a=input().split()
  atk.append(int(n))
  MP.append(int(a))

@lru_cache(maxsize=1000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    b = fx(H - atk[i]) + MP[i]
    arr.append(b)   
  return min(arr)

a = fx(H)
print(a)

Buggy submission (verdict: Runtime Error):
def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[inf for i in range(W+1)] for j in range(N+1)]
  dp[0][0]= 0
  #DP
  for i in range(N):
    for w in range(W+1):
      if 0 <= w-weight[i]: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        if dp[i+1][w-1] != inf:
          a = dp[i+1][w-1]+value[i]
        else:
          a = inf
        dp[i+1][w]=min(dp[i+1][w-weight[i]]+value[i], dp[i][w],a)
      else:
        dp[i+1][w]=dp[i][w]
  return dp[N][W]
 
H,N = map(int,input().split())
 
weight=[]
value=[]
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
a = knapsack(N,H,weight,value)
 
print(int(a))

Buggy submission (verdict: Runtime Error):
def knapsack(N,W,weight,value):
  #初期化
  inf=float("inf")
  dp=[[inf for i in range(W+1)] for j in range(N+1)]
  dp[0][0]= 0
  #DP
  for i in range(N):
    for w in range(W+1):
      if 0 <= w-weight[i]: #dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=min(dp[i+1][w-weight[i]]+value[i], dp[i][w],dp[i+1][w-1]+value[i])
      else:
        dp[i+1][w]=dp[i][w]
  return dp[N][W]
 
H,N = map(int,input().split())
 
weight=[]
value=[]
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
a = knapsack(N,H,weight,value)
 
print(int(a))

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
arr=[]
for i in range(N):
  n,a=input().split()
  arr.append([int(n),int(a)])
arr.sort()

for i in range(N):
  atk.append(arr[i][0])
  MP.append(arr[i][1])

@lru_cache(maxsize=100000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    kaisuu = int(H // atk[i])
    if kaisuu >= 1:
      for j in range(1,kaisuu+1):
        c = int(H - atk[i]*j)
        b = int(fx(c) + MP[i]*j)
        arr.append(b)   
    else:
      b = int(fx(H - atk[i]) + MP[i])
      arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
arr=[]
for i in range(N):
  n,a=input().split()
  arr.append([int(n),int(a)])
arr.sort()

for i in range(N):
  atk.append(arr[i][0])
  MP.append(arr[i][1])

@lru_cache(maxsize=100000000000000000000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    c = int(H - atk[i])
    b = int(fx(c) + MP[i])
    arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
arr=[]
for i in range(N):
  n,a=input().split()
  arr.append([int(n),int(a)])
arr.sort()

for i in range(N):
  atk.append(arr[i][0])
  MP.append(arr[i][1])

@lru_cache(maxsize=1000000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    c = int(H - atk[i])
    b = int(fx(c) + MP[i])
    arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

H,N = map(int,input().split())
atk=[]
MP=[]
for i in range(N):
  n,a=input().split()
  atk.append(int(n))
  MP.append(int(a))

@lru_cache(maxsize=1000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    b = fx(H - atk[i]) + MP[i]
    arr.append(b)   
  return min(arr)

a = fx(H)
print(a)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

#@lru_cache(maxsize=1000)
def fx(H,atk,MP):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    b = fx(H - atk[i],atk,MP) + MP[i]
    arr.append(b)   
  return min(arr)
  
H,N = map(int,input().split())
atk=[]
MP=[]
for i in range(N):
  n,a=input().split()
  atk.append(int(n))
  MP.append(int(a))

a = fx(H,atk,MP)

print(a)