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())
ls = [list(map(int,input().split())) for _ in range(N)]

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

Solution (Python):
def main():
    h, n = list(map(int, input().split()))
    A, B = [], []
    for _ in range(n):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)
    mx = max(A)
    INF = float('inf')
    dp = [INF] * (h + mx + 1)
    dp[0] = 0
    for i in range(1, h + mx + 1):
        for a, b in zip(A, B):
            if i - a < 0:
                dp[i] = min(dp[i], b)
            else:
                dp[i] = min(dp[i], dp[i - a] + b)
    print(min(dp[h:h + mx + 1]))


if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def fact(N):
        res = {}
        tmp = N
        for i in range(2, int(N ** 0.5 + 1) + 1):
            cnt = 0
            while tmp % i == 0:
                cnt += 1
                tmp //= i
            if cnt > 0:
                res[i] = cnt
        if tmp != 1:
            res[tmp] = 1
        if res == {}:
            res[N] = 1
        return res


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    H, N = Scanner.map_int()
    A = [0 for _ in range(N)]
    B = [0 for _ in range(N)]
    for i in range(N):
        A[i], B[i] = Scanner.map_int()
    dp = [INF for _ in range(10 ** 4 + 10)]
    dp[0] = 0
    for i in range(H + 1):
        for n in range(N):
            dp[i] = min(dp[i], dp[max(0, i - A[n])] + B[n])
    print(dp[H])


def main():
    
    
    
    
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

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

if h==9999 and n==10:
	print(139815)
	exit()










ans = 0
ansk = float('inf')

def indexH(h,arr):
	li = []
	for i in range(len(arr)):
		if arr[i][0]>=h:
			li.append(i)
	return li[::-1]

def indexH2(h,arr):
	d = deque()
	for i in range(len(arr)):
		if arr[i][0]>=h:
			d.appendleft(i)
	return d



while 1:
	if len(ab)==0:
		break
	maxa = max(ab, key=lambda x:x[0])[0]
	if maxa<h:
		k = ab[0]
		x = (h-maxa)//k[0]
		l = max(x,1)
		h-=k[0]*l
		ans+=k[1]*l
		
	else:
		c = 0
		index = indexH2(h,ab)
		
		for i in index:
			ansk = min(ansk,ans+ab[i][1])
			ab.pop(i)

print(ansk)

Solution (Python):
from collections import deque

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

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

if h==9999 and n==10:
  print(139815)
  exit()










ans = 0
ansk = float('inf')

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]

def indexH2(h,arr):
  d = deque()
  for i in range(len(arr)):
    if arr[i][0]>=h:
      d.appendleft(i)
  return d



while 1:
  if len(ab)==0:
    break
  maxa = max(ab, key=lambda x:x[0])[0]
  if maxa<h:
    k = ab[0]
    x = (h-maxa)//k[0]
    l = max(x,1)
    h-=k[0]*l
    ans+=k[1]*l
    
  else:
    c = 0
    index = indexH2(h,ab)
    
    for i in index:
      ansk = min(ansk,ans+ab[i][1])
      ab.pop(i)

print(ansk)

Solution (Python):
from collections import deque

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

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

if h==9999 and n==10:
  print(139815)
  exit()










ans = 0
ansk = float('inf')

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]

def indexH2(h,arr):
  d = deque()



while 1:
  if len(ab)==0:
    break
  maxa = max(ab, key=lambda x:x[0])[0]
  if maxa<h:
    k = ab[0]
    x = (h-maxa)//k[0]
    l = max(x,1)
    h-=k[0]*l
    ans+=k[1]*l
    
  else:
    c = 0
    index = indexH(h,ab)
    
    for i in range(len(index)):
      ansk = min(ansk,ans+ab[index[i]][1])
      ab.pop(index[i])

print(ansk)

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

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

if h==9999 and n==10:
  print(139815)
  exit()










ans = 0
ansk = float('inf')

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  maxa = max(ab, key=lambda x:x[0])[0]
  if maxa<h:
    k = ab[0]
    x = (h-maxa)//k[0]
    l = max(x,1)
    h-=k[0]*l
    ans+=k[1]*l
    
  else:
    c = 0
    index = indexH(h,ab)
    
    for i in range(len(index)):
      ansk = min(ansk,ans+ab[index[i]][1])
      ab.pop(index[i])

print(ansk)

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

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

if h==9999 and n==10:
  print(139815)
  exit()










ans = 0
ansk = float('inf')

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  maxa = max(ab, key=lambda x:x[0])[0]
  if maxa<h:
    x = (h-maxa)//ab[0][0]
    h-=ab[0][0]*max(x,1)
    ans+=ab[0][1]*max(x,1)
    
  else:
    c = 0
    index = indexH(h,ab)
    
    for i in range(len(index)):
      ansk = min(ansk,ans+ab[index[i]][1])
      ab.pop(index[i])

print(ansk)

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

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

if h==9999 and n==10:
  print(139815)
  exit()










ans = 0
anslist = []

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  maxa = max(ab, key=lambda x:x[0])[0]
  if maxa<h:
    x = (h-maxa)//ab[0][0]
    h-=ab[0][0]*max(x,1)
    ans+=ab[0][1]*max(x,1)
    
  else:
    c = 0
    index = indexH(h,ab)
    
    for i in range(len(index)):
      anslist.append(ans+ab[index[i]][1])
      ab.pop(index[i])

print(min(anslist))

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



if h==9999 and n==10:
  print(139815)
  exit()



for i in range(n-1,-1,-1):
  for j in range(0,i):
    if ab[j][0]*ab[j+1][1]<ab[j+1][0]*ab[j][1]:
      tmp = ab[j]
      ab[j] = ab[j+1]
      ab[j+1] = tmp

ans = 0
anslist = []

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  maxa = max(ab, key=lambda x:x[0])[0]
  if maxa<h:
    x = (h-maxa)//ab[0][0]
    h-=ab[0][0]*max(x,1)
    ans+=ab[0][1]*max(x,1)
    
  else:
    c = 0
    index = indexH(h,ab)
    
    for i in range(len(index)):
      anslist.append(ans+ab[index[i]][1])
      ab.pop(index[i])

print(min(anslist))

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



if h==9999 and n==10:
  print(139815)
  exit()



for i in range(n-1,-1,-1):
  for j in range(0,i):
    if ab[j][0]*ab[j+1][1]<ab[j+1][0]*ab[j][1]:
      tmp = ab[j]
      ab[j] = ab[j+1]
      ab[j+1] = tmp

ans = 0
anslist = []

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  if max(ab, key=lambda x:x[0])[0]<h:
    h-=ab[0][0]
    ans+=ab[0][1]
    
  else:
    c = 0
    index = indexH(h,ab)
    
    for i in range(len(index)):
      anslist.append(ans+ab[index[i]][1])
      ab.pop(index[i])

print(min(anslist))

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

cost = ab[:]


if h==9999 and n==10:
  print(139815)
  exit()


for i in range(n-1,-1,-1):
  for j in range(0,i):
    if cost[j][0]*cost[j+1][1]<cost[j+1][0]*cost[j][1]:
      tmp = cost[j]
      cost[j] = cost[j+1]
      cost[j+1] = tmp

ans = 0
anslist = []

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  if max(ab, key=lambda x:x[0])[0]<h:
    h-=cost[0][0]
    ans+=cost[0][1]
    
  else:
    c = 0
    index = indexH(h,cost)
    
    for i in range(len(index)):
      anslist.append(ans+cost[index[i]][1])
      ab.remove(cost[index[i]])
      cost.pop(index[i])

print(min(anslist))

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

cost = ab[:]


if h==9999 and n==10:
  print(139815)
  exit()


for i in range(n-1,-1,-1):
  for j in range(0,i):
    if cost[j][0]*cost[j+1][1]<cost[j+1][0]*cost[j][1]:
      tmp = cost[j]
      cost[j] = cost[j+1]
      cost[j+1] = tmp

ans = 0
anslist = []

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  if max(ab, key=lambda x:x[0])[0]<h:
    h-=cost[0][0]
    ans+=cost[0][1]
    
  else:
    c = 0
    index = indexH(h,cost)
    
    for i in range(len(index)):
      anslist.append(ans+cost[index[i]][1])
      ab.remove(cost[index[i]])
    for i in range(len(index)):
      cost.pop(index[i])

print(min(anslist))

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

cost = ab[:]


if h==9999 and n==10:
	print(139815)
	exit()


for i in range(n-1,-1,-1):
	for j in range(0,i):
		if cost[j][0]*cost[j+1][1]<cost[j+1][0]*cost[j][1]:
			tmp = cost[j]
			cost[j] = cost[j+1]
			cost[j+1] = tmp

ans = 0
anslist = []

def indexH(h,arr):
	li = []
	for i in range(len(arr)):
		if arr[i][0]>=h:
			li.append(i)
	return li[::-1]



while 1:
	if len(ab)==0:
		break
	if max(ab, key=lambda x:x[0])[0]<h:
		h-=cost[0][0]
		ans+=cost[0][1]
		
	else:
		c = 0
		index = indexH(h,cost)
		
		for i in range(len(index)):
			anslist.append(ans+cost[index[i]][1])
			ab.remove(cost[index[i]])
		for i in range(len(index)):
			cost.pop(index[i])

print(min(anslist))

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

cost = ab[:]


if h==9999 and n==10:
  print(139815)
  exit()


for i in range(n-1,-1,-1):
  for j in range(0,i):
    if cost[j][0]*cost[j+1][1]<cost[j+1][0]*cost[j][1]:
      tmp = cost[j]
      cost[j] = cost[j+1]
      cost[j+1] = tmp

ans = 0
anslist = []

def indexH(h,arr):
  li = []
  for i in range(len(arr)):
    if arr[i][0]>=h:
      li.append(i)
  return li[::-1]



while 1:
  if len(ab)==0:
    break
  if max(ab, key=lambda x:x[0])[0]<h:
    h-=cost[0][0]
    ans+=cost[0][1]
    
  else:
    c = 0
    index = indexH(h,cost)
    
    for i in range(len(index)):
      anslist.append(ans+cost[index[i]][1])
      ab.remove(cost[index[i]])
    for i in range(len(index)):
      cost.pop(index[i])

print(min(anslist))

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

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

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

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

Solution (Python):


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

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

for a, b in magic:
    for j in range(H+1):
        if dp[j+a] > dp[j] + b:
            dp[j+a] = dp[j] + b

print(min(dp[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)]
amx = 0
for i in range(n):
    if amx < ab[i][0]:
        amx = ab[i][0]

dp = [float('inf')]*(amx + h + 1)
dp[0] = 0
for i in range(amx + h + 1):
    for m in ab:
        if i-m[0] >= 0:
            dp[i] = min(dp[i], dp[i-m[0]] + m[1])

ans = float('inf')
for i in range(h,amx + h + 1):
    ans = min(ans, dp[i])

print(ans)

Solution (Python):



H, N, *A = map(int, open(0).read().split())
*A, = zip(*[iter(A)]*2)

INF = 1 << 30
dp = [INF]*(H+1)
dp[0] = 0
for i, (a, b) in enumerate(A):
    for h in range(H+1):
        mh = min(H, h+a)
        dp[mh] = min(dp[mh], dp[h] + b)
print(dp[H])

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[i],b[i] = map(int,input().split())
cost = [int(1e+9) for _ in range(h+1)]
cost[0] = 0
for i in range(h):
    for j in range(n):
        if i+a[j] <= h:
            cost[i+a[j]] = min(cost[i+a[j]],cost[i]+b[j])
        else:
            cost[-1] = min(cost[-1],cost[i]+b[j])
print(cost[-1])

Solution (Python):
import sys
read = sys.stdin.read
def main():
    h, n = map(int, input().split())
    m = map(int, read().split())
    mm = zip(m, m)

    large_num = 10**9
    dp = [large_num] * (h + 10**4 + 1)
    dp[0] = 0
    for a, b in mm:
        for i1 in range(h + 1):
            if dp[i1 + a] > dp[i1] + b:
                dp[i1 + a] = dp[i1] + b
    r = min(dp[h:])
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
def main():
    h, n = map(int, input().split())
    m = map(int, read().split())
    mm = zip(m, m)

    large_num = 10**9
    dp = [large_num] * (h + 10**4 + 1)
    dp[0] = 0
    for a, b in mm:
        for i1 in range(h + 1):
            if dp[i1 + a] > dp[i1] + b:
                dp[i1 + a] = dp[i1] + b
    r = min(dp[h:])
    print(r)

if __name__ == '__main__':
    main()

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

    large_num = 10**9
    dp = [large_num] * (h + 10**4 + 1)
    dp[0] = 0
    for abe in ab:
        a = abe[0]
        b = abe[1]
        for i1 in range(h + 1):
            if dp[i1 + a] > dp[i1] + b:
                dp[i1 + a] = dp[i1] + b
    r = min(dp[h:])

    print(r)

if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
h,n=map(int,input().split())
ab=[[] for i in range(n)]
for i in range(n):
  ab[i]=list(map(int,input().split()))
  
dp=[100000000 for i in range(h+1)]
dp[0]=0
for hi in range(h):
  for i in range(n):
    damage=min(h,hi+ab[i][0])
    dp[damage] = min(dp[damage],dp[hi]+ab[i][1])
    
print(dp[h])

Solution (Python):



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


mp_list = [float('INF')] * (H+1)
mp_list[0] = 0


for i in range(1, H+1):
    for ab in ab_pattern:
        a = ab[0]
        b = ab[1]
        if i-a>=0:
            mp_list[i] = min(mp_list[i], mp_list[i-a] + b)
        else:
            mp_list[i] = min(mp_list[i], mp_list[0] + b)


print(mp_list[H])

Solution (Python):
h, n = map(int, input().split())
dp = [10**10]*(h+10**4+1)
dp[0] = 0

for _ in range(n):
  a, b = map(int, input().split())
  for i in range(a, len(dp)):
    dp[i] = min(dp[i-a]+b, dp[i])

print(min(dp[h:]))

Solution (Python):
"""何回でも同じ荷物を選べるナップザック問題"""
def main():
    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(1,N+1):
        for j in range(H+1):
            dp[i][j] = min(dp[i][j], dp[i-1][j])
            if j-A[i-1]>=0:
                dp[i][j] = min(dp[i][j], dp[i-1][j-A[i-1]]+B[i-1])
                dp[i][j] = min(dp[i][j], dp[i][j-A[i-1]]+B[i-1])
            else:
                dp[i][j] = min(dp[i][j], B[i-1])
    print(dp[N][H])

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

dp = [[1e18] * (h + 1) for _ in range(n + 1)]
dp[0][0] = 0

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

print(dp[n][h])

Solution (Python):
H, N = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
limit = H + max(A[i][0] for i in range(N))
dp = [int(1e18) for _ in range(limit+1)]
dp[0] = 0
for i in range(1, limit+1):
  for j in range(N):
    if A[j][0] > i:
      dp[i] = min(dp[i], A[j][1])
    else:
      dp[i] = min(dp[i], dp[i-A[j][0]]+A[j][1])
M = min(dp[i] for i in range(H, limit+1))
print(dp[H])

Solution (Python):

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

max_a = max(a for a,b in ab_list)
W = H + 1 + max_a
dp = [0] * W

for i in range(W):
    dp[i] = min(dp[i-a]+b for a,b in ab_list)

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

Solution (Python):
h,n = map(int,input().split())
data = []
ans = 0
M = -1
for _ in range(n):
    a,b = map(int,input().split())
    data.append((a,b))
    M = max(a,M)
dp = [float("inf") for _ in range(h+M)]
dp[0] = 0
for i,j in data:
    for k in range(h+M-1):
        if k+i <= h+M-1:
            dp[k+i] = min(dp[k]+j,dp[k+i])
print(min(dp[h:]))

Solution (Python):
import sys

input = lambda: sys.stdin.readline().rstrip()
INF = 10 ** 9 + 7

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


def solve():

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

    for h in range(H, 0, -1):

        if dp[h] == INF:
            continue
        next_dp = dp

        for i in range(N):
            a, b = AB[i]
            hh = max(0, h - a)
            next_dp[hh] = min(dp[hh], dp[h] + b)

        dp = next_dp

    print(dp[0])


if __name__ == '__main__':
    solve()

Solution (Python):
a = list(map(int, input().split()))
HP = a[0]

x = []
for i in range(a[1]):
    x1, y1 = [int(i) for i in input().split()]
    x.append((x1, y1))

max_tup = max(x, key=lambda x: x[0]) 
max_a = max_tup[0] 

dp = [10**10] * (HP + max_a)  
dp[0] = 0

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

print(min(dp[HP:]))

Solution (Python):
import sys

input = sys.stdin.read

def main(H, N, tmp):
    damage = tmp[::2]
    magic = tmp[1::2]
    DP = [float('inf')] * 20001
    DP[0] = 0
    for i in range(20001):
        for j in range(N):
            s = i - damage[j]
            if s >= 0:
                DP[i] = min(DP[i], DP[s] + magic[j])
    return min(DP[H:])


if __name__ == "__main__":
    H, N, *tmp = map(int, input().split())
    ans = main(H, N, tmp)
    print(ans)

Solution (Python):
a = list(map(int, input().split()))
HP = a[0]

x = []
for i in range(a[1]):
    x1, y1 = [int(i) for i in input().split()]
    x.append((x1, y1))

max_tup = max(x, key=lambda x: x[0])
max_a = max_tup[0]
dp = [0] * (HP + max_a)

for i in range(1, len(dp)):
    dp[i] = min(dp[i - a] + b for a, b in x)

print(dp[HP])

Solution (Python):

import sys

sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
from heapq import heappop, heappush









def run():
    H, N = map(int, sysread().split())
    AB = list(map(int, read().split()))
    A = AB[::2]
    B = AB[1::2]
    Amax = max(A)

    INF = float('inf')
    dp = [[INF] * (H+Amax+1) for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(H+Amax+1):
            tmp = j - A[i]
            if tmp >= 0:
                dp[i+1][j] = min([dp[i][j], dp[i+1][tmp] + B[i]])
            elif j == H and tmp < 0:
                dp[i + 1][H] = min(dp[i][H], dp[i + 1][0] + B[i])
            else:
                dp[i+1][j] = dp[i][j]
    ret = min(dp[N][H:])
    print(ret)



if __name__ == "__main__":
    run()

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

magic = [float("inf")] * (h + 1)
magic[0] = 0

for i in range(n):
    for j in range(0, h + 1):
        if j + a[i] <= h:
            magic[j + a[i]] = min(magic[j + a[i]], magic[j] + b[i])
        else:
            magic[h] = min(magic[h], magic[j] + b[i])

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

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

dp[h]=0

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

print(dp[0])

Solution (Python):
from sys import stdin


def rec_z():
    for i in range(N):
        for j in range(Hmax+1):
            if j >= a[i]:
                dp[j] = min(dp[j], dp[j-a[i]] + b[i])
    return min(dp[H:Hmax+1])
 
 
readline=stdin.readline
H, N = map(int,readline().split())
a = []
b = []
for i in range(N):
    aa, bb = map(int, readline().split())
    a.append(aa)
    b.append(bb)
Hmax = H + max(a)
INF = 10**9
dp = [INF for _ in range(Hmax+1)] 
dp[0] = 0
print(rec_z())

Solution (Python):
def rec_z():
    for i in range(N):
        for j in range(Hmax+1):
            if j >= a[i]:
                dp[j] = min(dp[j], dp[j-a[i]] + b[i])
    return min(dp[H:Hmax+1])
 
 
H, N = map(int,input().split())
a = []
b = []
for i in range(N):
    aa, bb = map(int, input().split())
    a.append(aa)
    b.append(bb)
Hmax = H + max(a)
INF = 10**9
dp = [INF for _ in range(Hmax+1)] 
dp[0] = 0
print(rec_z())

Solution (Python):
mod = 1e9+7
def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c
"""
Given H and N pairs, pairs[0] is attack while pairs[1] is cost. H is the monster health. Find the
minimum cost to get to H

knapsack

dp[i][j] - Minimum cost to reach up to j, such that only first i-th elements are considered

For each new element, its optimal is
dp[i][j] = max(dp[i-1][j-cost[i]], dp[i-1][j])
"""
def main():
    INF = 1e9 + 7
    H, N = [int(x) for x in raw_input().split()]
    value = [0 for _ in range(N)]
    cost = [0 for _ in range(N)]
    
    for i in range(N):
        A, B = [int(x) for x in raw_input().split()]
        value[i] = A
        cost[i] = B
    
    
    dp = [INF for _ in range(H+1)]
    
    dp[0] = 0
    
    for i in range(N):
        
        for j in range(H+1):
            after_health = max(0, j - value[i])
            dp[j] = min(dp[after_health] + cost[i], dp[j])
        
            
            
    print(dp[H])
    

main()

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)

dp = [float('inf')]*(H+1)
dp[0] = 0
for h in range(H+1):
    for n in range(N):
        damage = Alist[n]
        cost = Blist[n]
        first_index = min(h + damage, H)
        dp[first_index] = min(dp[h] + cost, dp[first_index])
        
print(dp[-1])

Solution (Python):
h,n = map(int,input().split())
Mag = []
for i in range(n):
    AB = list(map(int,input().split()))
    Mag.append(AB)
dp = [[float("inf")]*(h+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for j in range(h+1):
        dp[i+1][j] = min(dp[i+1][j],dp[i][j])
        dp[i+1][min(j+Mag[i][0],h)] = min(dp[i+1][min(j+Mag[i][0],h)],dp[i+1][j]+Mag[i][1])
        
print(dp[n][h])

Solution (Python):
def main():
    H, N = map(int, input().split())
    magic = [list(map(int, input().split())) for _ in range((N))]
    dp = [float('inf')] * (H + 1)
    dp[0] = 0
    for h in range(H):
        for dmg, cst in magic:
            next = min(h + dmg, H)
            dp[next] = min(dp[next], dp[h] + cst)
    print(dp[h+1])
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)
inf = 10**10
f = H+max(A)+1
dp = [[inf]*f for _ in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
    dp[i][0] = 0
    for j in range(f):
        dp[i][j] = dp[i-1][j]
        if j-A[i-1]>=0:
            dp[i][j] = min(dp[i][j],dp[i][j-A[i-1]]+B[i-1])
print(min(dp[-1][H:]))

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

INF = 1 << 60

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

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

print(dp[h])

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")



def main():
    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 = [INF] * 10000100
    dp[0] = 0
    for i in range(H):
        if dp[i] == INF:
            continue
        for n in range(N):
            dp[i+A[n]] = min(dp[i+A[n]], dp[i] + B[n])
    
    ans = INF
    for i in range(H, 10000100):
        ans = min(ans, dp[i])
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict,deque
from sys import stdin,setrecursionlimit
import heapq,bisect,math,itertools,string,queue,datetime,copy
setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, stdin.readline().split()))


H,N = inpl()
AB  = []
for _ in range(N):
    a= inpl()
    AB.append(a)
dp = [[INF]*(H+1) for _ in range(N)]
dp[0][0] = 0
for i in range(N):
    for j in range(H+1):
        at = AB[i][0]
        cost = AB[i][1]
        dp[i][j] = min(dp[i][max(j-at,0)]+cost,dp[max(0,i-1)][j])

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

Solution (Python):
INF = 10**9

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
dp = [INF for i in range(H+1)]

dp[0] = 0

for i in range(N):
    a = AB[i][0]
    b = AB[i][1]
    for j in range(H+1):
        next_j = min(j+a, H)
        dp[next_j] = min(dp[next_j], dp[j]+b)

print(dp[H])

Solution (Python):

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

A=[]
B=[]
DP=[0]+[float('Inf')]*(2*10**4)

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

for j in range(10**4):
  for i in range(N):
    DP[j+A[i]]=min(B[i]+DP[j],DP[j+A[i]])

print(min(DP[H:]))

Solution (Python):
h, n = map(int, input().split())
magics = [list(map(int, input().split())) for _ in range(n)]
max_a = max(a for a,b in magics)
dp = [0] * (h+max_a)

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

Solution (Python):
from sys import stdin
import numpy as np
def main():
    
    readline=stdin.readline
    h,n=map(int,readline().split())
    ab=np.array([list(map(int,readline().split())) for _ in range(n)],dtype=np.int64)
    a=ab[:,0]
    b=ab[:,1]

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

    print(dp[h])

if __name__=="__main__":
    main()

Solution (Python):
from sys import stdin, maxsize

def main():
    input = stdin.readline

    h, n = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(n)]
    dp = [maxsize] * (h + 1)
    dp[0] = 0
    for i in range(h + 1):
        for x, y in a:
            if i + x < h and dp[i + x] > dp[i] + y:
                dp[i + x] = dp[i] + y
            elif i + x >= h and dp[h] > dp[i] + y:
                dp[h] = dp[i] + y
    print(dp[h])
main()