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


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


bsum=[0]
for i in range(1,h+1):
    min_bsum = 9999999999999
    for atk in range(n):
        min_bsum = min(min_bsum,bsum[max(0,i-a[atk])]+b[atk])
    bsum.append(min_bsum)
 

print(bsum[h])

Solution (Python):
def DP():
  H,N,*T = map(int,open(0).read().split())
  
  
  dp = [0] + [10 ** 9] * H
  
  
  for d,c in zip(*[iter(T)] * 2):
    
    
    for j in range(H + 1):
      k = j - d
      if k < 0:
        k = 0
      
      
      l = dp[k] + c
      if l < dp[j]:
        dp[j] = l
      
  print(dp[-1])
  
DP()

Solution (Python):
def main():
    h,n,*t=map(int,open(0).read().split())
    d=[0]+[10**18]*h
    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])
main()

Solution (Python):

import sys
input = sys.stdin.readline
INF = 10**9

def main(): 
    h, n = [int(item) for item in input().split()]
    MAX_LEN = h + 10**4 - 2
    dp = [INF] * MAX_LEN
    dp[0] = 0
    vw = []
    for _ in range(n):
        v, w = map(int, input().split())
        vw.append((v, w))
    vw.sort(reverse=True, key=lambda x:x[0])
    for v, w in vw:
        for j in range(h):
            if dp[j] == INF:
                continue
            tmp = dp[j] + w
            if tmp < dp[j+v]:
                dp[j+v] = tmp
    print(min(dp[h:]))

if __name__ == "__main__":
    main()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

H, N = MAP()
AB = [()]
for i in range(N):
    a, b = MAP()
    AB.append((a, b))

dp = list2d(N+1, 20007, INF)
dp[0][0] = 0
for i in range(1, N+1):
    a, b = AB[i]
    for j in range(20007):
        dp[i][j] = min(dp[i][j], dp[i-1][j])
        if j-a >= 0:
            dp[i][j] = min(dp[i][j], dp[i-1][j-a] + b)
            dp[i][j] = min(dp[i][j], dp[i][j-a] + b)
print(min(dp[N][H:]))

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


bsum=[0]
for i in range(1,h+1):
    min_bsum = 9999999999999
    for atk in range(n):
        if bsum[max(0,i-a[atk])]+b[atk] < min_bsum:
            min_bsum = bsum[max(0,i-a[atk])]+b[atk]
    bsum.append(min_bsum)


print(bsum[h])

Solution (Python):
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]


dp = np.array([0]*(H+1))


for i in range(1, H+1):
    dp[i] = min(dp[np.maximum(i - A, 0)] + B)

print(dp[-1])

Solution (Python):
H, N  = map(int, input().split())
fs = [] 
for i in range(N):
    fs.append(map(int, input().split()))
INF = 10**10
M = 2*10**4+1
dp = [INF]*M
dp[0] = 0
for i,(a,b) in enumerate(fs):
    
    for j in range(H):
        
        dp[j+a] = min(dp[j+a], dp[j]+b) 
print(min(dp[H:]))

Solution (Python):
H, N = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
    A[i],B[i] = map(int, input().split())
    C[i] = A[i]/B[i]
inf=float("inf")
dp = [[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(N):
    for j in range(H+1):
        dp[i+1][j] = min(dp[i][j],dp[i+1][max(j-A[i],0)]+B[i])
print(dp[N][H])

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


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

INF = 10**10
M = H + max(As) + 1
dp = [INF]*M
dp[0] = 0
for i,(a,b) in enumerate(zip(As,Bs)):
    
    for j in range(H):
        
        dp[j+a] = min(dp[j+a], dp[j]+b) 
print(min(dp[H:]))

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


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

INF = 10**10
M = 21000
dp = [INF]*M
dp[0] = 0
for i,(a,b) in enumerate(zip(As,Bs)):
    
    for j in range(H):
        
        dp[j+a] = min(dp[j+a], dp[j]+b) 
print(min(dp[H:]))

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


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

"""
dp[i][j] = i番目までの魔法を見たとき、ダメージがちょうどjになる最小値
"""
INF = 10**10
M = 21000
dp = [[INF]*M for i in range(N+1)]
dp[0][0] = 0
for i,(a,b) in enumerate(zip(As,Bs)):
    dp[i+1][:] = dp[i][:]
    for j in range(H):        
        dp[i+1][j+a] = min(dp[i][j+a], dp[i+1][j]+b)
        
print(min(dp[N][H:]))

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

A = np.array(A)
B = np.array(B)
DP = np.zeros(H+1)

for i in range(1,H+1):
    DP[i] = np.amin(DP[np.maximum(i - A, 0)] + B)
    
print(int(DP[-1]))

Solution (Python):

import numpy as np


H, N = [int(str) for str in input().strip().split()]
AB = np.array([[int(str) for str in input().strip().split()] for _ in range(N)])

dp = np.zeros(H + 1)
for h in range(1, H + 1):
    dp[h] = min(AB[:, 1] + dp[np.maximum(0, h - AB[:, 0])]) 
print(int(dp[-1]))

Solution (Python):
import sys
from pprint import pprint
import math

h, n = map(int, sys.stdin.readline().strip().split(" "))

a, b = [], []
for _ in range(n):
    a_i, b_i = map(int, sys.stdin.readline().strip().split(" "))
    a.append(a_i)
    b.append(b_i)

a_max = max(a)
dp = [[10**12] * (h+a_max+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+a_max+1):
        if j < a[i]:
            dp[i+1][j] = dp[i][j]
        
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j-a[i]] + b[i])


print(min(dp[n][h:]))

Solution (Python):




import sys
input = sys.stdin.readline



def main():
    H,N = map(int,input().split())
    
    A = []
    B = []
    for i in range(N):
        temp = list(map(int,input().split()))
        A.append(temp[0])
        B.append(temp[1])

    
    

    maxA = max(A)
    INF = 10 ** 4 + 1

    dp = [[INF for j in range(H+maxA+1)] for i in range(N)]
    dp[0][0] = 0
    for j in range(1,H+maxA+1):
        time = (j+A[0]-1)//A[0]
        dp[0][j] = B[0] * time

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

    res = min(dp[N-1][H:])
    print(res)





main()

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

A_max = 10 ** 4
INF = float('inf')

dp = [INF] * (H + A_max + 1)
dp[0] = 0
for i in range(H):
    if dp[i] == INF:
        continue
    for a, b in AB:
        if dp[i] + b < dp[i + a]:
            dp[i + a] = dp[i] + b
print(min(dp[H:]))

Solution (Python):
from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 8)

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


AB.sort(key=lambda ab: (ab[1] / ab[0], ab[0]))


@lru_cache(maxsize=None)
def dp(h):
    if h <= 0:
        return 0
    else:
        best = float("inf")
        for a, b in AB:
            val = b + dp(h - a)
            if val < best:
                best = val
            else:
                
                break
        return best


print(dp(H))

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

max_size = 2*10**4 + 1
h, n = map(int, input().split())
DP = [float('INF')] * max_size
DP[0] = 0

for _ in range(n):
    damage, cost = map(int, input().split())
    for idx in range(damage, max_size):
        DP[idx] = min(DP[idx], DP[idx-damage] + cost)

print(min(DP[h:]))

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


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


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


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = {False: 'No', True: 'Yes'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def main():
    H, N = MI()
    A = [0] * N
    B = [0] * N
    dp = [[IINF] * (H+2) for i in range(N+1)]
    dp[0][0] = 0

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

    
    for i in range(N):
        for j in range(H+1):
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
            max_h = min(H, j+A[i])
            dp[i+1][max_h] = min(dp[i+1][max_h], dp[i+1][j] + B[i])
    print(dp[N][H])


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for a, b in AB:
            t = dp[i] + b
            if t < dp[i + a]:
                dp[i + a] = t
    print(min(dp[H:]))


main()

Solution (Python):
def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for j in range(N):
            a, b = AB[j]
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


main()

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(H):
        nj = min(j+a, H)
        dp[nj] = min(dp[nj], dp[j]+b)
print(dp[H])

Solution (Python):
def e_crested_ibis_vs_monster():
    H, N = [int(i) for i in input().split()]
    Damage, Cost = [], []
    for _ in range(N):
        a, b = [int(i) for i in input().split()]
        Damage.append(a)
        Cost.append(b)

    
    dp = [[float('inf')] * (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 + Damage[i], H)] = min(dp[i + 1][min(j + Damage[i], H)],
                                                   dp[i + 1][j] + Cost[i])
    return dp[N][H]

print(e_crested_ibis_vs_monster())

Solution (Python):
H, N = [int(str) for str in input().strip().split()]
AB = [[int(str) for str in input().strip().split()] for _ in range(N)]

dp = [0]
for h in range(1, H + 1):
    a, b = min(AB, key=lambda i: i[1] + dp[max(0, h - i[0])])
    dp.append(b + dp[max(0, h - a)])    
print(dp[-1])

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

dp = [float("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):
        nj = min(j+a, h)
        dp[nj] = min(dp[nj], dp[j]+b)

print(dp[h])

Solution (Python):
import sys
import numpy as np

read = sys.stdin.read

H, N, *AB = map(int, read().split())

A = np.array(AB[::2], np.int64)
B = np.array(AB[1::2], np.int64)

INF = 10 ** 10
dp = np.full(H + 1, INF, np.int64)
dp[0] = 0

for i in range(1, H + 1):
    dp[i] = (dp[np.maximum(i - A, 0)] + B).min()

print(dp[-1])

'''
pypyなら346ms

import sys

read = sys.stdin.read

H, N, *AB = map(int, read().split())

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

for a, b in zip(*[iter(AB)] * 2):
    for i in range(a, H + 1):
        dp[i] = min(dp[i], dp[i - a] + b)
    r = min(r, min(dp[-a:]) + b)

print(min(r, dp[-1]))
'''

Solution (Python):
H,N=map(int,input().split())
L=[]
m=0
for i in range(N):
    T=[0,0]
    T[0],T[1]=map(int,input().split())
    L.append(T)
    m=max(m,T[0])
L=sorted(L,reverse=True)
L.insert(0,[0,0])
m+=1
DP=[[10**10 for i in range(H+1+m)] for j in range(N+1)]
DP[0][0]=0

for i in range(1,N+1):
    t=0
    DP[i][0]=0
    for j in range(H+1):
        DP[i][j]=min(DP[i][j],DP[i-1][j])
        t=min(DP[i][j],DP[i-1][j])+L[i][1]
        DP[i][j+L[i][0]]=min(DP[i][j+L[i][0]],t)
    DP[i][H]=min(DP[i][H:-1])
print(min(DP[N][H:-1]))

Solution (Python):
h, n = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]
INF = 10**18

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

Solution (Python):
def main():
  def cal(k):
    for a,b in AB:
      tmp = k-a
      if tmp<0:
        tmp = 0
      new = dp[tmp]+b
      if new<dp[k]:
        dp[k]=new
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  mi_a,mi_b = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [0]+[mi_b]*mi_a+[INF]*H
  for i in range(mi_a+1,H+1):
    cal(i)

  print(dp[H])
main()

Solution (Python):
import sys
import numpy as np
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    H, N, *ab = map(int, read().split())
    A = []
    B = []
    for a, b in zip(*[iter(ab)] * 2):
        A.append(a)
        B.append(b)

    A = np.array(A)
    B = np.array(B)
    
    dp = np.full((H+1), np.inf)
    dp[0] = 0


    for i in range(1,H+1):
        dp[i] = np.min(dp[np.maximum(i - A, 0)] + B)

    print(int(dp[H]))


if __name__ == "__main__":
    main()

Solution (Python):
h,n = [int(i) for i in input().split()]
 
l = sorted([[int(i) for i in input().split()] for _ in range(n)],key = lambda x: x[0])
 
inf = float('inf')
memo = [inf for _ in range(10**5)]
memo[0] = 0
for i in range(h+1):
    for x,y in l:
        memo[i+x] = min(memo[i+x],memo[i] + y)
print(min(memo[h:]))

Solution (Python):
h,n = [int(i) for i in input().split()]
l = [[int(i) for i in input().split()] for _ in range(n)]
 
inf = float('inf')
memo = [inf for _ in range(h+1)]
memo[0] = 0
for i in range(h+1):
    for x,y in l:
        j = min(i+x, h)
        memo[j] = min(memo[j],memo[i] + y)
print(memo[-1])

Solution (Python):
import sys

read = sys.stdin.read

H, N, *AB = map(int, read().split())

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

for a, b in zip(*[iter(AB)] * 2):
    for i in range(a, H + 1):
        dp[i] = min(dp[i], dp[i - a] + b)
    r = min(r, min(dp[-a:]) + b)

print(min(r, dp[-1]))

Solution (Python):
def resolve():
    import sys
    input = sys.stdin.readline
    
    H, N = map(int, input().split())

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

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

    print(dp[H])


if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)

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


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


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


print(dp[N][H])

Solution (Python):
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]


dp = np.array([0 for i in range(H+1)])

for i in range(1, H+1):
    dp[i] = np.min(dp[np.maximum(i - A, 0)] + B)

print(dp[-1])

Solution (Python):
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  mi_a,mi_b = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [0]+[mi_b]*mi_a+[INF]*H
  for i in range(mi_a+1,H+1):
    mi = float('inf')
    for a,b in AB:
      mi = min(mi,dp[max(0,i-a)]+b)
    if dp[i]>mi:
      dp[i] = mi

  print(dp[H])
  
main()

Solution (Python):
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]


dp = np.array([0]*(H+1))


for i in range(1, H+1):
    dp[i] = min(dp[np.maximum(i - A, 0)] + B)

print(dp[-1])

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

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

h_dp = h + max(a)

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

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

ans = float("inf")
for i in range(h, h_dp+1):
    ans = min(ans, dp[n][i])

print(ans)

Solution (Python):
def solve(H, N, AB):
    INF = 10e10
    dp = [[INF] * (H+1) for i in range(N+1)]
    dp[0][0] = 0
    A, B = AB[0]
    for j in range(H+1):
        dp[0][j] = min(dp[0][j], dp[0][max(0, j-A)]+B)
    for i in range(1,N):
        A, B = AB[i]
        for h in range(H+1):
            dp[i][h] = min(dp[i-1][h], dp[i][max(0, h-A)]+B)
        
    
    return(dp[N-1][H])


def main():
    H, N = map(int, input().split())
    AB = []
    for i in range(N):
        A, B = map(int, input().split())
        AB.append((A, B))
    print(solve(H, N, AB))
main()

Solution (Python):
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]


dp = np.array([0 for i in range(H+1)])

for i in range(1, H+1):
    dp[i] = min(dp[np.maximum(i - A, 0)] + B)

print(int(dp[-1]))

Solution (Python):
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]




dp = np.array([np.inf for i in range(H+1)])
dp[0] = 0

for i in range(1, H+1):
    dp[i] = min(dp[np.maximum(i-A, 0)] + B)

print(int(dp[-1]))

Solution (Python):
H, N = map(int, input().split())
damage = []
mp = []
 
for i in range(N):
    a, b = map(int, input().split())
    damage.append(a)
    mp.append(b)
 
dp = [[0]+[float("inf")]*H for _ in range(N + 1)]

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

print(dp[N][H])

Solution (Python):
import numpy as np

H, N = map(int, input().split())
AB = np.array([list(map(int, input().split())) for i in range(N)])
A = AB[:, 0]
B = AB[:, 1]


dp = np.array([10**10 for i in range(H+1)], dtype=int)

dp[0] = 0

for i in range(1, H+1):
    
    dp[i] = min(dp[np.maximum(i-A, 0)] + B)

print(dp[-1])

Solution (Python):
def main():
  H,N = map(int,input().split())
  AB = [list(map(int,input().split())) for _ in range(N)]
  ab = min(AB,key = lambda x:x[1])
  INF = float('inf')
  dp = [INF]*(H+1)
  mi_a = ab[0]
  mi_b = ab[1]
  dp[0] = 0
  for i in range(1,H+1):
    if i<=mi_a:
      dp[i]=mi_b
    else:
      mi = float('inf')
      for a,b in AB:
        mi = min(mi,dp[max(0,i-a)]+b)
      if dp[i]>mi:
        dp[i] = mi

  print(dp[H])
  
main()

Solution (Python):
import sys
from operator import itemgetter

input = sys.stdin.readline


def main():
    H, N = [int(x) for x in input().split()]
    AB = []
    for _ in range(N):
        a, b = [int(x) for x in input().split()]
        AB.append([a, b])

    dp = [0] * (H + 1)

    for i in range(1, H + 1):
        mi = float("inf")
        for a, b in AB:
            mi = min(mi, dp[max(i - a, 0)] + b)
        dp[i] = mi

    print(dp[H])



if __name__ == '__main__':
    main()

Solution (Python):

from bisect import *
from collections import *
from heapq import *



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

A=H+10**4+1
dp=[10**9 for i in range(H+10**4+1)]
dp[0]=0
for a,b in AB:
    for i in range(1,A):
        if i>=a:
            dp[i]=min(dp[i-a]+b,dp[i])

print(min(dp[H:]))

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

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

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

dp = [float("inf")] * (h + 1)
dp[0] = 0
for i in range(h):
    for j in range(n):
        index = min(i + a[j], h)
        dp[index] = min(dp[index], dp[i] + b[j])

print(dp[h])

Solution (Python):
import numpy as np


H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
a, b = np.array(AB, dtype=np.int64).reshape(N, 2).T

d = np.zeros(H + 1, dtype=np.int64)

for hp in range(1, H + 1):
    c = d[np.maximum(hp - a, 0)] + b
    d[hp] = np.amin(c)

print(d[H])

Solution (Python):



H,N=map(int,input().split())
weights=[list(map(int,input().split())) for _ in range(N)]
INF=100100101




dp=[INF]*(H+1)
for i in range(H+1):
    if i==0:
        dp[i]=0
    else:
        for j in weights:
            a=j[0]
            b=j[1]
            piv=max(0,i-a)  
            dp[i]=min(dp[i],dp[piv]+b) 
    

print(dp[H])

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

dp = [10 ** 18] * (2 * 10 ** 4 + 1)
dp[0] = 0
for a, b in AB:
    for i in range(H):
        dp[i + a] = min(dp[i + a], dp[i] + b)

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

Solution (Python):


import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    H, N = MAP()
    L = [LIST() for i in range(N)]
    A = []
    B = []
    for i in range(N):
        A.append(L[i][0])
        B.append(L[i][1])

    dp = [INF] * 20010
    dp[0] = 0
    for i in range(10010):
        for j in range(N):
            dp[i+A[j]] = min(dp[i+A[j]], dp[i]+B[j])
    ans = INF
    for i in range(H, 20010):
        ans = min(ans, dp[i])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


h, n = map(int, input().split())
a = ["" for _ in range(n)]
b = ["" for _ in range(n)]

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

inf = 10**12
dp = [[inf 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(n):
    for j in range(1,h+1):
        
        
        if j <= a[i]:
            
            
            
            
            dp[i+1][j] = min(b[i], dp[i][j])
            
        else:
            
            
            
            
            dp[i+1][j] = min(dp[i][j], dp[i+1][j - a[i]]+b[i])
    
 

print(dp[n][h])

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

INF = 10**10

dp = [INF for i in range(H+1)]
dp[0] = 0

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

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

INF = 10**10

dp = [INF for i in range(H+1)]
dp[0] = 0

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def dc(n, m):
    return -(-n // m)


def slv(H, N, AB):

    dp = [INF] * (H+1)
    dp[0] = 0
    for a, b in AB:
        for h, bb in enumerate(dp):
            if bb == INF:
                continue
            n = min(H, h+a)
            dp[n]  = min(dp[n], bb+b)
    return dp[-1]


def main():
    H, N = read_int_n()
    AB = [read_int_n() for _ in range(N)]
    print(slv(H, N, AB))
    
    
    
    


if __name__ == '__main__':
    main()

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

INF = 10**10


dp = [INF for i in range(H+1)]
dp[0] = 0

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

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

INF = 10**10


AM = 0
for i in range(N):
    AM = max(AM,magic[i][0])



dp = [INF for i in range(H+AM+1)]
dp[0] = 0

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

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

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

for _ in range(n):
    damage, mp = map(int, input().split())
    for i in range(h+1):
        if i <= damage:
            dp[i] = min(dp[i], mp)
        else:
            dp[i] = min(dp[i], dp[i-damage] + mp)
print(dp[h])

Solution (Python):
h, n = map(int, input().split())
dp = [[0] * (h+1) for i in range(n)]
a, b = map(int, input().split())
for c in range(1, h+1):
  dp[0][c] = b * ((c-1) // a + 1)
for i in range(1, n):
  a, b = map(int, input().split())
  for c in range(1, h+1):
    opt1 = dp[i-1][c]
    opt2 = dp[i-1][max(0, c-a)] + b
    opt3 = dp[i][max(0, c-a)] + b
    dp[i][c] = min(opt1, opt2, opt3)
print(dp[-1][-1])

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

INF = 10**10


AM = 0
for i in range(N):
    AM = max(AM,magic[i][0])



dp = [INF for i in range(H+AM+1)]
dp[0] = 0

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

Solution (Python):
H, N = map(int, input().split())
inf =1000100001001000
dp = [inf for _ in range(H+1)]
 

dp[0]=0
for n in range(N):
    a,b =  map(int, input().split())
    for h in range(H):
        nj = min(h+a,H)
        dp[nj] = min([dp[nj],dp[h]+b])
        
print(int(dp[H]))

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

inf = 2**30
dp = [inf] * (H + 1)
dp[0] = 0
for i in range(N):
    A, B = list(map(int, input().split()))
    for j in range(H + 1):
        if dp[j] != inf:
            next = min(H, j + A)
            dp[next] = min(dp[next], dp[j] + B)

print(dp[H])

Solution (Python):
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
ab = min(AB,key = lambda x:x[1])
INF = float('inf')
dp = [INF]*(H+1)
mi_a = ab[0]
mi_b = ab[1]
dp[0] = 0
for i in range(1,H+1):
  if i<=mi_a:
    dp[i]=mi_b
  else:
    for k in AB:
      a = k[0]
      b = k[1]
      tmp = max(0,i-a)
      dp[i] = min(dp[i],dp[tmp]+b)
    
print(dp[H])