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):
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(10000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

INF = 10**10
dp_map = [[INF]*(H+1) for _ in range(N+1)]


dp_map[0][0]  = 0
for i in range(N):
    for j in range(H+1):
        if j<As[i]:
            dp_map[i+1][j] = min(dp_map[i+1][0]+Bs[i],dp_map[i][j])
        else:
            dp_map[i+1][j] = min(dp_map[i+1][j-As[i]]+Bs[i] ,dp_map[i][j])

print(dp_map[N][H])

Solution (Python):
import numpy as np
H, N = map(int, input().split())
ATK, MP = np.array(tuple(tuple(map(int, input().split())) for _ in range(N)), dtype=np.int32).T
table = np.zeros((H + ATK.max(),), dtype=np.int32)
for damage in range(1, H + 1):
    table[damage] = (table[damage - ATK] + MP).min()
print(table[H])

Solution (Python):
import numpy as np
H, N = map(int, input().split())
ATK, MP = np.array(tuple(tuple(map(int, input().split())) for _ in range(N)), dtype=np.int64).T
MAX_ATK = ATK.max()
table = np.zeros((H + MAX_ATK,), dtype=np.int64)
for damage in range(1, H + 1):
    table[damage] = (table[damage - ATK] + MP).min()
print(table[H])

Solution (Python):
import numpy as np
H, N = map(int, input().split())
ATK, MP = np.array(tuple(tuple(map(int, input().split())) for _ in range(N)), dtype=np.int64).T
MAX_ATK = ATK.max()
table = np.zeros((H + MAX_ATK,), dtype=np.int64)
for damage in range(1, H + 1):
    table[damage] = (table[damage - ATK[np.arange(N, dtype=np.int16)]] + MP[np.arange(N, dtype=np.int16)]).min()
print(table[H])

Solution (Python):
H, N = map(int, input().split())
ATK, MP = zip(*(tuple(map(int, input().split())) for _ in range(N)))
MAX_ATK = max(ATK)
table = [0] * (H + MAX_ATK)
for damage in range(1, H + 1):
    table[damage] = min(table[damage - ATK[magic]] + MP[magic] for magic in range(N))
print(table[H])

Solution (Python):
H, N = map(int, input().split())
hpdown = list()
mpdown = list()
for i in range(N):
  a, b = map(int, input().split())
  hpdown.append(a)
  mpdown.append(b)
  
hmax = H+(max(hpdown)+1)
dp = [10 ** 9] * (hmax)
dp[0] = 0
for h in range(1, hmax):
  for n in range(N):
    if(h-hpdown[n]>=0):
      dp[h] = min(dp[h], dp[h-hpdown[n]] + mpdown[n])
      
ans = 10 ** 9
for i in range(H, hmax):
  if(dp[i] < ans):
    ans = dp[i]
print(ans)

Solution (Python):
import sys
import math  
import bisect  
import queue  


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


def main():
    H, N = map(int, input().split())
    A = [0 for _ in range(N)]
    B = [0 for _ in range(N)]
    for i in range(N):
        a, b = map(int, input().split())
        A[i] = a
        B[i] = b

    INF = 1000000005
    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):
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            dp[i + 1][min(j + A[i], H)] = min(dp[i + 1]
                                              [min(j + A[i], H)], dp[i + 1][j] + B[i])

    print(dp[N][H])


if __name__ == '__main__':
    main()

Solution (Python):
INF = float('inf')

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 = [[INF for j in range(H + 1)] for i in range(N + 1)]

dp[0][0] = 0

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

print(dp[N][H])

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    H, N = map(int, input().split())
    magic = [list(map(int, input().split())) for _ in range(N)]
    dp = [float("inf") for _ in range(H + 1)]
    dp[0] = 0

    for h in range(H):
        for m in magic:
            a, b = m[0], m[1]
            tmp = dp[h] + b
            if h + a >= H and tmp < dp[H]:
                dp[H] = tmp
            elif h + a < H and tmp < dp[h + a]:
                dp[h + a] = tmp
    print(dp[-1])


if __name__ == "__main__":
    main()

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

attack_list = []
mp_list = []
for i in range(N):
    a, m = map(int, input().split())
    attack_list.append(a)
    mp_list.append(m)

inf = float('inf')
max_hp = H+max(attack_list)
dp = [inf for i in range(max_hp)]
dp[0] = 0

for i in range(H):
    for j in range(N):
        next_i = i + attack_list[j]
        dp[next_i] = min(dp[next_i], dp[i] + mp_list[j])

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


def main():
    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)
    dp[0] = 0

    for h in range(0, H):
        for a, b in zip(A, B):
            tmp = dp[h] + b
            if h + a >= H and tmp < dp[H]:
                dp[H] = tmp
            elif h + a < H and tmp < dp[h + a]:
                dp[h + a] = tmp

    print(dp[-1])


if __name__ == "__main__":
    main()

Solution (Python):
INF=10**30
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=[[INF]*(H+10) for i in range(N+1)]
dp[0][0]=0
for i in range(N):
    for j in range(H+1):
        if(j<A[i]):
            dp[i+1][j]=min(dp[i][j],dp[i][0]+B[i])
        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())
magics = []
for _ in range(n):
    a, b = map(int, input().split())
    magics.append((a, b))


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

print(dp[-1])

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
H,N = (int(x) for x in input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
mem = [0] * H
def dp(rem):
    global mem
    if rem <= 0:
        return 0
    elif mem[rem-1]:
        return mem[rem-1]
    else:
        a = min([dp(rem-AB[i][0]) + AB[i][1] for i in range(N)])
        mem[rem-1] = a
        return a
print(dp(H))

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

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

dp[H] = 0

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

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

dp = [10**10]*(h+1)
dp[h] = 0
for i in range(h,0,-1):
    for j in ab:
        tmp = max(0,i-j[0])
        dp[tmp] = min(dp[tmp], dp[i]+j[1])

print(dp[0])

Solution (Python):

import math
import operator as op
from functools import reduce
from fractions import Fraction as frac




def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom
    

def rd(p):
    if(p==0):
        return input()
    if(p==1):
        return int(input())
    if(p==2):
        return map(int,input().split())
    if(p==3):
        return list(map(int,input().split()))



global par


def find(u):
    if(par[u]<0):
        return u
    else:
        return find(par[u])
def uni(x,y):
    u=find(x)
    v=find(y)
    if u==v:
        return
    if(par[u]<par[v]):
        par[u]+=par[v]
        par[v]=u
    else:    
        par[v]+=par[u]
        par[u]=v

        

t=1
for term in range(1,t+1):
    h,n=rd(2)
    P=[list(map(int,input().split())) for i in range(n)]
    x=max(a for a,b in P)
    DP=[0]*(h+x)
    for i in range(1,h+x):
        DP[i]=min(DP[i-a]+b for a,b in P)
    print(DP[h])

Solution (Python):
def main():
    H, N, *AB = map(int, open(0).read().split())
    dp = [-1]*(H + 1)
    dp[0] = 0
    for i in range(N):
        a = AB[2 * i + 0]
        b = AB[2 * i + 1]
        for i in range(H + 1):
            d = dp[i] + b
            if dp[i] != -1:
                j = i + a
                j = min(j, H)
                if dp[j] == -1 or d < dp[j]:
                    dp[j] = d
    print(dp[H])

if __name__ == '__main__':
    main()

Solution (Python):



def input_int():
    return int(input())



def int1(x):
    return int(x) - 1



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



def input_to_int_tuple():
    return tuple(map(int, input().split()))



def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    h, n = input_to_int_map()
    ab = [input_to_int_tuple() for i in range(n)]

    x = max(a for a, b in ab)

    dp = [0] * (h + x)
    for i in range(1, h + 1):

        dp[i] = min(dp[i - a] + b for a, b in ab)

    return dp[h]


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

Solution (Python):
h,n=map(int,input().split())
a=[0]
b=[0]
for i in range(n):
    A,B=map(int,input().split())
    a.append(A)
    b.append(B)
dp=[[10**9 for j in range(h+max(a)+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(h+max(a)+1):
        if j>=a[i+1]:
            dp[i+1][j]=min(dp[i][j],dp[i+1][j-a[i+1]]+b[i+1])
        else:
            dp[i+1][j]=dp[i][j]
ans=10**9
for j in range(h,h+max(a)+1):
    if ans>dp[n][j]:
        ans=dp[n][j]
print(ans)

Solution (Python):
H,N=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N)]
dp=[0]*(H+10**5)
for i in range (1,H+1):
    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)]
x=max(a for a,b in ab)
dp=[0]*(H+x)
for i in range (1,H+1):
    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)]
x=max(a for a,b in ab)
dp=[0]*(H+x)
for i in range (1,H+x):
    dp[i]=min(dp[i-a]+b for a,b in ab)
print(dp[H])

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


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


dp[H] = 0

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

print(dp[0])

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

def main():
    H, N = map(int, input().split())
    inf = float('inf')
    AB = [tuple(map(int, input().split())) for _ in range(N)]
    dp = [inf] * (H+10**4+1)
    dp[0] = 0
    for h in range(H+1):
        for a, b in AB:
            dp[h+a] = min(dp[h+a], dp[h]+b)
    print(min(dp[H:]))


if __name__ == '__main__':
    main()

Solution (Python):
H, N = map(int,input().split())
M = [list(map(int,input().split())) for k in range(N)]
dp = [float("inf")]*(H+1)
dp[0] = 0

for e in M:
    a, b = e[0], e[1]
    for k in range(min(a+1,H+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())
AB = [tuple(map(int,input().split())) for i in range(N)]

INF = float('inf')
dp = [INF] * (H+1)
dp[H] = 0
for a,b in AB:
    for h in range(H,0,-1):
        i = max(0,h-a)
        dp[i] = min(dp[i], dp[h]+b)
print(dp[0])

Solution (Python):
h, n = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(n)]
A = [a for a, b in AB]
B = [b for a, b in AB]
inf = 10**10

DP = [[inf for j in range(h+1)] for i 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][j], DP[i+1][max(0, j-A[i])]+B[i])
print(DP[n][h])

Solution (Python):
h, n = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(n)]
A = [a for a, b in AB]
B = [b for a, b in AB]
inf = 10**10

DP = [[inf for j in range(h+1)] for i in range(n+1)]
DP[0][0] = 0
for i in range(1, n+1):
    for j in range(h+1):
        DP[i][j] = min(DP[i-1][j], DP[i][max(0, j-A[i-1])]+B[i-1])
print(DP[n][h])

Solution (Python):
def main():
  H, N = (int(_) for _ in input().split())
  A = [[int(_) for _ in input().split()] for __ in range(N)]
  INF = 10 ** 15
  dp = [[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])
      if A[i][0] <= j < H:
        dp[i+1][j] = min(dp[i+1][j], A[i][1] + dp[i+1][j-A[i][0]])
      elif j == H:
        for k in range(max(j - A[i][0], 0), j):
          dp[i+1][j] = min(dp[i+1][j], A[i][1] + dp[i+1][k])
  print(dp[N][H])
  return

if __name__ == '__main__':
  main()

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

INF = 10**18
dp = [INF]*(h+1)

dp[0]=0

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

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

Max_A = max(a for a, b in AB)


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

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 h in range(H):
    for i in range(N):
        
        dmg = min(h+ab[i][0],H)
        
        dp[dmg] = min(dp[dmg],dp[h]+ab[i][1])
print(dp[H])

Solution (Python):
H,n=map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(n)]
maxAi= max(a for a,b in AB)
dp=[0]*(H+maxAi)
i=1
while True:
  try:
    dp[i]=min(dp[i-a]+b for a,b in AB)
    i+=1
  except:
    print(min(dp[H:]))
    break

Solution (Python):
if __name__ == '__main__':
    H, N = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]
    maxA = max(AB)[0]

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

    for j in range(H + maxA, -1, -1):
        if j == 0:
            dp[0][j] = 0
        else:
            factor = j // AB[0][0] if j % AB[0][0] == 0 else j // AB[0][0] + 1
            dp[0][j] = factor * AB[0][1]

    for i in range(N):
        for j in range(H + maxA + 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])
            else:
                dp[i + 1][j] = dp[i][j]

    print(min([dp[N][j] for j in range(H, H + maxA + 1)]))

Solution (Python):
H, N = map(int,input().split())
magic = []
for i in range(N):
    a,b = map(int,input().split())
    magic.append((a, b))
magic.sort(reverse=True)
amax, _ = magic[0]

INF = 1<<60

def solve():
    btab = [INF] * (H+1)
    btab[0] = 0
    oneshot = min(amax, H)
    for h in range(1, oneshot+1):
        btab[h] = min([b for a, b in magic if a >= h])
    for h in range(2, H+1):
        for a, b in magic:
            c = b + btab[max(h-a, 0)]
            if c < btab[h]:
                btab[h] = c
    return btab[H]

print(solve())

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

Solution (Python):



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


dp = [float('INF')]*(H+1)
dp[0] = 0
mg_len = len(magic_list)


for i in range(1, H+1):
    for j in range(mg_len):
        a = magic_list[j][0]
        b = magic_list[j][1]
        last_p = i - a
        if last_p<0:
            last_p = 0
        tmp = dp[last_p] + b
        dp[i] = min(dp[i], tmp)


print(dp[H])

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

ma = max(a for a, b in ab)
dp = [0] * (h+ma)

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    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
    

    inf = 10**17
    

    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(h+1):
        for a,b in ab:
            if i+a > h:
                c = h
            else:
                c = i + a
            dp[c] = min(dp[c], dp[i]+b)

    print(dp[-1])

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    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
    

    inf = 10**17
    

    h,n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    dp = [inf] * (3*(10**4))
    dp[0] = 0
    for i in range(3*(10**4)):
        for a,b in ab:
            if i-a >= 0:
                dp[i] = min(dp[i], dp[i-a]+b)
    res = inf
    for i in range(h, 3*(10**4)):
        res = min(res, dp[i])
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    H, N = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp = [[10**9+7]*(H+1) for _ in range(N+1)]
    for i in range(N+1):
        dp[i][0] = 0
    for i in range(N):
        for j in range(1, H+1):
            lessHP = max(j - A[i], 0)
            dp[i+1][j] = min(dp[i+1][lessHP] + B[i], dp[i][j])
    print(dp[N][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 + 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 = zip(*[tuple(map(int, input().split()))for _ in range(n)])
INF = 10**18
dp = [INF]*(h+1)
dp[0] = 0
for j in range(h):
    for i in range(n):
        if h < j+A[i]:
            dp[-1] = min(dp[-1], dp[j]+B[i])
        else:
            dp[j+A[i]] = min(dp[j+A[i]], dp[j]+B[i])
print(dp[-1])

Solution (Python):
INF = 10 ** 9

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

    print(dp[H])

    
if __name__ == "__main__":
    main()

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

data = []

for i in range(N):
    a, b = map(int, input().split())
    data.append({'a': a, 'b': b})

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


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

for j in range(H+1):
    dp[0][j] = -(-j // data[0]['a']) * data[0]['b']

for i in range(1, N):
    power = data[i]['a']
    magic = data[i]['b']

    for j in range(H+1):
        dp[i][j] = dp[i - 1][j]
        if j <= power:
            dp[i][j] = min(dp[i][j], dp[i-1][max(0, j - power)] + magic)
        else:
            dp[i][j] = min(dp[i][j], dp[i][j - power] + magic)


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

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

A = [A_B[i][0] for i in range(N)]
B = [A_B[i][1] for i in range(N)]

M = H + max(A)



def crested(H, N, A, B):
    inf = float('inf')
    dp = [inf for j in range(M+1)]
    dp[0] = 0

    for i in range(N):
        for j in range(A[i],M):
            dp[j] = min(dp[j], dp[j-A[i]]+B[i])
    
    ans = inf
    for k in range(H, M):
        ans = min(ans, dp[k])
    print(ans)

crested(H, N, A, B)

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

A = []
B = []
for i in range(N):
    A.append(C[i][0])
    B.append(C[i][1])

M = max(A)
m = min(A)

dp = {} 

for i in range(-M+1,1):
    dp[i] = 0

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

print(min(dp[i] for i in range(H,H+m)))

Solution (Python):
H,N = map(int,input().split())
magicL =[]
for x in range(N):
    magic = list(int(i) for i in input().split())
    magicL.append(magic)
 
ml = magicL
dp = [10**9 for i in range(H +1)]
dp [0] = 0
for i in range(N):
    for w in range(H +1):
        dp[min(w+ml[i][0],H)] = min(dp[min(w+ml[i][0],H)],dp[w]+ml[i][1])
print(dp[H])

Solution (Python):
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
maxAi = max(a for a,b in AB)
dp = [0]*(H+maxAi)

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def solve():
    h, n = LI()
    ab = LIR(n)
    maxa = max(map(lambda x: x[0], ab))
    rangeh = h + maxa
    dp = [inf] * (rangeh + 1)
    dp[0] = 0
    for a,b in ab:
        for hi in range(rangeh - a):
            dp[hi + a] = min(dp[hi + a], dp[hi] + b)
        for hi in range(rangeh-1, -1, -1):
            dp[hi] = min(dp[hi], dp[hi + 1])
    print(dp[h])
    return



if __name__ == '__main__':
    solve()

Solution (Python):
H, N = map(int, input().split())
dp = [10 ** 100] * H
for _ in range(N):
    A, B = map(int, input().split())
    for i in range(H - 1, -1, -1):
        if A + i >= H:
            dp[i] = min(dp[i], B)
        else:
            dp[i] = min(dp[i], dp[A + i] + B)
print(dp[0])

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

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

h,n = l_in()
ab = []
for _ in range(n):
    ab.append(l_in())
    

dp = [10**8 for _ in range(2*h)]

dp[0] = 0

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

print(min(dp[h:]))

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

print(dp[-1])

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

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

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

print(int(dp[n][h]))

Solution (Python):


h, n = map(int, input().split())

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


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

print(int(dp[n][h]))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, n = map(int, readline().split())
ab = [list(map(int, readline().split())) for i in range(n)]
inf = float('inf')
dp = [inf] * (2 * 10 ** 4)
dp[0] = 0
for a, b in ab:
    for n in range(h):
        if dp[n + a] > dp[n] + b:
            dp[n + a] = dp[n] + b

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

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

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

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

print(dp[N][H])

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, N = lr()
INF = 10 ** 15
limit = H + 100
dp = [INF] * limit
dp[0] = 0
for _ in range(N):
    a, b = lr()
    for i in range(H):
        index = i+a
        if index > limit - 1:
            index = limit - 1
        dp[index] = min(dp[index], dp[i] + b)

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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, n = list(map(int, readline().split()))
    ab = [[0] * 2 for _ in range(n)]

    for i in range(n):
        ab[i] = list(map(int, readline().split()))

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

    for i in range(h + 1):
        mpcur = dp[i]
        for a, b in ab:
            hpnext = min(i + a, h)
            dp[hpnext] = min(mpcur + b, dp[hpnext])

    print(dp[h])


if __name__ == '__main__':
    main()

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, N = lr()
INF = 10 ** 15
limit = H + 100
dp = [INF] * limit
dp[0] = 0
for _ in range(N):
    a, b = lr()
    for i in range(limit):
        index = i+a
        if index > limit - 1:
            index = limit - 1
        dp[index] = min(dp[index], dp[i] + b)

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

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 = [INF] * 20007
dp[0] = 0
for a, b in AB:
    for i in range(20007):
        if i-a >= 0:
            dp[i] = min(dp[i], dp[i-a] + b)
ans = min(dp[H:])
print(ans)

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 = [INF] * 20007
dp[0] = 0
for i in range(20007):
    for a, b in AB:
        if i-a >= 0:
            dp[i] = min(dp[i], dp[i-a] + b)
ans = min(dp[H:])
print(ans)

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


inf=float('inf')
component=[0]
for i in range(H):
    component.append(inf)
DP=[]
for j in range(N+1):
    DP.append(copy.copy(component))


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

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


inf=float('inf')
component=[0]
for i in range(H):
    component.append(inf)
DP=[]
for j in range(N+1):
    DP.append(component)


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

print(DP[N][H])