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

INF = 10**18
dp = [INF]*(h+1)
dp[h] = 0
for i in reversed(range(h+1)):
    for j in range(n):
        a,b = AB[j]
        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):
INF = 10 ** 9 + 7
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0

for i in range(N):
    for j in range(H):
        if dp[j] == INF: continue
        if j + ab[i][0] <= H:
            dp[j + ab[i][0]] = min(dp[j + ab[i][0]], dp[j] + ab[i][1])
        else:
            dp[H] = min(dp[H], dp[j] + ab[i][1])

print(dp[H])

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

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


if __name__ == '__main__':
    main()

Solution (Python):
def ceil(x,p):
    if x%p==0:
        return x//p
    else:
        return x//p+1

def main():
    INF = 10**9
    h,n = map(int,input().split())
    spell = []
    for _ in range(n):
        spell.append(tuple(map(int,input().split())))

    dp = [[INF]*(h+1) for _ in range(n+1)]

    for i,item in enumerate(spell):
        a,b = item
        for j in range(h):
            if j-a<0:
                dp[i+1][j+1] = min(dp[i][j+1],b*ceil(j+1,a))
            else:
                dp[i+1][j+1] = min(dp[i][j+1],dp[i+1][j+1-a]+b)
        
    print(dp[n][h])
main()

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  p=10005
  h,n=LI()
  a=[]
  b=[]

  for _ in range(n):
    x,y=LI()
    a.append(x)
    b.append(y)

  dp=[[inf]*(p+1) for _ in range(n+1)]
  dp[0][0]=0

  for i in range(n):
    for j in range(p):
      dp[i+1][j]=min(dp[i+1][j],dp[i][j])
      if j+a[i]<p:
        dp[i+1][j+a[i]]=min(dp[i+1][j+a[i]],dp[i+1][j]+b[i])
      else:
        dp[i+1][p]=min(dp[i+1][p],dp[i+1][j]+b[i])

  
  ans=inf
  for i in range(h,p+1):
    ans=min(ans,dp[n][i])
  return ans


print(main())

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  p=20010
  h,n=LI()
  a=[]
  b=[]

  for _ in range(n):
    x,y=LI()
    a.append(x)
    b.append(y)

  dp=[[inf]*p for _ in range(n+1)]
  dp[0][0]=0

  for i in range(n):
    for j in range(p-5):
      dp[i+1][j]=min(dp[i+1][j],dp[i][j])
      if j+a[i]<p:
        dp[i+1][j+a[i]]=min(dp[i+1][j+a[i]],dp[i+1][j]+b[i])

  
  ans=inf
  for i in range(h,20010):
    ans=min(ans,dp[n][i])
  return ans


print(main())

Solution (Python):
def getval():
    h,n = map(int,input().split())
    m = [list(map(int,input().split())) for i in range(n)]
    return h,n,m

def main(h,n,m):
    
    dp = [0]
    for i in range(h):
        update = 2**60
        for j in m:
            if j[0]>=(i+1):
                update = min(update,j[1])
            else:
                update = min(update,j[1]+dp[i+1-j[0]])
        dp.append(update)
    print(dp[h])    
                
if __name__=="__main__":
    h,n,m = getval()
    main(h,n,m)

Solution (Python):
H, N = map(int, input().split())
INF = 10**9
dp= [INF for _ in range(H+1)]

dp[0] = 0

for i in range(N):
  a, b = map(int, input().split())
  for j in range(H):
    if j+a > H:
      dp[H] = min(dp[H], dp[j] + b)
    else:
      dp[j+a] = min(dp[j+a], dp[j] + b)
print(dp[H])

Solution (Python):











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



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
















































































max_a = max(a for a, b in A_B)

max_damege = H + max_a

dp = [0] * (max_damege + 1)

for i in range(max_damege + 1):
    dp[i] = min(dp[i-a]+b for a,b in A_B)

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

Solution (Python):
h,n = map(int, input().split())
ab = []
amax = 10**5
for _ in range(n):
  ab.append(tuple(map(int,input().split())))
amax = max(a for a,b in ab)
p = [0]*(h+amax+1)
for i in range(1,h+amax):
  p[i] = min(p[i-a]+b for a,b in ab)
print(p[h])

Solution (Python):
H, N = map(int, input().split())
AB = [0] * N
for i in range(N):
    AB[i] = list(map(int, input().split()))

m = 10**4 + 100

dp = [float("INF")] * m

dp[0] = 0

for i in range(H):
    for x in AB:
        if i + x[0] <= H:
            dp[i + x[0]] = min(dp[i + x[0]], dp[i] + x[1])
        else:
            dp[H] = min(dp[H], dp[i] + x[1])

print(dp[H])

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

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

dp = [10**10]*(h+1)
dp[0] = 0

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

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

dp = [float("inf") for i in range(10**5)]
dp[0] = 0

for a,b in ab:
    for k in range(a+1):
        dp[k] = min(dp[k], b)
    for k in range(a+1,h+1):
        dp[k] = min(dp[k], dp[k-a] + b)
print(dp[h])

Solution (Python):
h,n=map(int,input().split())
dp=[10**10 for i in range(h+1)]
dp[0]=0
for i in range(n):
    x,y=map(int,input().split())
    for j in range(h+1):
        nj=min(h,j+x)
        dp[nj]=min(dp[nj],dp[j]+y)
print(dp[h])

Solution (Python):
h,n=map(int,input().split())
a=[0]
b=[0]
width=2*10**4+7
dp=[[10**10 for i in range(width)] for j in range(n+1)]
dp[0][0]=0
dp[1][0]=0
for i in range(n):
    x,y=map(int,input().split())
    a.append(x)
    b.append(y)
for i in range(1,n+1):
    for j in range(width):
        dp[i][j]=min(dp[i][j],dp[i-1][j])
        if(j+a[i]<width):
            dp[i][j+a[i]]=min(dp[i][j+a[i]],dp[i][j]+b[i])
print(min(dp[n][h:]))

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

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

dp=[[float("inf")]*(H+1) for _ in range(N+1)]
dp[0][0]=0
for i in range(N):
	a, b = A[i], B[i]
	for j in range(H+1):
		if a>=j:
			dp[i+1][j] = min(dp[i][j], b)
		else:
			dp[i+1][j] = min(dp[i][j], dp[i+1][j-a]+b)
print(dp[-1][-1])

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

dp = [float("inf")]*(h+1)
dp[0] = 0

for i in range(h):
  for d,c in ab:
    hp = min(i+d,h)
    dp[hp] = min(dp[hp],dp[i]+c)

print(dp[-1])

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from  collections import defaultdict
from collections import Counter
import bisect
from functools import reduce

def main():
    H, N = MI()
    A = []
    B = []
    A_B = []
    for i in range(N):
        a, b = MI()
        A.append(a)
        B.append(b)
    max_A = max(A)
    
    dp = [10 ** 9] * (H + max_A)
    dp[0] = 0
    for j in range(N):
        a_j = A[j]
        b_j = B[j]
        for i in range(H + max_A):
            if i - a_j >= 0:
                dp[i] = min(dp[i - a_j] + b_j, dp[i])
            else:
                dp[i] = min(b_j, dp[i])

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


if __name__ == "__main__":
    main()

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

for i in range(n):
    p,m = map(int,input().split())  
    P.append(p)
    M.append(m)
    
dp=[[999999999]*(h+1) for _ in range(n+1)]
dp[0][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])
        dp[i+1][min(j+P[i],h)] = min(dp[i+1][min(j+P[i],h)],dp[i+1][j] + M[i] )
 
print(dp[n][h])

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

dp = [math.inf]*(h+10000)
dp[0] = 0
for a, b in ab:
    for i in range(h+10000):
        dp[i] = min(dp[i], dp[i-a]+b)
print(min(dp[h:]))

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

m = 20010
dp = [1000000000] * m
dp[0] = 0
for a, b in ab:
    for i in range(m):
        if i - a >= 0:
            dp[i] = min(dp[i], dp[i - a] + b)
print(min(dp[h:]))

Solution (Python):

import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
from itertools import accumulate
sys.setrecursionlimit(10**6)




def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


H, N = zz()
A, B = [], []
for i in range(N):
    a, b = zz()
    A.append(a)
    B.append(b)


max_a = max(A)
dp = [[100000000]*(H + max_a + 1)] * (N+1)  

for j in range(H):
    if (j % A[0] == 0):
        dp[0][j] = (j//A[0])*B[0]
    else:
        dp[0][j] = (j//A[0]+1)*B[0]



for j in range(1, H + max_a + 1):
    for i in range(0, N):

        if(j < A[i]):
            dp[i][j] = min(dp[i-1][j], B[i])
        else:
            
            
            
            

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



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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    H,N=MI()
    A=[0]*N
    B=[0]*N
    
    for i in range(N):
        A[i],B[i]=MI()
        
    inf =10**20
    dp=[[inf]*(H+1)for _ in range(N+1)]
    
    
    dp[0][0]=0
    
    for i in range(N):
        a=A[i]
        b=B[i]
        for j in range(H+1):
            if j-a>=0:
                dp[i+1][j]=min(dp[i+1][j],
                               dp[i][j],
                               dp[i+1][j-a]+b)
            else:
                dp[i+1][j]=min(dp[i+1][j],
                               dp[i][j],
                               dp[i+1][0]+b)
                
    print(dp[-1][-1])

main()

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    H,N=MI()
    A=[0]*N
    B=[0]*N
    
    for i in range(N):
        A[i],B[i]=MI()
        
    inf =10**20
    dp=[[inf]*(H+1)for _ in range(N+1)]
    
    
    dp[0][0]=0
    
    for i in range(N):
        a=A[i]
        b=B[i]
        for j in range(H+1):
            dp[i+1][j]=min(dp[i+1][j],
                           dp[i][j])
            if j-a>=0:
                dp[i+1][j]=min(dp[i+1][j],
                               dp[i+1][j-a]+b)
            else:
                dp[i+1][j]=min(dp[i+1][j],
                               dp[i+1][0]+b)
                
    print(dp[-1][-1])

main()

Solution (Python):
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from bisect import bisect_left,bisect_right
from math import sqrt,gcd,ceil,floor,factorial

from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse=False):
    data.sort(key=lambda x:x[col],reverse=revese)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def mydictvaluesort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[1] )

def mydictkeysort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[0] )

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]







h,n = MI()
ab = LI(n)


dp = [ [INF]*(h+1) for _ in range(n+1) ]
dp[0][0] = 0

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

print(dp[n][h])

Solution (Python):
def main():
    h, n = map(int, input().split())
    INF = 10 ** 9
    dp = [INF for _ in range(2*10**4)]
    AB = [tuple(map(int, input().split())) for _ in range(n)]
    dp[0] = 0
    for a, b in AB:
        for i in range(h):
            dp[i + a] = min(dp[i + a], dp[i] + b)
    print(min(dp[h:]))
if __name__ == "__main__":
    main()

Solution (Python):
INFTY = 10**8
H,N = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[INFTY for j in range(H+1)] for i 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]
        if j<=A[i-1][0]:
            dp[i][j] = min(dp[i][j],A[i-1][1])
        else:
            dp[i][j] = min(dp[i][j],dp[i][j-A[i-1][0]]+A[i-1][1])
print(dp[N][H])

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq














import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
    return int(readline())
h,n = readInts()
A = [readInts() for _ in range(n)]
dp = [INF] * (h+1)
dp[h] = 0
for idx in range(h,-1,-1):
    for i in range(n):
        a,b = A[i]
        dp[max(0,idx-a)] = min(dp[max(0,idx-a)], dp[idx] + b)
print(dp[0])

Solution (Python):
import numpy as np


def solve(H, A, B):
    A_max = A.max()
    dp = np.zeros(H + A_max + 1, dtype=np.int64)
    for i in range(A_max + 1, H + A_max + 1):
        dp[i] = np.min(dp[i - A] + B)
    return dp[-1]


H, N, *AB = map(int, open(0).read().split())
A = np.array(AB[::2], dtype=np.int64)
B = np.array(AB[1::2], dtype=np.int64)
print(solve(H, A, B))

Solution (Python):
import numpy as np
from numba import njit, i8


@njit(i8(i8, i8[:], i8[:]), cache=True)
def solve(H, A, B):
    A_max = A.max()
    dp = np.zeros(H + A_max + 1, dtype=np.int64)
    for i in range(A_max + 1, H + A_max + 1):
        dp[i] = np.min(dp[i - A] + B)
    return dp[-1]


H, N, *AB = map(int, open(0).read().split())
A = np.array(AB[::2], dtype=np.int64)
B = np.array(AB[1::2], dtype=np.int64)
print(solve(H, A, B))

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

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

Solution (Python):
H, N = map(int, input().split())
AB = []
for _ in range(N):
    A, B = map(int, input().split())
    AB.append([A, B])
dp = [10**9] * (H + 1)
dp[0] = 0
for i in range(H):
    for a, b in AB:
        if i + a <= H:
            dp[i + a] = min(dp[i + a], dp[i] + b)
        else:
            dp[H] = min(dp[H], dp[i] + b)
print(dp[H])

Solution (Python):
H, N = map(int,input().split())
M = []
for k in range(N):
    M.append(list(map(int,input().split())))
dp = [10**9]*(10**5)
dp[0] = 0
for A, B in M:
    for k in range(A+1):
        dp[k] = min(dp[k],B)
    for k in range(A+1,H+1):
        dp[k] = min(dp[k],dp[k-A]+B)
print(dp[H])

Solution (Python):

import sys
def main(H, N, AB):
    INF = 10**9
    dp = [INF] * (H + 1)
    dp[H] = 0
    for h in reversed(range(H + 1)):
        for i, (a, b) in enumerate(AB):
            nh = max(0, h - a)
            dp[nh] = min(dp[nh], dp[h] + b)
    print(dp[0])

if __name__ == '__main__':
    input = sys.stdin.readline
    H, N = map(int, input().split())
    AB = [tuple(map(int, input().split())) for _ in range(N)]
    main(H, N, AB)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





N, M = getNM()
W = []
V = []
for i in range(M):
    a, b = getNM()
    W.append(b)
    V.append(a)


def knapsack_4(N, limit, weight, value):
    dp = [float('inf')] * (limit + 1)
    dp[0] = 0

    for i in range(1, limit + 1):
        for j in range(N):
            if i < value[j]:
                dp[i] = min(dp[i], weight[j])
            else:
                dp[i] = min(dp[i], dp[i - value[j]] + weight[j])
    return dp[-1]
print(knapsack_4(M, N, W, V))

Solution (Python):
H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
dp=[10**8]*(H+1)
dp[0]=0
i=0

for i in range(H+1):
  for A,B in AB:
    if i-A>=0:
      dp[i]=min(dp[i-A]+B,dp[i])
  for A,B in AB:
    if i+A>H:
      dp[-1]=min(dp[-1],dp[i]+B)
print(dp[-1])

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

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

dp = [0 for _ in range(10**5)]

for i in range(H):
    dp[i] = min(dp[i-a]+b for a, b in magic)

print(dp[H-1])

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

dpl = H+max(A)
x = 10**10
dp = [x]*dpl
dp[0] = 0

for a, b in zip(A, B):
    for i in range(a, dpl):
        dp[i] = min(dp[i-a]+b, dp[i])
    for i in range(dpl-2, -1, -1):
        dp[i] = min(dp[i+1], dp[i])

print(dp[H])

Solution (Python):
h,n=map(int,input().split())
dp=[10000000000]*(10**5+1)
dp[0]=0

a=[]
b=[]
for _ in range(n):
    a1,b1=map(int,input().split())
    a.append(a1)
    b.append(b1)
for i in range(10**4+2):
    
    for j in range(n):
        dp[i+a[j]]=min((dp[i+a[j]]),dp[i]+b[j])
print(min(dp[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)

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

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

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=[[float("inf")]*(H+1) for _ in range(N+1)]

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

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


if __name__ == "__main__":
    main()

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

dp=[float('inf')]*(h+1)
dp[0]=0
for i in range(h):
    for j in range(n):
        if dp[i] == float('inf'):continue
        next=i+ab[j][0] if i+ab[j][0]<=h else h
        dp[next]=min(dp[next],dp[i]+ab[j][1])
print(dp[-1])

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

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

        next=i+ab[j][0] if i+ab[j][0]<=h else h
        dp[next]=min(dp[next],dp[i]+ab[j][1])
print(dp[-1])

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

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

        next=i+ab[j][0] if i+ab[j][0]<=h else h
        dp[next]=min(dp[next],dp[i]+ab[j][1])
print(dp[-1])

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


dp = [float('inf')] * (h + 1)
dp[0] = 0

for i in range(h + 1):
    for j in range(n):
        if dp[i] == float('inf'):continue
        temp_a = i + ab[j][0] if i + ab[j][0] < h else h
        temp_b = dp[i] + ab[j][1]

        dp[temp_a] = min(dp[temp_a], temp_b)

print(dp[-1])

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=[float('inf')]*(h+1)
dp[0]=0
for i in range(h):
    for j in range(n):

        next=i+a[j] if i+a[j]<=h else h
        dp[next]=min(dp[next],dp[i]+b[j])
print(dp[-1])

Solution (Python):
H,N = map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N)]
INF = float("inf")
dp0 = [INF]*(H+1)
dp1 = [0]*(H+1)
dp0[0] = 0
for i in range(N):
    a,b = AB[i]
    dp1[H] = dp0[H]
    for h in range(H-1, -1, -1):
        dp1[h] = min(dp0[h], dp1[h+1])
    for h in range(H+1):
        dp1[h] = min(dp1[h], dp1[max(h-a,0)]+b)
    t = dp1
    dp1 = dp0
    dp0 = t
print(dp0[H])

Solution (Python):
INF = 2**60 
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [[INF] * (H + 1) for _ in range(N + 1)]
dp[0][0] = 0
for i, (A, B) in enumerate(AB):
    for j in range(H+1):
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        dp[i+1][j] = min(dp[i+1][j], dp[i+1][max(j-A, 0)] + B)
print(dp[N][H])

Solution (Python):
INF = 2**60 
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [[INF] * (H + 1)] * (N + 1)
dp[0][0] = 0
for i, (A, B) in enumerate(AB):
    for j in range(H+1):
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        dp[i+1][j] = min(dp[i+1][j], dp[i+1][max(j-A, 0)] + B)
print(dp[N][H])

Solution (Python):
H,N=map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N)]
dp = [[0]*(N+1) for _ in range(2*10**4+5)]

for j in range(1,H+1):
  for i in range(1,N+1):
    if AB[i-1][0] < j:
      dp[j][i] = dp[j - AB[i-1][0]][i] + AB[i-1][1]
    else:
      dp[j][i] = AB[i-1][1]
  MIN = min(dp[j][1:])
  dp[j][1:] = [MIN]*(len(dp[j])-1)

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

Solution (Python):
INF = 2**60 
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for A, B in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

Solution (Python):
INF = 2**60 
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for A, B in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

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

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

dp = [[10000000000000]*(H+1) for _ in range(N+1)]

for y in range(N):
    a,b = AB[y]
    for x in range(H+1):
        if x <= a:
            dp[y+1][x] = min(dp[y][x], b)
        else:
            dp[y+1][x] = min(dp[y][x], dp[y+1][x-a]+b)
print(dp[-1][-1])

Solution (Python):
def main():
  h,n=map(int,input().split())
  dp=[10**9]*(h+1)
  dp[h]=0
  for i in range(n):
    a,b=map(int,input().split())
    for j in range(h,0,-1):
      nxt=j-a
      if nxt<0:
        nxt=0
      if dp[nxt]>dp[j]+b:
        dp[nxt]=dp[j]+b
  print(dp[0])
main()

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

INF=10**9+7
dp=[INF]*(H+1)
dp[0]=0
for i in range(H):
    if dp[i]==INF:
        continue
    for j in range(N):
        a=A[j][0]
        b=A[j][1]
        k=min(H,i+a)
        dp[k]=min(dp[k], dp[i]+b)
print(dp[-1])

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

dp[0]=0
for a,b in ab:
  
  ndp=[inf]*(h+1)
  ndp[0]=0
  for i in range(1,h+1):
    ndp[i]=min(dp[i],ndp[max(0,i-a)]+b)
  dp=ndp
print(dp[h])

Solution (Python):
H, N = map(int, input().split())
magics = []
min_cost = (0, 100000)
max_attack = (0, 100000)
for i in range(N):
    attack, cost = map(int, input().split())
    magics.append((attack,cost))
    if cost < min_cost[1]:
        min_cost = (attack, cost)
    if max_attack[0] < attack:
        max_attack = (attack, cost)
    

dp = [0]
dp.extend([min_cost[1]] * min_cost[0])
for i in range(min_cost[0]+1, H + max_attack[0] + 1):
    minc = 100000000000
    for magic in magics:
        cost = magic[1]
        if 0 < i - magic[0]:
            cost += dp[i - magic[0]]
        if cost < minc:
            minc = cost
    dp.append(minc)
print(min(dp[H:]))

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

dp = [float("INF")] * 10 ** 5
dp[0] = 0

for h in range(H):
    for a, b in AB:
        dp[h+a] = min(dp[h] + b, dp[h+a])

print(min(dp[H:]))

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

for i in range(n):
  for j in range(h+1):
    if j-ab[i][0]>=0:
      dp[i+1][j]=min(dp[i][j],dp[i+1][j-ab[i][0]]+ab[i][1],(j//ab[i][0]+1)*ab[i][1])
    else:
      dp[i+1][j]=min(dp[i][j],ab[i][1])
print(dp[-1][h])

Solution (Python):
H,N = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
INFTY = 10**8
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]
        if j>=A[i-1][0]:
            dp[i][j] = min(dp[i][j],dp[i][j-A[i-1][0]]+A[i-1][1])
        else:
            dp[i][j] = min(dp[i][j],A[i-1][1])
print(dp[N][H])

Solution (Python):
H,N = map(int,input().split());INF = float("inf")
A=[];B=[]
for i in range(N):
  a,b = map(int,input().split())
  A.append(a);B.append(b)


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

Solution (Python):
import numpy as np
from numba import njit, i8


@njit(i8(i8, i8, i8[:, :]), cache=True)
def solve(H, N, magics):
    dp = np.full((H + 1), 1 << 31, dtype=np.int64)
    dp[0] = 0
    for i in range(N):
        A, B = magics[i]
        for h in range(H):
            dp[min(h + A, H)] = min(dp[min(h + A, H)], dp[h] + B)
    return dp[-1]


H, N, *AB = map(int, open(0).read().split())
magics = np.array([(A, B) for A, B in zip(*[iter(AB)] * 2)], dtype=np.int64)
print(solve(H, N, magics))

Solution (Python):

import sys
input = sys.stdin.buffer.readline
H, N = map(int, input().split())

INF = 1 << 40
dp = [INF]*(H+1)
dp[H] = 0
for i in range(N):
    a, b = map(int, input().split())
    for j in range(H, -1, -1):
        x = max(0, j-a)
        dp[x] = min(dp[x], dp[j]+b)

print(dp[0])