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):
import sys
import numpy as np

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

H, N = lr()

U = 20000 

INF = 10 ** 10
dp = np.full(U, INF, np.int64)
dp[0] = 0

for _ in range(N):
    a, b = lr()
    n = (U + (-U)%a) // a
    dp = np.resize(dp, U + (-U)%a).reshape(-1, a)
    
    dp -= (np.arange(n) * b)[:, None]
    dp = np.minimum.accumulate(dp, axis=0)
    
    dp += (np.arange(n) * b)[:, None]
    dp = np.ravel(dp)[:U]

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

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

DP = [0] * (H + 1)

for i in range(len(DP)):
    if i == 0 or DP[i] != 0:
        for j in range(N):

            if i + A[j] <= H:
                if DP[i + A[j]] != 0:
                    DP[i + A[j]] = min(DP[i + A[j]], DP[i] + B[j])
                else:
                    DP[i + A[j]] = DP[i] + B[j]

            else:
                if DP[H] == 0:
                    DP[H] = DP[i] + B[j]
                else:
                    DP[H] = min(DP[H], DP[i] + B[j])

print(DP[H])

Solution (Python):

import sys
sys.setrecursionlimit(1000000)

from heapq import heappush, heappop, heapify

iim = lambda: map(int, input().rstrip().split())

def resolve():
    H, N = iim()
    AB = [tuple(iim()) for i in range(N)]
    inf = float("inf")
    AB.sort(key=lambda a: (a[0]/a[1], -a[0]), reverse=True)

    def memo(f):
        cache = {}
        def wrapper(*a):
            if a in cache:
                return cache[a]
            b = f(*a)
            cache[a] = b

            return b

        return wrapper

    @memo
    def calc(h):
        if h <= 0: return 0

        bi = inf
        for a, b in AB:
            bj = b + calc(h - a)
            if bi > bj:
                bi = bj
            else:
                break
        return bi

    print(calc(H))



if __name__ == "__main__":
    resolve()

Solution (Python):
h, ｎ = map(int, input().split())
dp = [-1] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  if dp[i] == -1: continue
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    if dp[ht] != -1:
      dp[ht] = min(dp[ht], dp[i] + j[1])
    else:
      dp[ht] = dp[i] + j[1]

print(dp[h])

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

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
INF = 10 ** 15

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

print(dp[-1])

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

dp = [[10**10]*(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+magic[i][0], H)] = min(dp[i+1][min(j+magic[i][0], H)], dp[i+1][j] + magic[i][1])
print(dp[N][H])

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

    max_A = max(a for a, _ in AB)
    inf = 10 ** 9
    dp = [inf] * (H + max_A+ 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == inf:
            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):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,n = inpl()
att = []
cost = []
for i in range(n):
    a,b = inpl()
    att.append(a); cost.append(b)
dp = [[INF for i in range(h+1)] for j in range(n+1)]
dp[0][0] = 0

for i in range(n):
    cnt = h//att[i]
    for j in range(h+1):
        if att[i] >= j:
            dp[i+1][j] = min(dp[i][j], cost[i])
            continue
        dp[i+1][j] = min(dp[i][j], cost[i]+dp[i+1][j-att[i]])

print(dp[n][h])

Solution (Python):
h, n = map(int, input().split())
l = []
for i in range(n):
    a, b = map(int, input().split())
    l.append([a, b])
INF = 10 ** 9 + 11
dp = [INF] * (h + 1)
dp[0] = 0
nextj = 0
for i in range(n):
    for j in range(h):
        nextj = j + l[i][0]
        if nextj <= h:
            dp[nextj] = min(dp[nextj], dp[j] + l[i][1])
        else:
            dp[h] = min(dp[h], dp[j] + l[i][1])
print(dp[-1])

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

    max_A = max(a for a, _ in AB)
    dp = [100000000] * (H + max_A+ 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):
import numpy as np
h, N = map(int,input().split())
VW = [[int(x) for x in input().split()] for _ in range(N)]
VW = np.array(sorted(VW, key=lambda x:(x[0]/x[1],x[0])))
a = VW[:, 0]
b = VW[:, 1]

dp = np.zeros(10**4+1, dtype=np.int)
for i in range(1, h+1):
    dp[i] = (dp[i-a]+b).min()
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)]
ab = np.array(ab)
 
a = ab[:, 0]
b = ab[:, 1]
 
dp = np.zeros(100001, dtype=np.int)
for i in range(1, h+1):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Solution (Python):
h,n=map(int,input().split())
magi=[list(map(int,input().split())) for i in range(n)]
magi.sort(reverse=True)
infi=10**9
dp=[infi]*(2*10**4)
dp[0]=0
for a,b in magi:
  for i in range(h+1):
    if dp[i]!=infi:
      dp[i+a]=min(dp[i]+b,dp[i+a])
print(min(dp[h:]))

Solution (Python):
import sys

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

dp = [sys.maxsize for i in range(H+1)]
dp[0] = 0


for i in range(H):
    if dp[i] == sys.maxsize:
        continue
    
    for j in range(N):
        a = min(i + AB[j][0], H)
        b = dp[i] + AB[j][1]
        if dp[a] > b:
            dp[a] = b

print(dp[H])

Solution (Python):
import math
INF = 1001001001
h, n = map(int, input().split())
ab = list(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):
        nj = min(j+ab[i][0], h)
        dp[nj] = min(dp[nj], dp[j]+ab[i][1])

print(dp[h])

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

f_inf = float('inf')
dp = [f_inf] * (H + 1)
dp[0] = 0
for h in range(H):
    for damage, cost in AB:
        next_index = min(h + damage, H)
        dp[next_index] = min(dp[next_index], dp[h] + cost)

print(dp[H])

Solution (Python):

H, N = map(int, input().split())
A_to_minB = {}
max_A = -1
for i in range(N):
    a, b = map(int, input().split())
    max_A = max(max_A, a)
    if a not in set(A_to_minB.keys()):
        A_to_minB[a] = b
    else:
        A_to_minB[a] = min(A_to_minB[a], b)
dp = [float("inf")] * (H + max_A)
dp[0] = 0
for i in range(1, H+max_A):
    tmp = [dp[i-a]+b for a, b in A_to_minB.items() if a<=i]
    if tmp:
        dp[i] = min(tmp)
print(min(dp[H:H+max_A]))

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

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

a = ab[:, 0]
b = ab[:, 1]

dp = np.zeros(100001, dtype=np.int)
for i in range(1, h+1):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

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

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

dp[0] = 0

for a, b in M:
    for i in range(H+1):
        d = min(H, i + a)
        dp[d] = min(dp[d], dp[i] + b)

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


INF=10**9
dp=[INF for i in range(H+max(A))]
dp[0]=0

for i in range(H):
    for j in range(N):
        p1=dp[i+A[j]]
        p2=dp[i]+B[j]
        dp[i+A[j]]=min(p1,p2)

print(min(dp[H:]))

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

ab = []
for i in range(n):
    ab.append([int(i) for i in input().split()])

mp = [float("inf") for i in range(h+1)]
mp[0] = 0

for i in range(h):
    for j in range(n):
        if i + ab[j][0] >= h:
            mp[h] = min(mp[h], mp[i]+ab[j][1])
        else:
            mp[i+ab[j][0]] = min(mp[i+ab[j][0]], mp[i] + ab[j][1])

print(mp[-1])

Solution (Python):
import sys
from collections import deque, defaultdict
from itertools import combinations, permutations
from heapq import heapify, heappop, heappush

def solve():
    input = sys.stdin.readline
    H, N = map(int, input().split())
    M = [tuple(int(a) for a in input().split()) for _ in range(N)]
    DP = [1000000000] * (H + 2) 
    DP[0] = 0
    for i, m in enumerate(M):
        a, b = m
        for j in range(max(H-a+1, 0)): DP[j + a] = min(DP[j + a], DP[j] + b)
        for j in range(max(H-a+1, 0), H): DP[H + 1] = min(DP[H + 1], DP[j] + b)
    print(min(DP[H], DP[H + 1]))

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
def main():
    H, N = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    dp = [10**12 for i in range(H+1)]  
    dp[0] = 0
    for h in range(H):
        for damage, cost in AB:
            next_index = min(h + damage, H)
            dp[next_index] = min(dp[next_index], dp[h] + cost)
    print(dp[H])


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import numpy as np

h, n = map(int, input().split())
ab = np.array(sys.stdin.read().split(), dtype=np.int64)
aaa = ab[0::2]
bbb = ab[1::2]

dp = np.zeros(10001, dtype=np.int64)
for i in range(1, h + 1):
    dp[i] = (dp[i - aaa] + bbb).min()
print(dp[h])

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

md = {}
max_d = 0
for _ in range(N):
    d, m = [int(i) for i in input().split()]
    if m not in md:
        md[m] = 0
    md[m] = max(md[m], d)
    max_d = max(max_d, d)

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

for i in range(1, H + max_d + 1):
    for m, d in md.items():
        if 0 <= i - d <= H + max_d:
            dp[i] = min(dp[i], dp[i-d] + m)
ans = min(dp[H:])
print(ans)

Solution (Python):
import collections
import time


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

INF = 10 ** 20

dp = collections.defaultdict(lambda : INF)
dp[0] = 0

magic = []

for i in range(n):
    magic.append(list(map(int, input().split())))


t = time.time()

que = [0]
cc = [0]
m = 0
c = 0
while que:
    next_que = set()
    for now_dam in que:
        for a, b in magic:
            dam = now_dam + a
            if dp[dam] > dp[now_dam] + b:
                dp[dam] = dp[now_dam] + b
                if h > dam:
                    next_que.add(dam)
    que = list(next_que)
ans = INF


for k, v in dp.items():
    if k >= h:
        ans = min([v, ans])

print(ans)

Solution (Python):
import collections
import time

def main():
    h, n = map(int, input().split())

    INF = 10 ** 20

    dp = collections.defaultdict(lambda : INF)
    dp[0] = 0

    magic = []

    for i in range(n):
        magic.append(list(map(int, input().split())))


    t = time.time()

    que = [0]
    cc = [0]
    m = 0
    c = 0
    while que:
        next_que = set()
        for now_dam in que:
            for a, b in magic:
                dam = now_dam + a
                if dp[dam] > dp[now_dam] + b:
                    dp[dam] = dp[now_dam] + b
                    if h > dam:
                        next_que.add(dam)
        que = list(next_que)
    ans = INF
    

    for k, v in dp.items():
        if k >= h:
            ans = min([v, ans])

    print(ans)

    

main()

Solution (Python):
import time

def main():
    h, n = map(int, input().split())
    INF = 10 ** 9
    magic = []

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

    

    magic.sort(reverse=True, key=lambda x: x[0])

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

    for a, b in magic:
        for i in range(h):
            if dp[i] != INF:
                tmp = dp[i] + b
                if tmp < dp[i + a]:
                    dp[i + a] = tmp

    ans = min(dp[h:])

    print(ans)
    

main()

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

f_inf = float("inf")
dp = [f_inf] * (h + 1)
dp[0] = 0
for i in range(n):
    a, b = map(int, input().split())
    for j in range(h):
        next_j = min(j + a, h)
        dp[next_j] = min(dp[next_j], dp[j] + b)

print(dp[h])

Solution (Python):
import time

def main():
    h, n = map(int, input().split())
    INF = 10 ** 9
    magic = []

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

    

    magic.sort(reverse=True, key=lambda x: x[0])

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

    for a, b in magic:
        for i in range(h):
            if dp[i] != INF:
                tmp = dp[i] + b
                if tmp < dp[i + a]:
                    dp[i + a] = tmp

    ans = min(dp[h:])

    print(ans)
    

main()

Solution (Python):
from sys import stdin

h, n = map(int, stdin.readline().split())
attack = [None] * n
cost = [None] * n
for i in range(n):
    a, c = map(int, stdin.readline().split())
    attack[i] = a
    cost[i] = c
dp = [0] + [float('inf')] * h
for i in range(1, h + 1):
    for j in range(n):
        dp[i] = min(dp[i], cost[j] + dp[max(0, i - attack[j])])
print(dp[h])

Solution (Python):
def main():
    inf = 10**9
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))
 
 
main()

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

f_inf = float("inf")
dp = [f_inf] * (h + max(a))
dp[0] = 0
for i in range(n):
    for j in range(h):
        if dp[j] != f_inf:
            if dp[j + a[i]] > dp[j] + b[i]:
                dp[j + a[i]] = dp[j] + b[i]

print(min(dp[h:]))

Solution (Python):
import numpy as np
H,N=map(int, input().split())
A = [None]*N
B = [None]*N
for i in range(N):
  A[i],B[i]=map(int,input().split())
dp=[10**8]*(H+1)
A = np.array(A)
B = np.array(B)
dp = np.array(dp)
dp[0]=0
for i in range(1,H+1):
  x=np.amin((dp[np.maximum(0,i-A)]+B))
  dp[i]=min(x,dp[i])
print(dp[-1])

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

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

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

print(min(dp[h:]))

Solution (Python):
def main():
    H, N = map(int, input().split())
    dp = [10 ** 18] * (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])


main()

Solution (Python):
H, N = map(int, input().split())
dp = [10 ** 18] * (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):
H,N=map(int,input().split())
A=[0]*N
B=[0]*N
for i in range(N):
  A[i],B[i]=map(int,input().split())

INF=10**9
dp=[INF]*(H+max(A))

dp[0]=0
for i in range(N):
  for j in range(H):
    if dp[j]!=INF:
      if dp[j+A[i]]>dp[j]+B[i]:
        dp[j+A[i]]=dp[j]+B[i]
print(min(dp[H:]))

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

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

for i in range(-max_a, h+max_a):
    if i <= 0:
        dp[i] = 0
    else:
        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)]

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

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

Solution (Python):
import sys

def input() -> str:
    return sys.stdin.readline().rstrip()

h, n = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
m = max(a for a, _ in ab)
dp = [0] * (h + m * 2 + 1)
for i in range(-m, h + m + 1):
    if i <= 0:
        dp[i] = 0
    else:
        dp[i] = min(dp[i - a] + b for a, b in ab)
print(min(dp[h:h + m + 1]))

Solution (Python):
import sys

def input() -> str:
    return sys.stdin.readline().rstrip()

h, n = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
m = max(a for a, _ in ab)
dp = [0] * (h + m * 2 + 1)
for i in range(-m, h + m + 1):
    if i <= 0:
        dp[i] = 0
    else:
        dp[i] = min(dp[i - a] + b for a, b in ab)
print(min(dp[h:h + m + 1]))

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

ini_A,ini_B = A[0], B[0]
dp = [-(-j//ini_A)*ini_B for j in range(H+1)]

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

print(dp[-1])

Solution (Python):

def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H):
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        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):

def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  
    dp[0] = 0
    for i in range(H):
        for a, b in zip(*[iter(AB)] * 2):
            dp[i + a] = min(dp[i + a], dp[i] + b)
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  
    dp[0] = 0
    for i in range(H):
        if dp[i] == INF:
            continue
        for a, b in zip(*[iter(AB)] * 2):
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  
    dp[0] = 0
    for i in range(H):
        if dp[i] == INF:
            continue
        for a, b in zip(*[iter(AB)] * 2):
            dp[i + a] = min(dp[i + a], dp[i] + b)
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    INF = 1 << 30
    
    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 1)  
    dp[0] = 0
    for i in range(H):
        if dp[i] == INF:
            continue
        for a, b in zip(*[iter(AB)] * 2):
            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])


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    H, N, *AB = map(int, open(0).read().split())
    dp = defaultdict(lambda: 1 << 30)  
    dp[0] = 0
    for i in range(H):
        if i not in dp:
            continue
        for a, b in zip(*[iter(AB)] * 2):
            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])


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    H, N, *AB = map(int, open(0).read().split())
    dp = defaultdict(lambda: 1 << 30)  
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H):
            if i not in dp:
                continue
            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])


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict


def main():
    H, N, *AB = map(int, open(0).read().split())
    dp = defaultdict(lambda: 1 << 30)  
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H):
            if i not in dp:
                continue
            dp[i + a] = min(dp[i + a], dp[i] + b)
    ans = min(magic for health, magic in dp.items() if health >= H)
    print(ans)


if __name__ == "__main__":
    main()

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

dp = [10 ** 20] * 10 ** 4 * 2
dp[0] = 0
for i in range(h):
    for a, b in ab:
        dp[i + a] = min(dp[i + a], dp[i] + b)
print(min(dp[h:]))

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

dp = [10 ** 20] * 10 ** 4 * 2
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:]))

Solution (Python):
import math
H,N=map(int,input().split())
A=[0 for i in range(N)]
B=[0 for i in range(N)]
dp=[[0 for j in range(H)] for i in range(N)]
for i in range(N):
    A[i],B[i]=map(int,input().split())

for i in range(N):
    for j in range(H):
        if i==0:
            dp[i][j]=B[i]*math.ceil((j+1)/A[i])
        elif j+1<=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])

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

Solution (Python):
import sys

stdin = sys.stdin

mod = 1000000007
inf = 1 << 60

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

h, n = na()
ab = [na() for i in range(n)]
max_a = max([ab[i][0] for i in range(n)])
dp = [inf] * (h + max_a)
dp[0] = 0

for i in range(n):
    for j in range(h):
        a, b = ab[i]
        m = min(j + a, h)
        dp[m] = min(dp[m], dp[j] + b)

print(dp[h])

Solution (Python):
from sys import stdin

h, n = (int(v) for v in stdin.readline().split())
spells = [tuple(int(v) for v in stdin.readline().split()) for i in range(n)]



costs = [0]
for i in range(1, h + 1):
    lowest_cost = None
    for damage, cost in spells:
        if damage < i:
            total_cost = cost + costs[i - damage]
        else:
            total_cost = cost
        if lowest_cost is None or total_cost < lowest_cost:
            lowest_cost = total_cost
    costs.append(lowest_cost)

print(costs[h])

Solution (Python):
import numpy as np

h, n = map(int, input().split())
a = []
b = []
for _ in range(n):
  ai, bi = map(int, input().split())
  a.append(ai)
  b.append(bi)
a = np.array(a)
b = np.array(b)
dp = np.zeros(h+1, dtype=np.int)
for c in range(1, h+1):
  dp[c] = np.min(dp[np.maximum(0, c-a)] + b)
print(dp[-1])

Solution (Python):
def main():
    inf = 10**9
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))


main()

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

Solution (Python):

     

import math



H,N = map(int,input().split())
Damage = []
Cost = []


DP = [0] + [pow(10,9)] * (H+1)



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





for i in range(1,len(DP)):
    for spell in range(N):
        DP[i] = min(DP[i],DP[max(0,i-Damage[spell])]+Cost[spell])




print (DP[H])

Solution (Python):
import sys
input = sys.stdin.readline
inf = 10**9
H, N = map(int, input().split())
AB = []
for n in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort(reverse=True, key=lambda x: x[0])
dp = [inf] * (H + 10 ** 4)
dp[0] = 0
for a, b in AB:
    for h in range(H):
        if dp[h] != inf:
            tmp = dp[h] + b
            if tmp < dp[h + a]:
                dp[h + a] = tmp
print(min(dp[H:]))

Solution (Python):
inf = 10**9
H, N = map(int, input().split())
AB = []
for n in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB.sort(reverse=True, key=lambda x: x[0])
dp = [inf] * (H + 10 ** 4)
dp[0] = 0
for a, b in AB:
    for h in range(H):
        if dp[h] != inf:
            tmp = dp[h] + b
            if tmp < dp[h + a]:
                dp[h + a] = tmp
print(min(dp[H:]))

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


dp = [10 ** 9 + 7 for i in range(h + max_a)]
dp[0] = 0
for i in range(h + max_a):
    for j in magics:
        dp[i] = min(dp[i], dp[max(i - j[0], 0)] + j[1])

print(dp[h])

Solution (Python):
def main():
    inf = 10**9
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))


main()

Solution (Python):
inf = 10 ** 9


def main():
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            
            
            
            
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))


main()

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+1)
    dp[0] = 0

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

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

    print(dp[H])


if __name__ == '__main__':
    main()

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

INF = float('inf')

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

print(dp[N][H])

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


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

dp[0]=0
for _ in range(n):
    a, b = map(int, input().strip().split())
    for i in range(h):
        ni=min(i+a,h)
        dp[ni] = min(dp[ni],dp[i]+b)
        
print(dp[h])