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

Solution (Python):
h,n = map(int, input().split())
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append([a,b])

dp = [0]*(h+1)

for i in range(1,h+1):
    mn = 10**9
    for j in ab:
        hp = j[0]
        mp = j[1]  
        if i-hp<0:
            mn = min(mp,mn)
        else:
            mn = min(dp[i-hp]+mp, mn)
        dp[i] = mn

print(dp[h])

Solution (Python):
import sys
import math
from collections import deque
from collections import defaultdict
INF = 10**9

def main():
    h, n = list(map(int,sys.stdin.readline().split()))
    magic = [list(map(int,sys.stdin.readline().split())) for _ in range(n)]
    
    max_a = max(a for a, b in magic)
    dp = [0] * (h + max_a + 1)
    for i in range(1, h + max_a):
        dp[i] = min(dp[i - a] + b for a, b in magic)
        
    print(dp[h])

    return 0

if __name__ == "__main__":
    main()

Solution (Python):
import sys















sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    h, n = ns()
    d = [na() for _ in range(n)]

    mat = [INF for _ in range(h + 1)]

    mat[0] = 0
    for i in range(h):
        if mat[i] == INF:
            continue
        for a, b in d:
            if i + a < h + 1:
                mat[i + a] = min(mat[i + a], mat[i] + b)
            else:
                mat[h] = min(mat[h], mat[i] + b)

    print(mat[h])


if __name__ == '__main__':
    main()

Solution (Python):
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=[[-1 for i in range(H+1)] for i in range(N)]
dp[0][0]=0
for i in range(N):
    dp[i][0]=0
tmp=0
while True:
    if tmp>=H:
        break
    if dp[0][tmp]!=-1:
        if tmp+A[0]>H:
            dp[0][H]=dp[0][tmp]+B[0]
        else:
            dp[0][tmp+A[0]]=dp[0][tmp]+B[0]
    tmp+=1
for i in range(N-1):
    tmp=0
    for j in range(H+1):
        dp[i+1][j]=dp[i][j]
    while True:
        if tmp>=H:
            break
        if dp[i+1][tmp]!=-1:
            if tmp+A[i+1]>=H:
                dp[i+1][H]=min(dp[i+1][H],dp[i+1][tmp]+B[i+1])
            else:
                if dp[i+1][tmp+A[i+1]]==-1:
                    dp[i+1][tmp+A[i+1]]=dp[i+1][tmp]+B[i+1]
                else:
                    dp[i+1][tmp+A[i+1]]=min(dp[i+1][tmp]+B[i+1], dp[i+1][tmp+A[i+1]])
        tmp+=1
print(dp[N-1][H])

Solution (Python):
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter

from functools import lru_cache

sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


h, n = inpl()
data = [inpl() for i in range(n)]

dp = [[INF] * (h + 1) for _ in range(n + 1)]
dp[0][0] = 0
for i in range(n):
    for j in range(h + 1):
        unused = dp[i][j]
        used = dp[i][j - data[i][0]] + data[i][1] if j - data[i][0] >= 0 else data[i][1]
        reused = dp[i + 1][j - data[i][0]] + data[i][1] if j - data[i][0] >= 0 else INF
        dp[i + 1][j] = min(unused, used, reused)
ans = dp[n][h]
print(ans)

Solution (Python):


import math
H, N = map(int, input().split())
AB = []
max_A = 0
for i in range(N):
    ab = list(map(int, input().split()))
    if max_A < ab[0] :
        max_A = ab[0]
    AB.append(ab)

 
DP = [0] * (H + max_A)
for i in range(1, H + max_A ): 
    tmp_mini = 10 ** 8
    for j in range(N):
        if i - AB[j][0] >= 0:
            tmp = DP[i - AB[j][0]] + AB[j][1]
            if tmp_mini > tmp:
                tmp_mini = tmp
    DP[i] = tmp_mini
print(min(DP[H: H + max_A]))

Solution (Python):
h,n = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
inf = 10000000000
dp = [inf for _ in range(h+1)]
dp[0] = 0
for i in range(1,h+1):
    m = inf
    for a,b in ab:
        if i <= a:
            m = min(m,b)
        else:
            m = min(m,dp[i-a]+b)
    dp[i] = m
print(dp[-1])

Solution (Python):



H,N = map(int,input().split())
dp = [float("INF")]*(H+1)
dp[0] = 0
for i in range(N):
    A,B = map(int,input().split())
    for j in range(1,H+1):
        dp[j] = min(dp[j],dp[max(j-A,0)]+B)

print(dp[-1])

Solution (Python):
def solve(h,n,a,b):
    dp = [[0 for j in range(h+1)] for i in range(n+1)]

    for i in range(n):
        for j in range(h+1):
            if j <= a[i] and i == 0:
                dp[i+1][j] = b[i]
            elif i == 0:
                dp[i+1][j] = dp[i+1][j-a[i]]+b[i]
            elif j <= a[i]:
                dp[i+1][j] = min(dp[i][j],b[i])
            else:
                dp[i+1][j] = min(dp[i][j],dp[i+1][j-a[i]]+b[i])
    return dp[n][h]

h, n = map(int,input().split())
a = []
b = []

for i in range(n):
    ai,bi = map(int,input().split())
    a.append(ai)
    b.append(bi)

print(solve(h,n,a,b))

Solution (Python):
h , n = map(int,input().split())
maho = []
for i in range(n):
    a,b = map(int,input().split())
    maho.append((a,b))
dp = [10**8 for i in range(h+1)]
for i in range(1,h+1):
    for a,b in maho:
        if i <= a:
            dp[i] = min(dp[i],b)
        elif i > a:
            dp[i] = min(dp[i],dp[i-a]+b)

print(dp[h])

Solution (Python):
HN = list(map(int, input().split()))
H = HN[0]; N = HN[1]
AB = [list(map(int, input().split())) for _ in range(N)]
 
DP = [[0]*H for i in range(N)]
 
for i in range(H):
    DP[0][i] = int((1+i//AB[0][0])*AB[0][1])
 
for i in range(1,N):
    for j in range(H):
        if j<AB[i][0]:
            DP[i][j] = int(min([DP[i-1][j], AB[i][1]]))
        else:
            DP[i][j] = min([DP[i-1][j], DP[i][j-AB[i][0]] + AB[i][1]])
 
print(int(DP[N-1][H-1]))

Solution (Python):
h,n=map(int,input().split())

ab=[]
dp=[10**9]*(10**5)

maxA=0
for _ in range(n):
    a,b=map(int,input().split())
    dp[a]=min(dp[a],b)
    maxA=max(maxA,a)


result=10**9
for i in range(1,h+maxA):
    
    for k in range(1,i//2+2):
        dp[i]=min(dp[i],dp[k]+dp[i-k])
    if i>=h:
        result=min(result,dp[i])
    
print(result)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    a_max=np.max(lis[:,0])
    dp=np.zeros(h+a_max+1,int)
    for i in range(a_max+1,a_max+h+1):
        dp[i]=np.min(dp[i-lis[:,0]]+lis[:,1])
    print(dp[a_max+h])

if __name__=='__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
mx = max([a for a,b in ab])
dp = [[float('inf')]*(H+1+mx) for _ in range(N+1)]

dp[0][0] = 0

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

ans = float('inf')
for i in range(H, H+mx+1):
  ans = min(ans, dp[N][i])
print(ans)

Solution (Python):


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

INF = float("inf")
dp = [[INF for _ in range(H+1)] for _ in range(N+1)]
dp[0][0] = 0


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

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

Solution (Python):


from functools import lru_cache
import sys
sys.setrecursionlimit(200000)

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

AB_dict = dict() 

for i in range(N): 
    A,B = map(int, input().split())
    if A not in AB_dict:
        AB_dict[A] = B
    else:
        if B < AB_dict[A]: 
            AB_dict[A] = B

@lru_cache(maxsize=None)
def get_consumption_MP(HP): 
    ans = 10**9 + 7
    
    for AB in AB_dict.items():
        A = AB[0]
        B = AB[1]
        
        if A >= HP:
            if B < ans:
                ans = B
        else:
            if B + get_consumption_MP(HP-A) < ans:
                ans = B + get_consumption_MP(HP-A)
                
    return ans
        
print(get_consumption_MP(H))

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

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

print(min(dp[H:H+M+1]))

Solution (Python):
import sys
h, n = map(int, input().split())


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


min_hp = h
 
dp = [sys.maxsize] * (min_hp + 1)
dp[0] = 0
 
for i in range(0, min_hp + 1):
    for ab in AB: 
        next_index = min(i + ab[0], min_hp)
        dp[next_index] = min(dp[next_index], dp[i] + ab[1])
 









print(dp[h])

Solution (Python):
import sys
h, n = map(int, input().split())




AB = [list(map(int, input().split())) for _ in range(n)]
min_hp = h
max_hp = h + max(a for a, b in (AB))
 
dp = [sys.maxsize] * (max_hp + 1)
dp[0] = 0
 
for i in range(1, max_hp + 1):
    
    dp[i] = min(dp[i - a] + b for a, b in AB)
print(min(dp[h:]))

Solution (Python):
import sys
h, n = map(int, input().split())
A = [0] * n
B = [0] * n
for i in range(n):
    A[i], B[i] = map(int, input().split())
min_hp = h
max_hp = h + max(A)
 
dp = [sys.maxsize] * (max_hp + 1)
dp[0] = 0
 
for i in range(1, max_hp + 1):
    dp[i] = min(dp[i - A[j]] + B[j] for j in range(n))
print(min(dp[h:]))

Solution (Python):


H, N = (int(x) for x in input().split())

a = [0 for x in range(N)]
b = [0 for x in range(N)]
for i in range(N):
	a[i], b[i] = (int(x) for x in input().split())

inf = 10000000000
max_d = H + max(a)
dp = [[inf for x in range(max_d)] for x in range(N)]

dp[0][0] = 0

for i in range(N):
	for j in range(max_d):
		if i >= 1:
			dp[i][j] = min(dp[i][j], dp[i-1][j])
		if j - a[i] >= 0:
			dp[i][j] = min(dp[i][j], dp[i][j-a[i]] + b[i])









min_m = inf
for i in range(N):
	for j in range(max_d):
		if j >= H:
			min_m = min(min_m, dp[i][j])

print(min_m)

Solution (Python):
H, N = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N)]
dp = [float('inf')] * 10001
dp[0] = 0
for a, b in AB:
    for i in range(10001 - a):
        dp[i + a] = min(dp[i + a], dp[i] + b)
print(min(dp[H:]))

Solution (Python):
h, n = map(int, input().split())
inf = float("inf")

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



for i in range(n):
  a, b = map(int, input().split())
  for j in range(h+1):  
    dp[i+1][j] = min(dp[i][j], dp[i+1][max(0, j-a)] + b)
"""
for i in range(n+1):
  print(dp[i])
"""
print(dp[n][h])

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    a_max=np.max(lis[:,0])
    dp=np.zeros(h+a_max+1,int)
    for i in range(a_max+1,a_max+h+1):
        dp[i]=np.min(dp[i-lis[:,0]]+lis[:,1])
    print(dp[a_max+h])

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    dp=np.zeros(h+1,int)
    dp[0]=0
    for i in range(1,h+1):
        dp[i]=np.min(dp[np.maximum(i-lis[:,0],0)]+lis[:,1])
    print(dp[h])

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    dp=np.zeros(10**4+1,int)
    dp[0]=0
    for i in range(1,h+1):
        dp[i]=np.min(dp[np.maximum(i-lis[:,0],0)]+lis[:,1])
    print(dp[h])

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    dp=np.zeros(10**4+1,int)
    dp[0]=0
    for i in range(1,h+1):
        dp[i]=np.min(dp[i-np.minimum(lis[:,0],i)]+lis[:,1])
    print(dp[h])

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    dp=np.zeros(10**4+1,int)
    dp[0]=0
    for l in lis:
        dp[1:l[0]+1]=np.minimum(dp[1:l[0]+1],l[1])
    for i in range(1,h+1):
        dp[i]=np.min(dp[i-lis[:,0]]+lis[:,1])
    print(dp[h])

if __name__=='__main__':
    main()

Solution (Python):
import sys
h, n = map(int, input().split())




AB = [list(map(int, input().split())) for _ in range(n)]
min_hp = h
max_hp = h + max(a for a, b in (AB))

dp = [sys.maxsize] * (max_hp + 1)
dp[0] = 0

for i in range(1, max_hp + 1):
    
    dp[i] = min(dp[i - a] + b for a, b in AB)
print(min(dp[h:]))

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





H,N = inputIL()
M = inputILL(N)


cospa = max(a/b for a,b in M)

dp = [[0 for j in range(H+1)] for i in range(N)]
a0, b0 = M[0]
dp[0] = [((j+(a0-1))//a0)*b0 for j in range(H+1)]

for i in range(1,N):
  a,b = M[i]
  for j in range(H+1):
    if j <= a:
      dp[i][j] = min(dp[i-1][j],b)
    else:
      dp[i][j] = min(dp[i-1][j] , dp[i][j-a]+b)

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

Solution (Python):
import sys
sys.setrecursionlimit(1000000) 

def main():
    input = sys.stdin.readline  
    H, N = map(int, input().rstrip().split())
    magic_list = []
    for _ in range(N):
        A, B = map(int, input().rstrip().split())
        magic_list.append((A, B))

    
    dp = [100000000] * (H+1)
    
    dp[0] = 0
    
    for i, magic in enumerate(magic_list):
        damage, mp = magic
        
        
        for hp in range(H+1):
            
            dp[min(H, hp+damage)] = min(dp[min(H, hp+damage)], dp[hp] + mp)
    print(dp[H])

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000) 

def main():
    input = sys.stdin.readline  
    H, N = map(int, input().rstrip().split())
    magic_list = []
    for _ in range(N):
        A, B = map(int, input().rstrip().split())
        magic_list.append((A, B))

    dp = [[100000000] * (H+1) for _ in range(N+1)]
    
    dp[0][0] = 0
    for i, magic in enumerate(magic_list):
        damage, mp = magic
        for hp in range(H+1):
            
            dp[i+1][hp] = min(dp[i+1][hp], dp[i][hp])
            
            dp[i+1][min(H, hp+damage)] = min(dp[i+1][min(H, hp+damage)], dp[i+1][hp] + mp)
    print(dp[N][H])

if __name__ == '__main__':
    main()

Solution (Python):
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
max_a = max(a for a, b in ab)
dp = [0]*(H+max_a)
for i in range(1, H+max_a):
    dp[i] = min(dp[i-a]+b for a, b in ab)
print(dp[H])

Solution (Python):
import numpy as np

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

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

ab = np.array(ab)
a_list = ab[:, 0]
b_list = ab[:, 1]
max_a = ab.max()

inf = float('inf')
dp = np.array([inf for _ in range(H + max_a)])
dp[0] = 0

for i in range(1, len(dp)):
    dp[i] = np.amin(dp[np.maximum(i - a_list, 0)] + b_list)

print(int(min(dp[H:])))

Solution (Python):
m=10**9+7

h,n=map(int,input().split())
a=[0]*n
b=[0]*n
for i in range(n):
    a[i],b[i]=map(int,input().split())

dp=[m]*(h+max(a)+1)
dp[0]=0
for i in range(1,h+max(a)+1):
    k=0
    for j in range(n):
        if i< a[j]:
            k=b[j]
        else:
            k=b[j]+dp[i-a[j]]
        dp[i]=min(k,dp[i])

ans=min(dp[h:h+max(a)+1])

print(ans)

Solution (Python):
import numpy as np
h,n=map(int,input().split())

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

dp=np.zeros(10**4+1,int) 
dp[0]=0

for a,b in ab:
      dp[1:a+1]=np.minimum(dp[1:a+1],b)

for i in range(1,h+1):
  dp[i]=np.min(dp[i-ab[:,0]]+ab[:,1])
print(dp[h])

Solution (Python):
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
max_a = max(a for a, b in ab)
dp = [0]*(H+max_a)
for i in range(1, H+max_a):
    dp[i] = min(dp[i-a]+b for a, b in ab)
print(dp[H])

Solution (Python):
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
max_a = max(a for a, b in ab)
dp = [0]*(H+max_a)
for i in range(1, H+max_a):
    dp[i] = min(dp[i-a]+b for a, b in ab)
print(dp[H])

Solution (Python):
import sys
from io import StringIO
import unittest
import math
from collections import deque


def resolve():
    hp, n = map(int, input().split())
    magics = [list(map(int, input().split())) for i in range(n)]

    max_magic_power = 0
    

    for magic in magics:
        max_magic_power = max(max_magic_power, magic[0])
        

    
    

    
    

    
    
    
    
    
    

    
    que = deque()

    ans = [999999999 for i in range(hp + max_magic_power + 1)]
    ans[0] = 0

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    for i in range(hp + 1):
        
        if ans[i] is 999999999:
            continue

        
        for magic in magics:
            
            
            ans[i + magic[0]] = min(ans[i + magic[0]], ans[i] + magic[1])

    print(min(ans[hp:-1]))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """9 3
8 3
4 2
2 1"""
        output = """4"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """100 6
1 1
2 3
3 9
4 27
5 81
6 243"""
        output = """100"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461"""
        output = """139815"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """10000 1
1 10000
"""
        output = """100000000"""
        self.assertIO(input, output)



if __name__ == "__main__":
    
    resolve()

Solution (Python):
H, N = map(int, input().split())
mahou = []
a_max = 0
for _ in range(N):
    a, b = map(int, input().split())
    mahou.append([a, b])
    a_max = max(a_max, a)

dp = [[10 ** 9 for _ in range(H + a_max)] for _ in range(N + 1)]

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

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

print(min(dp[N][H:]))

Solution (Python):
h, n =map(int, input().split())
dp = [float('inf')] * (h+1)
dp[0] = 0

magics = [list(map(int, input().split())) for i in range(n)]
magics.sort()

for i in range(h+1):
    a = dp[i]
    for attack, magic in magics:
        next_index = min(i+attack, h)
        dp[next_index] = min(dp[next_index], a + magic)

print(dp[h])

Solution (Python):
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for _ in range(N)]
dp = [float('inf')] * (H + 1)
dp[0] = 0
for h in range(H):
    if dp[h] == float('inf'):
        continue
    for a, b in M:
        idx = min(H, h + a)
        dp[idx] = min(dp[idx], dp[h] + b)
print(dp[H])

Solution (Python):
h,n = map(int, input().split())
max_h = 10**4
inf = 10**30
info = []
for i in range(n):
    info.append(list(map(int, input().split())))

import math

mp_sort = sorted(info, key = lambda x : x[1])
damage_sort = sorted(info, key = lambda x : x[0], reverse = True)
max_damage = damage_sort[0][0]
'''
efficiency = [0]*n
for i in range(n):
    efficiency[i] = mp_sort[i][0]/mp_sort[i][1]

max_efficiency = max(efficiency)
max_eff_index = efficiency.index(max_efficiency)

max_eff_attack = mp_sort[max_eff_index][0]
max_eff_mp = mp_sort[max_eff_index][1]
'''
dp = [inf]*(max_h + max_damage + 1)
dp[0] = 0
for i in range(n):
    min_mp = info[i][1]
    tem_damage = info[i][0]
    for j in range(tem_damage+1):
        dp[j] = min(dp[j], min_mp)


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

print(dp[h])

Solution (Python):
def solve(h, n, A, B):
    dp = [float("inf")] * h + [0]
    for i in range(h, -1, -1):
        if dp[i] == float("inf"):
            continue
        for j in range(n):
            if 0 <= i - A[j]:
                dp[i - A[j]] = min(dp[i - A[j]], dp[i] + B[j])
            else:
                dp[0] = min(dp[0], dp[i] + B[j])
    return dp[0]

if __name__ == '__main__':
    h, n = map(int, input().split())
    a = [0] * n
    b = [0] * n

    for i in range(n):
        a[i], b[i] = map(int, input().split())

    print(solve(h, n, a, b))

Solution (Python):
h, n = map(int, input().split())
a_list = []
b_list = []

for _ in range(n):
    a, b = map(int, input().split())
    a_list.append(a)
    b_list.append(b)

max_a = max(a_list)    
dp = [float('inf')] * (h + max_a + 1)

dp[0] = 0
for i in range(h):
    for j in range(n):
        dp[i + a_list[j]] = min(dp[i] + b_list[j], dp[i + a_list[j]])
print(min(dp[h:]))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    INF = 1 << 30

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

    dp = [INF] * (H + 1)
    dp[0] = 0

    for _ in range(N):
        A, B = map(int, input().split())
        for j in range(H):
            nj = min(H, j + A)
            dp[nj] = min(dp[nj], dp[j] + B)
    print(dp[H])


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    INF = 1 << 30

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

    dp = [INF] * (10 ** 4 * 2 + 1)
    dp[0] = 0

    for _ in range(N):
        A, B = map(int, input().split())
        for j in range(H):
            dp[j + A] = min(dp[j + A], dp[j] + B)
    print(min(dp[H:]))


if __name__ == '__main__':
    main()

Solution (Python):
h,n=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n)]
inf=10**10
dp=[inf]*(h+1)
dp[-1]=0

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


h,n=MI()
lis=[LI() for i in range(n)]
mx=sorted(lis,reverse=True)[0][0]

dp=[0 for i in range(h+mx)]
for i in range(1,h+mx):
    mn=inf
    
    for j in range(n):
        u=dp[max(i-lis[j][0],0)]+lis[j][1]
        
        if u<mn:
            mn=u
    dp[i]=mn

z=[]
for i in range(mx):
    z.append(dp[h+i])
print(min(z))

Solution (Python):
h, n = map(int, input().split())
magics = [list(map(int, input().split())) for _ in range(n)]
max_damage = max(a for a, b in magics)
dp = [0] * (h + max_damage)
for i in range(1, h + max_damage):
    dp[i] = min(dp[i - a] + b for a, b in magics)
print(dp[h])

Solution (Python):
h,n = map(int,input().split())

ab_input = [list(map(int,input().split())) for i in range(n)]
max_d = max(a for a,b in ab_input)

dp =[0]*(h+max_d)
for i in range(1,h+max_d):
    dp[i]=min(dp[i-a]+b for a,b in ab_input)
print(dp[h])

Solution (Python):
import numpy as np
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)

A=np.array(A)
B=np.array(B)
dp=np.ones((H+1+10**4))*np.inf
dp[H+1:]=0
dp[0]=0
for i in range(1,H+1):
    
    dp[i]=min(dp[i-A]+B)

print(int(dp[H]))

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  




H, N = read_ints()
A, B = read_col(N)


'''
dp[i][j] ... 与えられるダメージの最大。[0,i)の魔法で、魔力j以下使うとき。
更新則
dp[i+1][j] = max(dp[i][j],dp[i+1,j-B[i]] + A[i])
∵i番目を使わないとき、i番目の魔法をもう一回使うとき でダメージの最大の方を選択する
初期条件
dp[0][0] = 0 


これだとjが大きすぎてTLEする
'''

'''
dp[i][j] ... 魔力の最小値。[0,i)の魔法で、ちょうどダメージjにできるとき。ちょうどjにできなければINF
更新則

dp[i+1][j] = min(dp[i][j],dp[i+1,j-A[i]] + B[i]) で良さそうじゃない？
∵i番目を使わないとき、i番目の魔法をもう一回使うとき でダメージの最大の方を選択する
初期条件
dp[i][0] = 0
'''


J = max(2 * H + 1, 2 * max(B))
dp = [[INF] * (J) for _ in range(N + 1)]  
for i in range(N + 1):
    dp[i][0] = 0

for i in range(N):
    for j in range(J):
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
        if j - A[i] >= 0:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - A[i]] + B[i])

ans = INF
for x in dp[N][H:]:
    ans = min(ans, x)

print(ans)

Solution (Python):
H,N = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
A.insert(0,[0,0])
INFTY = 10**9
dp = [[INFTY for _ in range(H+1)] for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for i in range(1,N+1):
    for j in range(1,H+1):
        dp[i][j] = dp[i-1][j]
        dp[i][j] = min(dp[i][j],dp[i][max(0,j-A[i][0])]+A[i][1])
print(dp[N][H])

Solution (Python):
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)
M=[]
M.append(0)
for i in range(h):
  M.append(10000000000000)
for i in range(0,h+1):
  for j in range(n):
    c=min(h,a[j]+i)
    if M[c]>M[i]+b[j]:
      M[c]=M[i]+b[j]
print(M[h])

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

DP=[float('inf')]*(H+1)
DP[0]=0
for i in range(H):
    for a,b in M:
        DP[min(i+a,H)] = min(DP[min(i+a,H)],DP[i]+b)
print(DP[-1])

Solution (Python):
H,N = map(int,input().split())
M=[list(map(int,input().split())) for _ in range(N)]
M = sorted(M,reverse=True)

DP=[float('inf')]*(H+1)
DP[0]=0
for i in range(H):
    for a,b in M:
        DP[min(i+a,H)] = min(DP[min(i+a,H)],DP[i]+b)
print(DP[-1])

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    h, n = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(n)]

    dp = [f_inf] * (h + 1)
    dp[0] = 0
    for a, b in AB:
        for i in range(1, h + 1):
            dp[i] = min(dp[i], dp[max(0, i - a)] + b)

    print(dp[h])


if __name__ == '__main__':
    resolve()