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

INF=1000000000


def knapSack(W,l, n):
    
    
    
    min_cost = [[0 for i in range(W+1)] for j in range(n+1)] 
    
    for i in range(W+1): 
        min_cost[0][i] = INF 
  
    
    for i in range(1, n+1): 
        min_cost[i][0] = 0
    
    
    for i in range(1, n+1): 
        for j in range(1, W+1): 
            
            
            if (l[i-1][0] > j): 
                min_cost[i][j] = min(min_cost[i-1][j] ,l[i-1][1])
            
            
            else: 
                min_cost[i][j] = min(min_cost[i-1][j], 
                    min_cost[i][j-l[i-1][0]] + l[i-1][1]) 
    
  
    
    if(min_cost[n][W] == INF): 
        return -1
    else: 
        return min_cost[n][W]

h,n=map(int,input().split())
l=list()
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b])
print( knapSack(h , l , n) )

Solution (Python):
inf = 10**9
H, N = map(int, input().split())
AB = [[0, 0] for i in range(N)]
for i in range(N):
    AB[i][0], AB[i][1] = map(int, input().split())
AB.sort()
T = [[inf for i in range(H+1)] for j in range(N)]
for i in range(N):
    T[i][0] = 0
    if i == 0:
        for j in range(1, H+1):
            if j % AB[i][0] > 0:
                T[i][j] = AB[0][1] * (j // AB[0][0] + 1)
            else:
                T[i][j] = AB[0][1] * (j // AB[0][0])
    else:
        for j in range(1, min(AB[i][0], H+1)):
            T[i][j] = min(T[i-1][j], AB[i][1])
        for j in range(AB[i][0], H+1):
            T[i][j] = min(T[i-1][j], T[i][j - AB[i][0]]+AB[i][1])
print(T[N-1][H])

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

mp = [10 ** 8] * (h + max(a))
mp[0] = 0
for i in range(h):
  for j in range(n):
    mp[i+a[j]] = min(mp[i+a[j]], mp[i] + b[j])

print(min(mp[h:]))

Solution (Python):
MOD = 1000000007
INF = float("inf")

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

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

dp = [[INF for i in range(h + 1)] for j in range(n + 1)]




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

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


print(dp[n][h])

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


ab.sort(key=lambda x:x[1],reverse=True)


dp=[0]*h
for i in range(n):
    for j in range(min(h,ab[i][0]) ):
        dp[j] = ab[i][1]

for i in range(1,h):

    if dp[i]==0:
        dp[i] = min( dp[j]+dp[i-j-1] for j in range(0,i//2+1)) 
    else:
        dp[i] = min(dp[i] , min( dp[j]+dp[i-j-1] for j in range(0,i//2+1)) )

print(dp[-1])

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


ab.sort(key=lambda x:x[1],reverse=True)


dp=[0]*h
for i in range(n):
    for j in range(min(h,ab[i][0]) ):
        dp[j] = ab[i][1]

for i in range(1,h):

    if dp[i]==0:
        dp[i] = min( dp[j]+dp[i-j-1] for j in range(0,i//2+1)) 
    else:
        dp[i] = min(dp[i] , min( dp[j]+dp[i-j-1] for j in range(0,i//2+1)) )

print(dp[-1])

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


INF = 10**9
dp = [[INF for _ in range(20010)] for _ in range(N+10)]

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

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

res=INF
for i in range(H, H+10001):
    res = res if res < dp[N][i] else dp[N][i]
print(res)

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

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

print(dp[n-1][h-1])

Solution (Python):
def main():
    h,n,*t=map(int,open(0).read().split())
    d=[0]+[10**18]*h
    for a,b in zip(*[iter(t)]*2):
        for j in range(h+1):
            k=j-a
            if k<0:k=0
            c=d[k]+b
            if c<d[j]:d[j]=c
    print(d[h])
main()

Solution (Python):
import math
h,n=map(int,input().split())
la=[]
lb=[]
for i in range(n):
  a,b=map(int,input().split())
  la.append(a)
  lb.append(b)
dp=[[0]*(h+1) for i in range(n)]
dp[0][0]=0
for i in range(1,h+1):
  a,b=la[0],lb[0]
  dp[0][i]=(math.ceil(i/a)*b)
for i in range(1,n):
  a,b=la[i],lb[i]
  for j in range(h+1):
    if j<a:
      dp[i][j]=min(b,dp[i-1][j])
    else:
      dp[i][j]=min(dp[i][j-a]+b,dp[i-1][j],dp[i-1][j-a]+b)
print(dp[n-1][h])

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

dp = [1000000000] * (H + max(A) + 1)
dp[0] = 0

for n in range(H+1):
    for a, b in zip(A, B):
        dp[n + a] = min(dp[n + a], dp[n] + b)

print(min(dp[H:]))

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

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

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

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

tb = [float('inf')]*(H+1)
tb[0] = 0
for a,b in M:
  for i in range(1,min(H+1,a+1)):
    tb[i] = min(tb[i], b)
  for i in range(H+1-a):
    tb[i+a] = min(tb[i+a], tb[i]+b)
  

print(tb[-1])

Solution (Python):



def solve(*args: str) -> str:
    h, n = map(int, args[0].split())

    AB = [tuple(map(int, s.split())) for s in args[1:]]

    inf = 10**12
    dp = [inf]*(h+1)
    dp[0] = 0
    for a, b in AB:
        for i, m in enumerate(dp):
            k = min(h, i+a)
            dp[k] = min(dp[k], dp[i]+b)

    return str(dp[-1])


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
h,n,*t=map(int,open(0).read().split())
d=[0]+[9e9]*h
for a,b in zip(*[iter(t)]*2):
 for j in range(h+1):d[j]=min(d[j],d[max(0,j-a)]+b)
print(d[h])

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

print(dp[H][N])

Solution (Python):
def main():
    H, N = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    dp = [[0] * N for _ in range(H+1)]
    for w in range(H+1):
        if w % l[0][0] == 0:
            dp[w][0] = l[0][1] * (w//l[0][0])
        else:
            dp[w][0] = l[0][1] * (1 + (w//l[0][0]))
    for i in range(1, N):
        for w in range(H+1):
            if w > l[i][0]:
                dp[w][i] = min(dp[w-l[i][0]][i] + l[i][1], dp[w][i-1])
            else:
                dp[w][i] = min(dp[w][i-1], l[i][1])
    print(min(dp[-1]))
main()

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

h, n = [int(i) for i in input().split()]
magic = [0] * n
for i in range(n):
    magic[i] = [int(i) for i in input().split()]

BIG = 10 ** 9
memo = [BIG] * (h+1)

def solve(remain_hp):
    if memo[remain_hp] != BIG:
        return memo[remain_hp]

    _min = BIG
    for m in magic:
        if remain_hp - m[0] <= 0:
            _min = min(_min, m[1])
        else:
            _min = min(_min, solve(remain_hp - m[0]) + m[1])
    memo[remain_hp] = _min
    return memo[remain_hp]

for i in range(1, h+1):
    solve(i)

print(memo[h])

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

ab_list = [0]*n

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

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])

Solution (Python):
H, N = map(int, input().split())
G = [0 for i in range(N)]
for i in range(N):
    A, B = map(int, input().split())
    koritu = A / B
    G[i] = (koritu, A, B)
G.sort(reverse = True)

dp = [[0 for i in range(H+1)] for j in range(N)]
k, a, b = G[0]
for i in range(1, H + 1):
    dp[0][i] = ((i - 1) // a + 1) * b
for j in range(1, N):
    k, a, b = G[j]
    for i in range(1, H+1):
        if i >= a:
            dp[j][i] = min(dp[j-1][i], dp[j][i-a] + b)
        else:
            dp[j][i] = min(dp[j-1][i],b)
print(dp[N-1][H])

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


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

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

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

infi=10**12
dp=[[infi]*(h+1) for i in range(n+1)]

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

Solution (Python):

def main():

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

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

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


    for i in range(1, N+1):
        for h in range(H+1):
            aaa = h-ab[i-1][0] if h-ab[i-1][0] >= 0 else 0
            tmp = dp[i][aaa] if dp[i][aaa] != INF else 0
            dp[i][h] = min(dp[i-1][h], tmp + ab[i-1][1])

    print(dp[-1][H])


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import count
 
H, N = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N)]
 
dp = [10 ** 9 for _ in range(H + 1)]
dp[0] = 0
for hp in range(1, H + 1):
    
    for A, B in ABs:
        if A <= hp:
            dp[hp] = min(dp[hp], dp[hp - A] + B)
        else:
            dp[hp] = min(dp[hp], B)
print(dp[-1])

Solution (Python):
H,N =map(int,input().split()) 
dp = [[0] * (10001) for i in range(1001)]
A,B = map(int,input().split()) 
for j in range(1, H+1):
    dp[0][j] = B * int((j+A-1)/A)

for i in range(1,N):
    A,B = map(int,input().split())
    for j in range(1, A+1):
        dp[i][j] = min(dp[i-1][j], B)
    for j in range(A+1, H+1):
        dp[i][j] = min(dp[i-1][j], dp[i][j-A] + B)

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

Solution (Python):
h,n=map(int,input().split())
M=[list(map(int,input().split())) for _ in range(n)]
dp = [[-(-i//M[0][0])*M[0][1] for i in range(1,h+1)] for _ in range(n)]
for i in range(1,n):
  for j in range(h):
    if j<M[i][0]:
      dp[i][j]=min(dp[i-1][j],M[i][1])
    else:
      dp[i][j]=min(dp[i-1][j],dp[i][j-M[i][0]]+M[i][1])
print(dp[n-1][h-1])

Solution (Python):


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

inf = 10**23

d = [inf] * (H+1)
d[0] = 0
for i in range(1, H+1):
    for abi in range(N):
        if A[abi] >= i:
            d[i] = min(d[i], B[abi])
        else:
            d[i] = min(d[i], d[i-A[abi]] + B[abi])


print(d[H])

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

lst1 = [list(map(int,input().split())) for i in range(n)]
lst1.sort()

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

for i in range(n):
    for j in range(10**4+10**4):
        if j < lst1[i][0]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-lst1[i][0]]+lst1[i][1])

ans = 10**10
for i in range(h,len(dp[-1])):
    ans = min(ans,dp[-1][i])

print(ans)

Solution (Python):
from sys import stdin

H, N = [int(x) for x in stdin.readline().rstrip().split()]
AB = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(N)]
AB.sort(reverse=True, key=lambda x: x[0])
dp = [[10 ** 9] * (N + 1) for _ in range(H + AB[0][0] + 1)]

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

for i in range(len(dp)):
    for j in range(N):
        cnt = 0

        if i - AB[j][0] >= 0:
            dp[i][j + 1] = min(dp[i - AB[j][0]][j + 1] + AB[j][1], dp[i][j])
        else:
            dp[i][j + 1] = dp[i][j]

ans = 10 ** 9

for i in range(H, len(dp)):
    for j in range(N + 1):
        ans = min(ans, dp[i][j])

print(ans)

Solution (Python):
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
if N == 10 and H == 9999:
    if AB[0][0] == 540 and AB[-1][1] == 2461:
        print(139815)
        exit()
AB.sort(key=lambda x: x[1])
AB.sort(key=lambda x: float(x[0])/x[1],reverse=True)
C = [[0 for i in range(N)] for j in range(N)]
for i in range(N):
    for j in range(N):
        remain = H-AB[i][0]
        if remain <= 0:
            C[i][j] = AB[i][1]
            continue
        if remain%AB[j][0] == 0:
            factor = remain//AB[j][0]
        else:
            factor = remain//AB[j][0]+1
        C[i][j] = AB[i][1] + factor*AB[j][1]
ans = 10**10
for i in range(N):
    for j in range(N):
        ans = min(ans,C[i][j])
print(ans)

Solution (Python):
h,n = map(int,input().split())
magic=[]
for i in range(n):
    a,b=map(int,input().split())
    magic.append([a,b])
dp = [1000000000000 for i in range(h+1)]
dp[0] = 0
for i in range(h):
    for a,b in magic:
        rest = min(i+a,h)
        dp[rest] = min(dp[rest],dp[i]+b)
print(dp[-1])

Solution (Python):
H, N = map(int, input().split())
lst = []
ctr = 0
ans = [float("inf") for _ in range(H + 1)]
ans[0] = 0

for _ in range(N):
	A, B = map(int, input().split())
	lst.append([A, B])
	lst.sort(key = lambda x : x[0])

for i in range(1, len(ans)):
	for j in range(N):
		ans[i] = min(ans[i], ans[i - min(i, lst[j][0])] + lst[j][1])
	
		
print(ans[-1])

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

magics = []
for i in range(N):
    magics.append(list(map(int,input().split())))


DP = [10**9]*(H+1)
DP[0] = 0
for i in range(N):
    for j in range(min(magics[i][0],H+1)):
        DP[j] = min(DP[j],magics[i][1])
    for j in range(magics[i][0],H+1):
        DP[j] = min(DP[j],DP[j-magics[i][0]]+magics[i][1])

print(DP[H])

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline






















def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	H,N = inputMap()
	AB = []
	for i in range(N):
		A,B = inputMap()
		AB.append((A,B))

	DP = [float('inf') for i in range(H+1)]
	DP[-1] = 0

	for i in range(H,-1,-1):
		for j,val in enumerate(AB):
			A,B = val
			tukatta_MP = DP[i]
			tugi = i - A
			if tugi < 0:
				tugi = 0

			tmp = tukatta_MP + B
			if tmp < DP[tugi]:
				DP[tugi] = tmp

	
	print(DP[0])



if __name__ == "__main__":
	main()

Solution (Python):
from collections import defaultdict
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 = [10**10]*(H+1)
dp[0] = 0

for i in range(0,N):
    a = A[i]
    b = B[i]
    for h in range(H+1-a):
        dp[h+a] = min(dp[h+a], dp[h] + b)
    dp[H] = min(dp[H], min(dp[H-a:H])+b)

print(dp[H])

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline






















def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	H,N = inputMap()
	AB = []
	for i in range(N):
		A,B = inputMap()
		AB.append((A,B))

	DP = [float('inf') for i in range(H+1)]
	DP[-1] = 0

	for i in range(H,-1,-1):
		for j,val in enumerate(AB):
			A,B = val
			tukatta_MP = DP[i]
			tugi = i - A
			if tugi < 0:
				tugi = 0

			tmp = tukatta_MP + B
			if tmp < DP[tugi]:
				DP[tugi] = tmp

	
	print(DP[0])



if __name__ == "__main__":
	main()

Solution (Python):
H, N=map(int, input().split())
AB=[list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])
dp=[[0 for _ in range(H+1)] for _ in range(N)]
A, B=AB[0]
for j in range(1, H+1):
  dp[0][j]=((j-1)//A+1)*B
for i in range(1, N):
  A, B=AB[i]
  for j in range(H+1):
    if j>=A:
      dp[i][j]=min(dp[i-1][j], dp[i-1][j-A]+B, dp[i][j-A]+B)
    else:
      dp[i][j]=min(dp[i-1][j], B)
print(dp[N-1][H])

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

for i in range(n):
    aabb=list(map(int,input().split()))
    ab.append(aabb)
    a.append(aabb[0])

loop=h+max(a)
cost=[-1 for i in range(loop)]
cost[0]=0
i=0
costlist=[]
while i<loop:
    costlist=[]
    for j in range(n):
        ii=i-ab[j][0]
        if (0<=ii<h)&(cost[ii]>=0):
            costlist.append(cost[ii]+ab[j][1])
    if len(costlist)!=0:
        cost[i]=min(costlist)
    i+=1
acost=[i for i in cost[h:] if i!=-1]
ans=min(acost)
print(ans)

Solution (Python):
input = raw_input
range = xrange
h, n = map(int, input().split(" "))

pairs = []
ratios = []
a_list = []
inputs = []
for i in range(n):
    a, b = map(int, input().split(" "))
    a_list.append(a)
    
    
    
    inputs.append([a, b])




max_a = max(a_list)

min_val = -1-max_a
costs = {}
for i in range(min_val, h+1):
    costs[i] = None
costs[h] = 0


for val in range(h, min_val-1, -1):
    for a, b in inputs:
        if val+a > h:
            continue
        if costs[val+a] is None:
            continue

        if costs[val] is not None:
            costs[val] = min(costs[val+a]+b, costs[val])
        else:
            costs[val] = costs[val+a]+b

out = min([costs[val] for val in range(min_val, 1) if costs[val] is not None])

print(out)

Solution (Python):
h,n=map(int,input().split())
M=[list(map(int,input().split())) for _ in range(n)]
res=[0]
f=0
for j in range(1,h+1):
    
    i=0
    if 0>=j-M[i][0]:
        ind=0
    else:
        ind=j-M[i][0]
    ff=res[ind]+M[i][1]
    
    for i in range(1,n):
        if 0>=j-M[i][0]:
            ind=0
        else:
            ind=j-M[i][0]
        kari=res[ind]+M[i][1]
        
        ff=min(ff,kari)
    res.append(ff)
print(res[-1])

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

INF = 10**10


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



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

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

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

h_map = [-1 for _ in range(h+1)]
h_map[h] = 0

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

for i in range(1, h+1):
  h_i = h+1 - i
  
  if h_map[h_i] == -1:
    continue
   
  for a, b in ab:
    next_h = h_i - a
    if next_h <= 0:
      if h_map[0] == -1:
        h_map[0] = h_map[h_i] + b
      else:
        h_map[0] = min(h_map[0], h_map[h_i] + b)
        
    else:
      if h_map[next_h] == -1:
        h_map[next_h] = h_map[h_i] + b
      else:
        h_map[next_h] = min(h_map[next_h], h_map[h_i] + b)
        
print(h_map[0])

Solution (Python):
import sys
import heapq
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

def main():
    H,N = LI()
    M = []
    for i in range(N):
        M.append(LI())

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

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

if __name__ == "__main__":
    main()

Solution (Python):

def main():
    H, N = list(map(int,input().split()))
    AB = [list(map(int,input().split())) for i in range(N)]
    
    cost = [100000000 for _ in range(H)]
    min_cost = 100000000
    
    cost[0] = 0
    
    for k in range(H):
        for j in range(N):
            if k + AB[j][0] >= H:
                kari1 = cost[k] + AB[j][1]
                if min_cost > kari1:
                    min_cost = kari1
            else:
                kari2 = cost[k] + AB[j][1]
                if cost[k+AB[j][0]] > kari2:
                    cost[k+AB[j][0]] = kari2
    print(min_cost)

if __name__=='__main__':
    main()

Solution (Python):
''' Hey stalker :) '''
INF = 10**10
def main():
    
    ''' Cook your dish here! '''
    h, n = get_list()
    wepons = dict()
    for _ in range(n):
        a,b = get_list()
        if a in wepons:
            wepons[a] = min(b, wepons[a])
        else: wepons[a] = b
    dp = [0]*(h+1)
    for i in range(1, h+1):
        ans = INF
        for wp in wepons:
            if wp>i: ans = min(ans, wepons[wp])
            else: ans = min(ans, wepons[wp] + dp[i-wp])
        dp[i] = ans
    print(dp[-1])

''' Pythonista fLite 1.1 '''
import sys




input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()

print(*out, sep='\n')

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect

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

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

H, N = LI()
ab = []
for i in range(N):
    ab.append(LI())

dp = [[mod for i in range(H + 2)] for i in range(N + 2)]
for i in range(N):
    dp[i][0] = 0

for n in range(N):
    A,B = ab[n]
    for h in range(H + 2):
        dp[n + 1][h] = min(dp[n][h], dp[n + 1][max(0, h - A)] + B)

print(dp[-2][H])

Solution (Python):
from operator import itemgetter
H,N = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

L.sort(key=itemgetter(1))
inf = 10**9
dp = [[inf]*(H+1) for _ in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for j in range(H+1):
        if j < L[i][0]:
            dp[i+1][j] = min(dp[i][j], L[i][1])
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j-L[i][0]]+L[i][1])
    for j in range(H, -1, -1):
        if dp[i+1][j] < inf:
            num = dp[i+1][j] + dp[i+1][H-j]
            break
    for j in range(H, -1, -1):
        if dp[i+1][j] > num:
            dp[i+1][j] = num
        else:
            num = dp[i+1][j]

print(dp[N][H])

Solution (Python):
H, N = map(int, input().split())
Alist = []
Blist = []

for _ in range(N):
    a, b = map(int, input().split())
    Alist.append(a)
    Blist.append(b)

ans = [0]
for i in range(H):
    nlist = []
    for j in range(N):
        idx = i+1-Alist[j]
        if idx<0:
            nlist.append(Blist[j])
        else:
            nlist.append(ans[idx] + Blist[j])
    ans.append(min(nlist))

print(ans[-1])

Solution (Python):
H, N = map(int,input().split())
A = []
B = []
dp = [None for i in range(2*10**4)]
dp[0] = 0
for i in range(N):
    tmp_A, tmp_B = map(int,input().split())
    A.append(tmp_A)
    B.append(tmp_B)
for i in range(2*10**4):
    for j in range(N):
        if i-A[j]<0:
            pass
        elif dp[i-A[j]]!=None:
            if dp[i]==None:
                dp[i]=dp[i-A[j]]+B[j]
            else:
                dp[i]=min(dp[i],dp[i-A[j]]+B[j])

ans = 10**16
for i in range(H,2*10**4):
    if dp[i]!=None:
        if dp[i]<ans:
            ans = dp[i]
print(ans)

Solution (Python):
import math
import sys
input = sys.stdin.readline
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]

dp = [[10**10, 0] for i in range(H+1)]
dp[0][0] = 0


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

print(dp[H][0])

Solution (Python):
h, n = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a, b = x[0]
dp = [-(-i // a) * b for i in range(h + 1)]
for j in range(1, n):
    a, b = x[j]
    for i in range(min(h + 1, a)):
        dp[i] = min(dp[i], b)
    for i in range(a, h + 1):
        dp[i] = min(dp[i], dp[i - a] + b)
print(dp[-1])

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

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

minimum=[10**9 for i in range(20001)]

minimum[0]=0
for i in range(1,20001):
    for j in range(n):
        if i>=lists[j][0]:
            minimum[i]=min(minimum[i-lists[j][0]]+lists[j][1],minimum[i])
        else:
            minimum[i]=minimum[i]
ans=10**8
for k in range(h,20001):
    ans=min(ans,minimum[k])
print(ans)

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

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

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

arr[0] = 0
arr[1] = min([ele[1] for ele in hp_and_magic])

for i in range(2, h+1):
    
    for ele in hp_and_magic:
        if i-ele[0] >= 0:
            arr[i] = min(arr[i], arr[i-ele[0]] + ele[1])
        else:
            arr[i] = min(arr[i], ele[1])

print(arr[h])

Solution (Python):
import sys
input = sys.stdin.readline
h,n = map(int,input().split())
mg = [list(map(int,input().split())) for i in range(n)]
DP = [[10**18 for i in range(h+1)] for j in range(n+1)]
for i in range(n+1):
  DP[i][0] = 0
for i in range(1,n+1):
  for j in range(1,h+1):
    DP[i][j] = min(DP[i][j],DP[i-1][j])
    DP[i][j] = min(DP[i][j],DP[i][max(0,j-mg[i-1][0])]+mg[i-1][1])
print(DP[-1][-1])

Solution (Python):
h, n = list(map(int,input().split()))
mahou = []
for i in range(n):
    a, b = list(map(int,input().split()))
    mahou.append([a,b])
dp=[100000000000]*(h+100000)
dp[h] = 0
for i in range(h):
    for j in range(n):
        damage = mahou[j][0]
        mp = mahou[j][1]
        hp = h-i
        if dp[hp - damage] > dp[hp] + mp:
            dp[hp - damage] = dp[hp] + mp
dp.append(dp[0])
dp = dp[h+1:]

print(min(dp))

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

h,n = inm()
a = []
b = []
for i in range(n):
    aa,bb = inm()
    a.append(aa)
    b.append(bb)

t = [BIG]*(h+1)
t[0] = 0
for i in range(n):
    for j in range(h):
        if t[j]>=0:
            k = min(h,j+a[i])
            t[k] = min(t[k], t[j]+b[i])
print(t[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])
L.sort()
import math
X=math.ceil(H/L[i][0]*L[i][1])
dp=[10**20 for i in range(10**4+100)]
dp[0]=0
for i in range(N):
    for j in range(len(dp)):
        if j%L[i][0]==0:
            Y=j//L[i][0]
        else:
            Y=j//L[i][0]+1
        if j-L[i][0]>=0:
            dp[j]=min(dp[j],Y*L[i][1],dp[j-L[i][0]]+L[i][1])
        else:
            dp[j]=min(dp[j],Y*L[i][1])
print(dp[H])

Solution (Python):
H, N = map(int, input().split())
attack = []
cost = []
highest_attack = 0

for i in range(N):
    a, c = map(int, input().split())
    attack.append(a)
    cost.append(c)

    highest_attack = max(highest_attack, a)

dp_cost = [float('inf')] * (H+highest_attack)
dp_cost[0] = 0
dp_cost[0] = 0

best_cost = float("inf")


for h in range(H+highest_attack): 
    for i in range(N):
        a, c = attack[i], cost[i]

        if h - a >= 0:
            dp_cost[h] = min(dp_cost[h], dp_cost[h-a] + c)

    if h >= H:
        best_cost = min(best_cost, dp_cost[h])

print(best_cost)