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 = []
b = []
for _ in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)

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

ans = float('inf')
for j in range(h, 2 * 10000 + 1):
    ans = min(ans, dp[j])
print(ans)

Solution (Python):
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
MP = [0 for _ in range(H+1)]
tmp = [0 for _ in range(N)]
for h in range(1, H+1):
    for n in range(N):
        if A[n] >= h:
            tmp[n] = B[n]
        else:
            tmp[n] = MP[h-A[n]]+B[n]
    MP[h] = min(tmp)
print(MP[H])

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

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

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

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

inf=float("inf")
dp=[inf]*(H+1)
dp[0]=0
ans=inf
for i in range(N):
    A,B=AB[i]
    for j in range(max(0,H-A+1)):
        dp[j+A]=min(dp[j+A],dp[j]+B)
    for j in range(max(0,H-A+1),H):
        ans=min(ans,dp[j]+B)
ans=min(ans,dp[H])

print(ans)

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

Solution (Python):
h, n = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]
MOD = 10 ** 18
D = [MOD] * (h + 10 ** 4)
D[0] = 0
 
for i in range(h):
    for a, b in AB:
        D[i + a] = min(D[i + a], D[i] + b)
print(min(D[h:]))

Solution (Python):



import bisect

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

ab = []
max_a = 0
for _ in range(n):
    ab.append(list(map(int, list(input().split()))))
    max_a = max(max_a, ab[-1][0])

dp = [int(1e9)] * int(1e6)
checked = [False] * int(1e6)
dp[0] = 0

sorted_list = [0]
while len(sorted_list) > 0:
    v = sorted_list.pop(0)
    
    if v > h:
        break
    
    for i in range(n):
        if checked[v + ab[i][0]] == False:
            bisect.insort_right(sorted_list, v + ab[i][0])
            checked[v + ab[i][0]] = True
        dp[v + ab[i][0]] = min(dp[v + ab[i][0]], dp[v] + ab[i][1])
    
print(min(dp[v-1:]))

Solution (Python):
import sys
from bisect import bisect_left
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

H, N = map(int, readline().split())
m = map(int, read().split())
AB = zip(m, m)

INF = 10**18

dp = [INF] * (H + (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

print(min(dp[H:]))

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

Solution (Python):
import math

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



    


table = [[0] * H for i in range(N)]

for i in range(H):
    table[0][i] = List[0][1] * (math.ceil((i+1) / List[0][0]))
    
for j in range(1,N):
    attack = List[j][0]
    MP = List[j][1]
    for i in range(H):
        if (i+1) < attack:
            table[j][i] = min(table[j-1][i], MP)
        else:
            table[j][i] = min(table[j-1][i], table[j][i-attack]+MP)
            
print(table[N-1][H-1])

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 ** 10
dp = [INF] * (H+10**4+10)
dp[0] = 0
for i in range(N):
    a, b = lr()
    for j in range(a, H+a+1):
        if dp[j-a] == INF:
            continue
        dp[j] = min(dp[j], dp[j-a] + b)

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

Solution (Python):
h, n = map(int, input().split())
magics = []
for i in range(n):
    a, b = map(int, input().split())
    magics.append((a, b))
h_table = [float("inf") for _ in range(h + 1)]
h_table[0] = 0
for i in range( h + 1):
    for a, b in magics:
        h_table[min(i + a, h)] = min(h_table[min(i + a, h)], h_table[i] + b)
print(h_table[h])

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

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

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

Solution (Python):
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)

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

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


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


AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
bestA, bestB = AB[0]

print(f(H))

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

dp=[100000000 for i in range(h+1)]
dp[0]=0
for i in range(h):
    for q in range(n):
        power=L[q][1]
        magic=L[q][2]
        dp[min(h,i+power)]=min(dp[min(h,i+power)],dp[i]+magic)

print(dp[h])

Solution (Python):
def sol():
    h,n=map(int,input().split())
    l=[]
    ans=0
    for i in range(n):
        a,b=map(int,input().split())
        l.append((a,b))
    d=[0]*20005
    for i in range(h):
        for j in l:
            a,b=j[0],j[1]
            x=d[i]+b
            if i==0 or (d[i]!=0 and d[i+a]==0):
                if d[i+a]==0:
                    d[i+a]=x
                else:
                    d[i+a]=min(d[i+a],x)
            elif d[i]!=0 and d[i+a]!=0:
                d[i+a]=min(d[i+a],x)

    for i in range(h,20005):
        if ans==0:
            ans=d[i]
        elif d[i]!=0:
            ans=min(ans,d[i])
    print(ans)

if __name__=="__main__":
    sol()

Solution (Python):
import bisect
H,N = [int(x) for x in input().split()]
AB = [[int(x) for x in input().split()] for _ in range(N)]
dp = [[10**18]*(10**4+1) for _ in range(N+1)]
for i in range(N+1):
  dp[i][0]=0
mi=10**9
for i in range(N):
  for w in range(10**4+1):
    dp[i+1][w]=min(dp[i+1][w],dp[i][w])
    if w-AB[i][0]>=0:
      dp[i+1][w]=min(dp[i+1][w],dp[i + 1][w - AB[i][0]] + AB[i][1])
for i in range(H,10**4+1):
  mi = min(mi,dp[N][i])
print(mi)

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

dp = [0] * (H+1)

for i in range(1, 1+H):
  dp[i] = 10 ** 8
  for A, B in AB:
    if i >= A:
      t = dp[i - A] + B
      if t < dp[i]:
        dp[i] = t
    else:
      if B < dp[i]:
        dp[i] = B


print(dp[-1])

Solution (Python):
from math import ceil, floor

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

if n == 1:
  print(ceil(h/l[0][0]) * l[0][1])
  exit(0)

dp = [ceil(hp/l[0][0]) * l[0][1] for hp in range(h + 1)]

for i in range(1, n):
  for hp in range(h + 1):
    if hp >= l[i][0]:
      dp[hp] = min([dp[hp], dp[hp - l[i][0]] + l[i][1]])
    else:
      dp[hp] = min([dp[hp], l[i][1]])

print(dp[-1])

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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 <= AB[i][0]:
            dp[i+1][j] = min(AB[i][1], dp[i][j])
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j - AB[i][0]]+AB[i][1])

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

print(answer)

Solution (Python):
import sys
input = sys.stdin.readline
h,n = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]

D = [float("inf")] * (h+10**4+3)
D[0] = 0
for j in range(n):
    for i in range(len(D)):
        D[i] = min(D[i],D[i-A[j][0]] + A[j][1])
    

print(min(D[h:]))

Solution (Python):
import sys

use_clipboard=True
bbn=1000000007
	


		
def main():
	h, n = map(int, input().split())
	ii=[]
	for i in range(n):
		a,b=map(int, input().split())
		ii.append([a/b, a, b])
	
	
	_,ma,mb = ii[0]
	tn = (h + ma-1)//ma
	tc = tn * mb
	
	vv=[bbn]*(h+1)
	vv[0]=0
	for i in range(h+1):
		for c,a,b in ii:
			nn = min(i + a, h)
			vv[nn]= min(vv[i] + b, vv[nn])
	
	print(vv[-1])
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
class input_clipboard:
	def __init__(self,s):
		self.input_l=s.splitlines()
		self.ii=0
		
	def input(self):
		ret = self.input_l[self.ii]
		self.ii+=1
		return ret

if __name__ == "__main__":
	if sys.platform =='ios':
		if use_clipboard:
			import clipboard
			input_text=clipboard.get()
			ic=input_clipboard(input_text)
			input = lambda : ic.input()
		else:
			sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
def main():
    H,N = map(int, input().split())
    weight = list(); value = list()
    for n in range(N):
        temp = input().split()
        weight.append(int(temp[0]))
        value.append(int(temp[1]))
    
    inf = float("inf")
    dp = [[inf for i in range(H+max(weight)+1)] for j in range(N+1)]
    for i in range(N+1): dp[i][0] = 0
    for i in range(N):
        for h in range(H+max(weight)+1):
            if(weight[i] <= h):
                dp[i+1][h]=min(dp[i][h-weight[i]]+value[i], dp[i+1][h-weight[i]]+value[i], dp[i][h])
            else:
                dp[i+1][h]=dp[i][h]
    res = int(min(dp[N][H:]))
    print(res)



if __name__ == "__main__":
    main()

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):
    ans = 0
    abc = [(a/b, b, a) for a, b in AB]

    abc.sort(key=lambda x: x[1])
    abc.sort(key=lambda x: x[2])
    abc.sort(key=lambda x: x[0], reverse=True)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    HD = defaultdict(lambda : INF)
    HD[H] = 0
    _, b, a = abc[0]
    for i in range(1, dc(H, a)+1):
        HD[H - i*a] = i*b
    for _, b, a in abc[1:]:
        for k, v in reversed(list(HD.items())):
            for i in range(1, dc(k, a)+1):
                if HD[k - i*a] > v+i*b:
                    HD[k- i*a] = v+i*b
                else:
                    break
    ans = INF
    for k, v in HD.items():
        if k <= 0:
            ans = min(ans, v)


    return ans


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()))
lst = [list(map(int, input().split())) for i in range(n)]

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

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

print(dp[h])

Solution (Python):
H,N = map(int,input().split(" "))
lis = []
lis_append = lis.append
dp = [2**50 for _ in range(H+1)]
ans = 2**60
for i in range(N):
    a,b = map(int,input().split(" "))
    if a >= H:
        ans = min(ans,b)
    lis_append([a,b])

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

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

Solution (Python):
import numpy as np

H, N = map(int, input().split())
A, B = np.zeros(N, dtype='int'), np.zeros(N, dtype='int')
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

dp = np.zeros(H, dtype='int')
minimum_cost_magic = np.argmin(B)
minA, minB = A[minimum_cost_magic], B[minimum_cost_magic]
dp[:minA] = minB
for h in range(minA, H):
    mincost = 10*dp[h-1]
    dp_minusAmj = dp[h - A]
    dp_minusAmj[h - A < 0] = 0
    dp[h] = np.min(dp_minusAmj + B)

print(dp[H-1])

Solution (Python):
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)

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

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

'''
H = 10000
N = 1000
import random
AB = []
for i in range(N):
    A = random.randint(1, 10000)
    B = random.randint(1, 10000)
    AB.append((A, B))
'''




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

AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
bestA, bestB = AB[0]
threshold = sum(a for a, b in AB)

ans = None
times = (H - threshold) // bestA
if times > 0:
    ans = bestB * times + f(H - bestA * times)
else:
    ans = f(H)

print(ans)

Solution (Python):

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

list = [[0] * 2 for i in range(n)]
for i in range(n):
    list[i][0], list[i][1] = map(int,input().split())

dp = [[10000000000] * (hp+1) for i in range(n+1)]

dp[0][0] = 0
for i, lista in enumerate(dp):
    dp[i][0] = 0
    for j, value in enumerate(lista):
        if i == 0:
            continue
        if j < list[i-1][0]:
            dp[i][j] = min(list[i-1][1], dp[i-1][j])
        else:
            target = j - list[i-1][0]
            dp[i][j] = min((dp[i][target] + list[i-1][1]), dp[i-1][j])

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

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

nums=[-1]*20001
for i in range(N):
  if nums[AB[i][0]]<0:
    nums[AB[i][0]]=AB[i][1]
  else:
    nums[AB[i][0]]=min(AB[i][1], nums[AB[i][0]])
  
for i in range(1, H):
  if nums[i]>0:
    for j in range(N):
      if nums[i+AB[j][0]]>0:
        if nums[i+AB[j][0]]>nums[i]+AB[j][1]:
          nums[i+AB[j][0]]=nums[i]+AB[j][1]
      else:
        nums[i+AB[j][0]]=nums[i]+AB[j][1]
      
anss=[]
for i in range(H, 20000):
  if nums[i]>0:
    anss.append(nums[i])
    
print(min(anss))

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

for i in range(1,H+1):
    DP[0][i] = B[0] * -(-i//A[0])

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

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

Solution (Python):
MAX = 10**9 + 7
 
 
H, N = map(int, input().split())
damage_costs = [tuple(map(int, input().split())) for _ in range(N)]

dp = [MAX] * (H + 1 + max(damage_costs, key=lambda x: x[0])[0])
dp[0] = 0

for (ai, bi) in damage_costs:
    for i, dp_i in enumerate(dp):
        if i >= ai:
            dp[i] = min(dp_i, dp[i - ai] + bi)

print(min(dp[H:]))

Solution (Python):
import sys
input = sys.stdin.readline
h, n = map(int, input().split())
ab = []
ma = 0
for i in range(n):
    a, b = map(int, input().split())
    ma = max(ma, a)
    ab.append([a, b])
ab.sort(key = lambda x:x[1]/x[0])
INF = 10 ** 9
dp = [[INF for i in range(h + ma + 1)] for j in range(n+1)]
ans = INF
for i, (a, b) in enumerate(ab):
    dp[i + 1][0] = 0
    for j in range(a, h + ma + 1):
        
        dp[i + 1][j] = min(dp[i + 1][j - a] + b, dp[i][j])
        if j >= h:
            ans = min(ans, dp[i + 1][j])

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

h, n = ril()
ls = rils(n)
dp = [float('inf')] * (h + 1)
dp[0] = 0
for i, (a, b) in enumerate(ls):
    for j in range(h + 1):
        dp[min(j + a, h)] = min(dp[min(j + a, h)], dp[j] + b)
print(dp[-1])

Solution (Python):


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

cost=[]
for _ in range(N):
    A,B=map(int,input().split())
    cost.append([A/B,A,B])
"""
mx=0
mi=10**12
ind=-1
for index,item in enumerate(cost):
    mx=max(mx,item[0])
    if mx==item[0] and mi>item[1]:
        ind=index

ans=0
temp=cost[ind][1]
ans+=H//(temp)*cost[ind][2]

H-=temp*(H//temp-1)
"""

dp=[10**12]*H
dp[0]=0
sans=10**12
for item in cost:
    for i in range(len(dp)):
        if dp[i]!=10**12:
            if i+item[1]>=H:
                sans=min(sans,dp[i]+item[2])
            else:
                dp[i+item[1]]=min(item[2]+dp[i],dp[i+item[1]])
    
print(sans)

Solution (Python):
H,N=map(int,input().split())
ABC=[]
for _ in range(N):
  a,b=map(int,input().split())
  ABC.append((a,b,a/b))
ABC=sorted(ABC, key=lambda x:x[2], reverse=True)
A=[ABC[i][0] for i in range(N)]
B=[ABC[i][1] for i in range(N)]
dp=[[float('inf')]*(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]>=0:
      dp[i+1][j]=min(dp[i][j],dp[i+1][j-A[i]]+B[i])
    else:
      dp[i+1][j]=min(dp[i][j],dp[i+1][0]+B[i])
print(dp[N][H])

Solution (Python):
H, N = map(int, input().split())
ms = []
for _ in range(N):
    h, c = map(int, input().split())
    ms.append((h, c))
inf = 10**18
dp = [inf] * (H+1)
dp[0] = 0
for i in range(1, H+1):
    for h, c in ms:
        dp[i] = min(dp[i], dp[max(i-h, 0)] + c)
print(dp[-1])

Solution (Python):
def resolve():
    h, n = map(int, input().split())
    w = []
    v = []
    for i in range(n):
        a, b = map(int, input().split())
        w.append(a)
        v.append(b)
    dp = [[0 for i in range(h + 1)] for j in range(n + 1)]
    for i in range(1, h + 1):
        dp[0][i] = float("inf")
    for i in range(n):
        for j in range(h + 1):
            if j >= w[i]:
                dp[i + 1][j] = min(dp[i + 1][j - w[i]] + v[i], dp[i][j])
            else:
                dp[i + 1][j] = min(dp[i][j], v[i])
    
    print(dp[n][h])


if __name__ == '__main__':
    resolve()

Solution (Python):

h,n = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(n)]
a = [i[0] for i in ab]
b = [i[1] for i in ab]
 
from math import ceil
dp = [[0] * (h + 1) for _ in range(n)]
 
for j in range(h+1):
    dp[0][j] = ceil(j/a[0]) * b[0]
 
for i in range(1,n):
    for j in range(h+1):
        if j - a[i] > 0:
            dp[i][j] = min(dp[i][j-a[i]] + b[i],dp[i-1][j])
        else:
            dp[i][j] = min(b[i],dp[i-1][j])
 
print(dp[n-1][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)
    
A, B = zip(*sorted(zip(A, B)))

dp = [[0] * (10000 + 1)] * (N + 1)

for dmg in range(1, 10000 + 1):
    dp[1][dmg] = -(-dmg // A[0]) * B[0]

for index in range(1, N + 1):
    a, b = A[index-1], B[index-1]
    for dmg in range(1, 10000 + 1):
        ahead = dp[index-1][dmg] 
        if dmg - a < 0:
            only = -(-dmg // a) * b
        else:
            only = dp[index][dmg - a] + b 
        dp[index][dmg] = min(ahead, only)
print(min(dp[N][H:]))

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

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



dp = {}

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

dp[H] = 0
for hp in range(H,-1,-1):
    
    for i in range(N):
        
        hp_next = hp - A[i]
        if hp_next < 0: hp_next = 0
        
        mp_next = dp[hp] + B[i]
        

        dp[hp_next] = min(dp[hp_next],mp_next)

print(dp[0])

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




Cost=[[int(x) for x in input().split()] for _ in range(N)]





DP=[float('inf')]*(H+2)
DP[0]=0
DP[1]=0
for i in range(2,H+2):
    for j in range(N):
      if i-Cost[j][0]>=0:
          DP[i]=min(DP[i], DP[i-Cost[j][0]]+Cost[j][1])
      else:
          DP[i]=min(DP[i], Cost[j][1])

print(DP[H+1])

Solution (Python):

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

AB = sorted([list(map(int,input().split())) for i in range(N)])
dp = [float('inf') for i in range(H)]
dp[0] = 0
for i in range(N):
    for j in range(H):
        if j-AB[i][0] >= 0:
            dp[j] = min(dp[j],dp[j-AB[i][0]]+AB[i][1])
ans = float('inf')
for i in range(N):
    for j in range(max(0,H-AB[i][0]),H):
        if dp[j] != float('inf') and j+AB[i][0] >= H:
            ans = min(ans,dp[j]+AB[i][1])
print(ans)

Solution (Python):




inf = 10**10


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

nap = [[inf for _ in range(H+max(A)+1)] for _ in range(N)]

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






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
















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

Solution (Python):
def main():
    h,n=map(int,input().split())
    ab=sorted([tuple(map(int,input().split())) for _ in range(n)],key=lambda x:x[1])
    mn=[0]
    for i,j in ab:
        if i > len(mn)-1:
            mn.extend([j]*(i-len(mn)+1))
    for i in range(1,len(mn)):
        for j in range(1,i):
            x=mn[j]
            cost=(i//j)*x+mn[i-(i//j)*j]
            if cost<mn[i]:
                mn[i]=cost
    if h<=len(mn)-1:
      print(mn[h])
    else:
      c=len(mn)
      while True:
        s=10**9
        for i in range(1,len(mn)):
          s=min(s,mn[i]+mn[-i])
        mn.append(s)
        if c==h:
          print(mn[-1])
          break
        else:
          c+=1
    
if __name__ == '__main__':
    main()

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)

mA = max(A)

INF = 1001001001
DP = [INF for _ in range(H+1)]
DP[0] = 0
for h in range(H):
    for j in range(N):
        nh = min(h+A[j], H)
        DP[nh] = min(DP[nh], DP[h] + B[j])

print(DP[H])

Solution (Python):
H, N = list(map(int, input().split()))
w = []
v = []
for _ in range(N):
    v1, w1 = list(map(int, input().split()))
    w.append(w1)
    v.append(v1)
    
INF = 1e15
dp = [[INF]*(H+1) for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for j in range(H+1):
    c = j//v[0]*w[0]
    if j%v[0]!=0:
        c += w[0]
    dp[1][j] = c

for i in range(1,N):
    for j in range(H+1):
        if j<v[i]:
            tmp = j//v[i]*w[i]
            if j%v[i]!=0:
                tmp += w[i]
            dp[i+1][j] = min(dp[i][j], tmp)
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j-v[i]]+w[i])
print(dp[-1][-1])

Solution (Python):
import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


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

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

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

dprint(dp[:10])
print(min(dp[h:]))

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

ablist=[]
for i in range(N):
  a,b=map(int,input().split())
  ablist.append((a,b))
  
dp=[]
for i in range(N+1):
  dp.append([10**9]*(H+1))
  
for i in range(N+1):
  dp[i][0]=0
  

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

print(dp[N][H])

Solution (Python):
h, n = map(int, input().split())
m = [list(map(int, input().split())) for _ in range(n)]
INF = 10 ** 12
dp = [INF for _ in range(h + 1)]
dp[0] = 0
for a, b in m:
    for i in range(h + 1):
        if i + a > h:
            dp[h] = min(dp[h], dp[i] + b)
        else:
            dp[i + a] = min(dp[i + a], dp[i] + b)
print(dp[h])

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

import numpy as np

H,N = map(int,readline().split())
m = map(int,read().split())
AB = zip(m,m)

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

for a,b in AB:
    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 = dp.ravel()[:U]

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

Solution (Python):
import sys, math
from time import sleep
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from fractions import gcd
e = enumerate

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

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


for i, ab in e(AB):
    a, b = ab[0], ab[1]
    for j in range(H+1):

        if j==0:
            newval = b
        elif j%a==0:
            newval = j//a * b
        else:
            newval = (j//a+1) * b

        if j>=a:
            newval2 = dp[i][j-a] + b
        else:
            newval2 = float('inf')

        if j>=a:
            newval3 = dp[i+1][j-a] + b
        else:
            newval3 = float('inf')

        dp[i+1][j] = min(dp[i][j], newval, newval2, newval3)


print(dp[N][H])

Solution (Python):
h,n=map(int,input().split())
a=[0]*n
b=[0]*n
inf=10**10
for i in range(n):
    a[i],b[i]=map(int,input().split())
dp = [[inf for i in range(h+1)] for j in range(n+1)]
for i in range(n):
    cnt=1
    for j in range(h+1):
            if j-a[i]<0:
                dp[i+1][j]=min(dp[i][j],dp[i][0]+b[i],b[i]*cnt)
            else:
                dp[i+1][j]=min(dp[i][j],dp[i+1][j-a[i]]+b[i],dp[i][j-a[i]]+b[i],b[i]*cnt)
                cnt+=1
"""
for i in range(1,n+1):
    for j in range(h+1):
        if j+a[i]<=h:
            dp[i+1][j+a[i]]=min(dp[i+1][a[i]],dp[i][j]+b[i])"""
print(dp[n][h])

Solution (Python):

import sys


def solve(H: int, N: int, A: "List[int]", B: "List[int]"):
    

    

    dp = [[10**9]*(H+10**4+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+10**4+1):
            if h - A[i] >= 0:
                dp[i+1][h] = min(dp[i+1][h-A[i]]+B[i],dp[i][h])
            else:
                dp[i+1][h] = dp[i][h]
 

    answer = 10**9
    for i in range(H,H+10**4+1):
        answer = min(answer,dp[-1][i])
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    N = int(next(tokens))  
    A = [int()] * (N)  
    B = [int()] * (N)  
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(H, N, A, B)

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

INF = 1<<60
dp = [[INF]*(N+1) for _ in range(H+1)]
for i in range(N+1):
    dp[0][i] = 0

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

Solution (Python):
def calc_m_value(M, A, B, N, h):
    return min([M[h - A[i]]+B[i] if A[i] <= h else B[i] for i in range(N)])

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

M = [0] * (H+1)
for i in range(1, H+1):
    M[i] = calc_m_value(M, A, B, N, i)

print(M[H])