Problem:


Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815



Sample input:
9 3
8 3
4 2
2 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
h,n = map(int,input().split())
dg = 10**5

ba = []
for _ in [0]*n:
    a,b = map(int,input().split())
    if a >= h:
        a = h
    ba.append(b*dg + a)
ba.sort()

d = [10**9]*(h+1)
d[0] = 0

for i in range(1, h+1):
    for e in ba:
        a,b = e%dg, e//dg
        if i<=a:
            if d[i] > b:
                d[i] = b
        else:
            if d[i] > d[i-a]+b:
                d[i] = d[i-1]+b
print(d[-1])

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())

A=[]
B=[]

for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[1000000000]*(H+max(A)+1)


for i in range(N):
    DP[A[i]]=B[i]

for i in range(N):
    for h in range(A[i],H+max(A)+1):
        DP[h]=min(DP[h-A[i]]+B[i],DP[h])

print(min(DP[H:]))

Submission status: Wrong Answer

Solution code:
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,weight,value)
 
print(dp[N][H])

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())

inf = float('inf')
dp = [inf] * (H + 1)
dp[0] = 0
for i in range(N):
    A, B = map(int, input().split())
    for j in range(H):
        t1 = j + A
        if t1 > H:
            t1 = H
        t2 = dp[t1]
        if t2 == inf:
            continue
        t2 += B
        if t2 < dp[t1]:
            dp[t1] = t2
print(dp[H])

Submission status: Wrong Answer

Solution code:
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 w == W:
        dp[i+1][w]=min(dp[i+1][w-weight[i]:])+value[i]
      elif 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[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(a)

Submission status: Wrong Answer

Solution code:
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(min(dp[i+1][w-weight[i]:])+value[i], dp[i][w])
      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(a)

Submission status: Wrong Answer

Solution code:
def knapsack(N,W,weight,value):
  #初期化
  dp=[[10**8 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 weight[i]<=w: #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[N][W]
 
H,N = map(int,input().split())
import math
 
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))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[10**4+1]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho>DP[dpi-Ai]+Bi:
                kouho=DP[dpi-Ai]+Bi
    DP[dpi]=kouho


print(min(DP[H-1:]))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho>DP[dpi-Ai]+Bi:
                kouho=DP[dpi-Ai]+Bi
    DP[dpi]=kouho


print(min(DP[H-1:]))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    #kouho=[]
    #kouho.append(DP[dpi])
    kouho=DP[dpi]
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            if kouho>DP[dpi-Ai]+Bi:
                kouho=DP[dpi-Ai]+Bi

            #kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=kouho


print(min(DP[H-1:]))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai,Bi in zip(A,B):#すでにあるものを埋める
    DP[Ai-1]=Bi

for dpi in range(H+max(A)-1):
    kouho=[]
    kouho.append(DP[dpi])
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=min(kouho)


print(min(DP[H-1:]))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai in A:#すでにあるものを埋める
    DP[Ai-1]=Ai 

for dpi in range(H+max(A)-1):
    kouho=[]
    kouho.append(DP[dpi])
    for Ai,Bi in zip(A,B):
        if dpi-Ai>=0:
            kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=min(kouho)


print(min(DP[H-1:]))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

DP=[10**9]*(H+max(A)-1)#DPの箱をH+max(A)-1個用意する

for Ai in A:#すでにあるものを埋める
    DP[Ai-1]=Ai 

for dpi in range(H+max(A)-1):
    kouho=[]
    for Ai,Bi in zip(A,B):
        kouho.append(DP[dpi])
        if dpi-Ai>=0:
            kouho.append(DP[dpi-Ai]+Bi)
    DP[dpi]=min(kouho)


print(min(DP[H-1:]))

Submission status: Wrong Answer

Solution code:
import numpy as np
H,N=map(int,input().split())
ABlist=[[0]*2 for _ in range(N)]
for i in range(N):
    ABlist[i][0],ABlist[i][1]=map(int,input().split())
AB=np.array(ABlist)
A=AB[:,0]
B=AB[:,1]
Z=[0]+[10**8]*H
z=[0]*(H+1)
for i in range(1,H+1):
    for a,b in zip(A,B):
        k=i-a
        if k<=0:
            z[i]=b
        else:
            z[i]+=Z[k]+b
        if z[i]<Z[i]:Z[i]=z[i]


print(Z[H])

Submission status: Wrong Answer

Solution code:
h, n = map(int, input().split())
dp = [10**10]*(h+1)
dp[0] = 0
for _ in range(n):
    a, b = map(int, input().split())
    for end in range(1, h+1):
        start = end - a
        if start < 0:
            continue
        dp[end] = min(dp[end], dp[start]+b)
print(dp[h])

Submission status: Wrong Answer

Solution code:
import math
import copy
H,N=map(int,input().split())

dp=[0]+[None]*H

for i in range(N):
    A,B=map(int,input().split())
    ndp=[0]+[None]*H
    for j in range(A,H+1):
        if dp[j]==None:
            if ndp[j-A]!=None:
                ndp[j]=ndp[j-A]+B
            continue
        
        if ndp[j-A]==None:
            if dp[j]!=None:
                ndp[j]=dp[j]
            
            continue

        ndp[j]=min(ndp[j-A]+B,dp[j])

    for j in range(max(H-A+1,0),H+1):
        if ndp[j]==None:
            continue

        if ndp[-1]!=None:
            ndp[-1]=min(ndp[j]+B,ndp[-1])
        else:
            ndp[-1]=ndp[j]+B

    dp=ndp

print(dp[-1])

Submission status: Wrong Answer

Solution code:
import math
import copy
H,N=map(int,input().split())

dp=[0]+[None]*H

for i in range(N):
    A,B=map(int,input().split())
    ndp=[0]+[None]*H
    for j in range(A,H+1):
        if dp[j]==None:
            if ndp[j-A]!=None:
                ndp[j]=ndp[j-A]+B
            continue
        
        if ndp[j-A]==None:
            if dp[j]!=None:
                ndp[j]=dp[j]
            
            continue

        ndp[j]=min(ndp[j-A]+B,dp[j])

    #for j in range(H-A+1,H+1):
    #    if ndp[j]==None:
    #        continue

    #    if ndp[-1]!=None:
    #        ndp[-1]=min(ndp[j]+B,ndp[-1])
    #    else:
    #        ndp[-1]=ndp[j]+B

    dp=ndp

print(dp[-1])

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
INF = 10 ** 12

M = []
for i in range(N):
    A, B = map(int, input().split())
    M.append((A, B))

dp = [INF for j in range(H+10**4+1)]
dp[0] = 0

for i in range(N):
    for j in range(1, H+10**4+1):
        if j - M[i][0] >= 0:
            dp[j] = min(dp[j], dp[j-M[i][0]] + M[i][1])

print(dp[H])

Submission status: Wrong Answer

Solution code:
h,n,*t=map(int,open(0).read().split())
d=[0]+[10**18]*h
print(d)
print(*[iter(t)])
for a,b in zip(*[iter(t)]*2):
    for j in range(h+1):
        k=j-a
        if k<0:k=0
        c=d[k]+b
        if c<d[j]:d[j]=c
print(d[h])

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
spells = [tuple(map(int,input().split())) for _ in range(N)]
max_a = max(spells,key=lambda t: t[0])[0]

dp = [10000000000 for _ in range(H + max_a + 1)]
dp[0] = 0

for i in range(1, H + max_a + 1):
  for spell in spells:
    if spell[0] <= i:
      dp[i] = min(dp[i], dp[i - spell[0]] + spell[1])

for i in range(H, H + max_a + 1):
  if dp[i] != 10000000000:
    print(dp[i])
    break

Submission status: Wrong Answer

Solution code:
# input
HP, n, *magics = map( int, open( 0 ).read().split() )

# DP wo tsukau
d = [ 0 ] + [ 10**7 ] * HP # |N| * |B| kuraide ok?

# fight! TOKI!
for attach, mp in zip( *[ iter( magics )] * 2 ):
  for i in range( HP + 1 ):
    d[ i ] = min( d[ i ], d[ max( 0, i - attach ) ] + mp )
print( d[ HP ] )

Submission status: Wrong Answer

Solution code:
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=100000000)
def fx(H):
  if H <= 0:
    return 0
  arr = []
  for i in range(len(atk)):
    kaisuu = H // atk[i]
    if kaisuu > 1:
      c = H - atk[i]*kaisuu
      b = fx(c) + MP[i]*kaisuu
      arr.append(b)   
    else:
      b = fx(H - atk[i]) + MP[i]
      arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Submission status: Wrong Answer

Solution code:
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)):
    kaisuu = H // atk[i]
    if kaisuu > 1:
      c = H - atk[i]*kaisuu
      b = fx(c) + MP[i]*kaisuu
      arr.append(b)   
    else:
      b = fx(H - atk[i]) + MP[i]
      arr.append(b)
  return min(arr)

a = fx(H)
print(a)

Submission status: Wrong Answer

Solution code:
def IN(): return map(int,input().split())
ans = pow(2,31); mod = pow(10,9)+7
h,n=IN()
A=[]; dp=[pow(2,20)]*(h+10000)
for i in range(n):
  x,y=IN()
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    dp[i+a]=min(dp[i+a],dp[i]+b)
for i in range(h,h+10000):
  ans=min(ans,dp[i])
print(ans)

Submission status: Wrong Answer

Solution code:
def knapsack(N,W,weight,value,per):
  #初期化
  dp=[[10**4 for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1):
    dp[0][i]= i*per
  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #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[N][W]

H,N = map(int,input().split())
import math

weight=[]
value=[]
per = 0
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
    per = math.ceil(max(int(a)/int(n),per))
a = knapsack(N,H,weight,value,per)

print(int(a))

Submission status: Wrong Answer

Solution code:
def knapsack(N,W,weight,value,per):
  #初期化
  dp=[[10**7 for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1):
    dp[0][i]= i*per
  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #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[N][W]

H,N = map(int,input().split())

weight=[]
value=[]
per = 0
for i in range(N):
    n,a=input().split()
    weight.append(int(n))
    value.append(int(a))
    per = max(int(a)/int(n),per) // 1 +1
a = knapsack(N,H,weight,value,per)

print(int(a))

Submission status: Wrong Answer

Solution code:
def knapsack(N,W,weight,value):
  #初期化
  dp=[[10**7 for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1):
    dp[0][i]=i
  #DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: #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[N][W]

weight = [1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
value = [1,1,1,1,1,1,1,1,1,1,1,1,1]

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(a)

Submission status: Wrong Answer

Solution code:
def read_int_n():
    return list(map(int, input().split()))

hn=read_int_n()
H=hn[0]
N=hn[1]

A=[]
B=[]
for i in range(N):
    a,b = map(int, input().split())
    A.append(int(a))
    B.append(int(b))

#初期化
inf= float('inf')
dp=[inf for i in range(H+max(A)+1)] #HP+最大魔法分-1
for j in range(N):
  dp[A[j]]=B[j]
  
#DP
for i in range(H): #iダメージを与えた状態に対して
  for j in range(N): #全魔法を試す
    dp[i+A[j]]=min(dp[i+A[j]],dp[i]+B[j]) #すでにinf以外が入っていた場合と、試した魔法を加えたときの小さい方を更新

ans=min(dp[H:])
print(ans)

Submission status: Wrong Answer

Solution code:
h,n,*ab=map(int,open(0).read().split())
max_a = max(ab[::2])
ans_list = [0]*max_a + [1e8]*h
for i in range(max_a,max_a+h):
    for a,b in zip(ab[::2],ab[1::2]):
        ans_list[i] = min(ans_list[i],ans_list[i-a]+b)
print(ans_list[h])

Submission status: Wrong Answer

Solution code:
h,n,*ab=map(int,open(0).read().split())
ans_list = [0] + [1e8]*h
for i in range(1,h+1):
    for a,b in zip(ab[::2],ab[1::2]):
        if a > i:
            ans_list[i] = min(ans_list[i], b)
        else:
            ans_list[i] = min(ans_list[i],ans_list[i-a]+b)
print(ans_list[h])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

H, N = map(int, input().split())
AB = []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB = sorted(AB, key=lambda x: (x[0], x[1]))

INF = 10**20
dp = [INF for i in range(H + 1)]
dp[0] = 0
for i in range(1, H + 1):
    if dp[i] != INF:
        continue
    min_mp_cost = INF
    for damage, mp_cost in AB:
        parent = i - damage
        if parent <= 0:
            new_cost = mp_cost
            if new_cost < min_mp_cost:
                min_mp_cost = new_cost
            break
        else:
            new_cost = dp[parent] + mp_cost
            if new_cost < min_mp_cost:
                min_mp_cost = new_cost
    dp[i] = min_mp_cost
print(dp[-1])

Submission status: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()

H, M = list(map(int, lines[0].split()))
AB = []

max_at = 0
min_mg = sys.maxsize
at_min_mg = 0
for i in range(M):
  t = tuple(map(int, lines[i+1].split()))
  AB.append(t)
  if t[0]>=max_at:
    max_at = t[0]
  if t[1]<=min_mg:
    min_mg = t[1]
    at_min_mg = t[0]

ABlen = len(AB)

DP = [0]
for i in range(1, at_min_mg+1):
  DP.append(min_mg)
  
for i in range(at_min_mg+1,H+max_at+1):
  mm = 99999999
  for j in range(ABlen):
    if i-AB[j][0]<0:
      continue
    if mm > DP[i-AB[j][0]]+AB[j][1]:
      mm = DP[i-AB[j][0]]+AB[j][1]
  DP.append(mm)
print(min(DP[H:H+max_at]))

Submission status: Wrong Answer

Solution code:
h,n = map(int,input().split())
l = []
k = 0
for i in range(n):
    a,b = map(int,input().split())
    l.append([a,b])
    k += a
ans = 0
i = 0
dp = [0 for i in range(h+1)]
for i in range(n):
    a,b = l[i]
    for j in range(h):
        if j+a <= h:
            if j == 0:
                if dp[j+a] == 0:
                    dp[j+a] = b
                else:
                    dp[j+a] = min(b,dp[j+a])
            else:
                if dp[j] != 0:
                    if dp[j+a] == 0:
                        dp[j+a] = dp[j] + b
                    else:
                        dp[j+a] = min(dp[j]+b,dp[j+a])
        else:
            if dp[j] != 0:
                if dp[h] == 0:
                    dp[h] = dp[j]+b
                else:
                    dp[h] = min(dp[h],dp[j]+b)
print(dp[h])

Submission status: Wrong Answer

Solution code:
import math


def inputIntList():
  return [int(s) for s in input().split()]


def inputInt():
  return int(input())


inf = float('inf')


def main():
  H, N = inputIntList()
  AB = [inputIntList() for _ in range(N)]
  dp = [inf for _ in range(H + 1)]

  dp[0] = 0
  for (a, m) in AB:
    for h in range(H):
      nxt = min(H, h+a)
      dp[nxt] = min(dp[h]+m, dp[nxt])
  print(dp)
  return dp[H]


if __name__ == "__main__":
  print(main())

Submission status: Wrong Answer

Solution code:
h, n = map(int, input().split())
ab = [map(int, input().split()) for _ in range(n)]

dp = [[float("inf")] * (h + 1) for _ in range(n + 1)]
for i in range(n + 1):
    dp[i][0] = 0

for i, (a, b) in enumerate(ab, 1):
    for j in range(h + 1):
        if j - a >= 0:
            dp[i][j] = min(dp[i-1][j], dp[i][j-a] + b)
        else:
            dp[i][j] = dp[i-1][j]

ans = dp[n][h]
if ans == float("inf"):
    for e in dp[n][::-1]:
        if e != float("inf"):
            ans = e + 1
            break

print(ans)

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())

A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)

maxA=A[A.index(max(A))]

DP=[10000000000]*(H+1+maxA)
DP[0]=0

for i in range(N):
    DP[A[i]]=B[i]

for i in range(A.index(min(A)),H+1+maxA):
    temp=[]
    for j in range(i+1):
        temp.append(DP[j]+DP[i-j])
    DP[i]=min(temp)

print(min(DP[H:]))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())

A=[]
B=[]
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
minA=A[A.index(min(A))]
minB=B[B.index(min(B))]
maxA=A[A.index(max(A))]

DP=[10000000000]*(H+1+maxA)
DP[0]=0

for i in range(N):
    DP[A[i]]=B[i]

for i in range(1,A.index(min(A))):
    DP[i]=minB

for i in range(A.index(min(A)),H+1+maxA):
    temp=[]
    for j in range(i+1):
        temp.append(DP[j]+DP[i-j])
    DP[i]=min(temp)

print(min(DP[H:]))

Submission status: Wrong Answer

Solution code:
H, N = (int(i) for i in input().split())
a_b = [[int(i) for i in input().split()] for i in range(N)]
#最大の力
max_power = 0
for i in range(N):
    if a_b[i][0] > max_power:
        max_power = a_b[i][0]
p_inf = float('inf') # 正の無限大
dp = [[p_inf] * (H+max_power+10) for i in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
dp[0][0] = 0
for i in range(N):
    for j in range(H+max_power):
        if j + a_b[i][0] < H+max_power+5: #相手に与えるダメージがHよりも小さいとき
            dp[i+1][j+ a_b[i][0]] = min(dp[i+1][j]+a_b[i][1],dp[i][j+ a_b[i][0]])
        else:
            dp[i+1][H+1+max_power+5] = dp[i][H+1+max_power+5]
print(min(dp[N][H:]))

Submission status: Wrong Answer

Solution code:
h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
dp=[10000000]*(h+1)
dp[h]=0
for i in range(0,h+1)[::-1]:
    for j in range(n):
        dp[max(0,i-a[j])]=min(dp[max(0,i-a[j])],dp[i]+b[j])

print(dp[0])

Submission status: Wrong Answer

Solution code:
H, N = (int(i) for i in input().split())
a_b = [[int(i) for i in input().split()] for i in range(N)]
#最大の力
max_power = 0
for i in range(N):
    if a_b[i][0] > max_power:
        max_power = a_b[i][0]
p_inf = float('inf') # 正の無限大
dp = [[p_inf] * (H+max_power+2) for i in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
dp[0][0] = 0
for i in range(N):
    for j in range(H+max_power):
        if j + a_b[i][0] < H+max_power: #相手に与えるダメージがHよりも小さいとき
            dp[i+1][j+ a_b[i][0]] = min(dp[i+1][j]+a_b[i][1],dp[i][j+ a_b[i][0]])
        else:
            dp[i+1][H+1+max_power] = dp[i][H+1+max_power]
print(min(dp[N][H:]))

Submission status: Wrong Answer

Solution code:
H, N = (int(i) for i in input().split())
a_b = [[int(i) for i in input().split()] for i in range(N)]
#最大の力
max_power = 0
for i in range(N):
    if a_b[i][0] > max_power:
        max_power = a_b[i][0]
p_inf = float('inf') # 正の無限大
dp = [[p_inf] * (H+max_power+2) for i in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
dp[0][0] = 0
for i in range(N):
    for j in range(H+max_power):
        if j + a_b[i][0] < H+max_power: #相手に与えるダメージがHよりも小さいとき
            dp[i+1][j+ a_b[i][0]] = min(dp[i+1][j]+a_b[i][1],dp[i][j+ a_b[i][0]])
        else:
            dp[i+1][H+1+max_power] = dp[i][H+1+max_power]
min(dp[N][H:])

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

# 最初に入力の最大値と最大量でdp配列を作成
dp = [100000000] * (H + 10 ** 4 + 1)


for i in range(H):
    if dp[i] == 100000000:
        continue
    for a, b in AB:
        # tは100000000 + 消費魔力
        t = dp[i] + b
        # dp[i + a] aはダメージ量
        if t < dp[i + a]:
            dp[i + a] = t

print(min(dp[H:]))

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
H,N = (int(x) for x in input().split())
A = np.array([0]*N)
B = np.array([0]*N)
C = np.array([0.]*N)
for i in range(N):
    A[i],B[i] = (int(x) for x in input().split())
    C[i] = float(A[i]) / float(B[i])

indexs = np.argsort(-C)
A=A[indexs]
B=B[indexs]
C=C[indexs]

V=[[0]*(H+1) for i in range(N+1)]
for h in range(H+1):
    V[0][h] = B[0]*math.ceil(h/A[0])

for i  in range(1,N):
    for h in range(H+1):
        if h < A[i]:
            V[i+1][h] = V[i][h]
        elif V[i][h] > (V[i][h-A[i]]+B[i]):
            V[i+1][h] = (V[i][h-A[i]]+B[i])
        else:
            V[i+1][h] = V[i][h]

print(V[N][H])

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
  H, N = map(int, input().split())
  damage= []
  cost = []
  for i in range(N):
    a,b = map(int, input().split())
    damage.append(a)
    cost.append(b)

  damage=np.array(damage)
  cost=np.array(cost)
  E = damage/cost
  best = E.argmax() 

  ma= math.ceil(H/damage[best]) * cost[best]
  count = np.array([0]*N)
  count[best] = math.ceil(H/damage[best])

  def search(count, damage, cost, E, H, at):
#    at= count.argmax()
    count[at] = count[at]-1
    n= len(count)
    tmp_c_lists =np.array([count]*n)
    sum_list = np.array([0]*n)

    for i in range(n):
      while True:
        tmp_c_lists[i][i] = tmp_c_lists[i][i]+1
        if np.sum(tmp_c_lists[i]* damage) > H:
          break
      sum_list[i] = np.sum(cost*tmp_c_lists[i])
    arg = np.argmin(sum_list)

    return np.sum(tmp_c_lists[arg]* cost), tmp_c_lists[arg]

  while True:
    new, count = search(count, damage, cost, E, H, best)
    if ma <= new:
      break
    else:
      ma =new
  print(ma)
  
if __name__=="__main__":
  main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
  H, N = map(int, input().split())
  damage= []
  cost = []
  for i in range(N):
    a,b = map(int, input().split())
    damage.append(a)
    cost.append(b)

  damage=np.array(damage)
  cost=np.array(cost)
  E = damage/cost
  best = E.argmax() 

  ma= math.ceil(H/damage[best]) * cost[best]
  count = np.array([0]*N)
  count[best] = math.ceil(H/damage[best])

  def search(count, damage, cost, E, H, at):
#    at= count.argmax()
    count[at] = count[at]-1
    n= len(count)
    tmp_c_lists =np.array([count]*n)
    sum_list = np.array([0]*n)

    for i in range(n):
      while True:
        tmp_c_lists[i][i] = tmp_c_lists[i][i]+1
        if np.sum(tmp_c_lists[i]* damage) >= H:
          break
      sum_list[i] = np.sum(cost*tmp_c_lists[i])
    arg = np.argmin(sum_list)

    return np.sum(tmp_c_lists[arg]* cost), tmp_c_lists[arg]

  while True:
    new, count = search(count, damage, cost, E, H, best)
    if ma <= new:
      break
    else:
      ma =new
  print(ma)
  
if __name__=="__main__":
  main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
  H, N = map(int, input().split())
  damage= []
  cost = []
  for i in range(N):
    a,b = map(int, input().split())
    damage.append(a)
    cost.append(b)

  damage=np.array(damage)
  cost=np.array(cost)
  E = damage/cost
  best = E.argmin() 

  ma= math.ceil(H/damage[best]) * cost[best]
  count = np.array([0]*N)
  count[best] = math.ceil(H/damage[best])

  def search(count, damage, cost, E, H):
    at= count.argmax()
    count[at] = count[at]-1
    n= len(count)
    tmp_c_lists =np.array([count]*n)
    sum_list = np.array([0]*n)

    for i in range(n):
      while True:
        tmp_c_lists[i][i] = tmp_c_lists[i][i]+1
        if np.sum(tmp_c_lists[i]* damage) >= H:
          break
      sum_list[i] = np.sum(cost*tmp_c_lists[i])
    arg = np.argmin(sum_list)

    return np.sum(tmp_c_lists[arg]* cost), tmp_c_lists[arg]

  while True:
    new, count = search(count, damage, cost, E, H)
    if ma <= new:
      break
    else:
      ma =new
  print(ma)
  
if __name__=="__main__":
  main()

Submission status: Wrong Answer

Solution code:
# ref https://atcoder.jp/contests/abc153/submissions/9758833
# ref http://drken1215.hatenablog.com/entry/2020/01/26/225000

# coding: utf-8
H, N = map(int, input().split())
A = [0] * N
B = [0] * N

for i in range(N):
    A[i], B[i] = (list(map(int, input().split())))

dp = [[float('inf') for i in range(H+1)] for j in range(N+1)]

for i in range(N+1):
    dp[i][0] = 0

for i in range(1, N):
    for j in range(1, H):
        dp[i][j] = min(dp[i-1][j],
                       dp[i-1][max(j-A[i], 0)] + B[i],
                       dp[i][max(j-A[i], 0)] + B[i])
        #print(dp[i-1][j],dp[i-1][max(j-AB[i][0],0)] + AB[i][1],dp[i][max(j-AB[i][0],0)] + AB[i][1])


print(dp[N-1][H-1])

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
H,N = (int(x) for x in input().split())
A = np.array([0]*N)
B = np.array([0]*N)
C = np.array([0.]*N)
for i in range(N):
    A[i],B[i] = (int(x) for x in input().split())
    C[i] = float(A[i]) / float(B[i])

indexs = np.argsort(-C)
A=A[indexs]
B=B[indexs]
C=C[indexs]

V=[[0]*(H+1) for i in range(N+1)]
for h in range(H+1):
    V[0][h] = B[0]*math.ceil(h/A[0])

for i  in range(N):
    for h in range(H+1):
        if h < A[i]:
            V[i+1][h] = V[i][h]
        elif V[i][h] > (V[i][h-A[i]]+B[i]):
            V[i+1][h] = (V[i][h-A[i]]+B[i])
        else:
            V[i+1][h] = V[i][h]

print(V[N][H])

Submission status: Wrong Answer

Solution code:
print(1)

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
T = [0] * N
M = [0] * N
for i in range(N):
    T[i], M[i] = map(int, input().split())

inf = 10 ** 9

dp = [[inf for j in range(H + 1)] for i in range(N + 1)]

for i in range(1, H + 1):
    if i % T[0] == 0:
        dp[1][i] = (i // T[0]) * M[0]
    else:
        dp[1][i] = (i // T[0] + 1) * M[0]

for i in range(1, N ):
    for j in range(H + 1):
        if j - T[i] >= 0:
            dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - T[i]]+ M[i])
        else:
            dp[i + 1][j] = dp[i][j]

print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
import bisect

h, n = map(int, input().split())
ab = []
for _ in range(n):
    *l, = map(int, input().split())
    ab.append(l)

ab.sort()

dp = [[float("inf")]*(h+1) for _ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0

for i in range(n):
    for j in range(h+1):
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        if ab[i][0] <= j:
            dp[i+1][j] = min(dp[i+1][j-ab[i][0]]+ab[i][1], dp[i+1][j])

if dp[n][h] != float("inf"):
    print(dp[n][h])
else:
    d = 0
    for i in reversed(range(h)):
        if dp[n][i] != float("inf"):
            d = i
            break

    index = bisect.bisect_left(ab, [h-d, float("inf")])
    ans = dp[n][d] + ab[index][1]
    print(ans)

Submission status: Wrong Answer

Solution code:
h, n = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]
INF = 10 ** 6
dp = [[INF] * (h + 10 ** 4 + 100) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
    a, b = AB[i]
    for j in range(h + 10 ** 4 + 100):
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j])
        if j >= a:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - a] + b)
# for d in dp:
#     print(*d)

ans = INF
for j in range(h, h + 10 ** 4 + 100):
    ans = min(ans, dp[n][j])
print(ans)

Submission status: Wrong Answer

Solution code:
# input
H, N = map(int, input().split())
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

# process
max_a = max(A)
dp = [10**9]*(H+max_a)
dp[0] = 0

for i in range(len(dp)):
    if dp[i] == 10**4:
        continue
    for j in range(N):
        idx = i+A[j]
        if idx < len(dp) and dp[i]+B[j] < dp[idx]:
            dp[i+A[j]] = dp[i]+B[j]

print(dp)

# output
print(min(dp[H:]))