Problem:
 Score : 500 points    Problem Statement Ibis is fighting with a monster.  The health of the monster is H .  Ibis can cast N kinds of spells. Casting the i -th spell decreases the monster's health by A_i , at the cost of B_i Magic Points.  The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.  Ibis wins when the health of the monster becomes 0 or below.  Find the minimum total Magic Points that have to be consumed before winning.      Constraints  1 \leq H \leq 10^4  1 \leq N \leq 10^3  1 \leq A_i \leq 10^4  1 \leq B_i \leq 10^4  All values in input are integers.         Input Input is given from Standard Input in the following format:  H  N  A_1  B_1  :  A_N  B_N       Output Print the minimum total Magic Points that have to be consumed before winning.        Sample Input 1 9 3
8 3
4 2
2 1      Sample Output 1 4  First, let us cast the first spell to decrease the monster's health by 8 , at the cost of 3 Magic Points. The monster's health is now 1 .  Then, cast the third spell to decrease the monster's health by 2 , at the cost of 1 Magic Point. The monster's health is now -1 .  In this way, we can win at the total cost of 4 Magic Points.       Sample Input 2 100 6
1 1
2 3
3 9
4 27
5 81
6 243      Sample Output 2 100  It is optimal to cast the first spell 100 times.       Sample Input 3 9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461      Sample Output 3 139815

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    inf=10**9
    h,n=MI()
    aa=[]
    bb=[]
    for _ in range(n):
        a,b=MI()
        aa.append(a)
        bb.append(b)
    dp=[inf]*(h+1)
    dp[0]=0
    for i in range(h):
        pre=dp[i]
        for a,b in zip(aa,bb):
            ni=h if i+a>h else i+a
            if pre+b<dp[ni]:dp[ni]=pre+b
    print(dp[h])

main()

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    H, N = map( int, input().split())
    AB = [ list( map( int, input().split())) for _ in range(N)]
    dp = [10**9]*H + [0]*(10**4+1)
    T = H+10**4
    for a, b in AB:
        for i in range(T,a-1,-1):
            if dp[i-a] > dp[i]+b:
                dp[i-a] = dp[i]+b
    print(dp[0])
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



H,N = II()
A,B = Line(N,2)

dp = [float('inf')]*(H+1)
dp[0] = 0
for i in range(1,H+1):
    for j in range(N):
        temp = i-A[j]
        if temp>=0:
            dp[i] = min(dp[i],dp[i-A[j]]+B[j])
        else:
            dp[i] = min(dp[i],dp[0]+B[j])

print(dp[-1])

Solution (Python):
H,N=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(N)]
dp=[0]*10001+[10**9]*H
for i in range(1,H+10001):
    for a,b in AB:
        if i>=a:
            dp[i]=min(dp[i],dp[i-a]+b)
print(dp[-1])

Solution (Python):
import sys

stdin = sys.stdin

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

h,n = nm()
v = [nl() for _ in range(n)]
dp = [1<<30]*(h+10020)
dp[0] = 0
for a,b in v:
    for i in range(h+1):
        if dp[i+a] > dp[i] + b:
            dp[i+a] = dp[i] + b
print(min(dp[h:]))

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

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

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


if __name__ == '__main__':
    main()

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

H, N = map(int, input().split())
AB = [[int(i) for i in input().split()] for _ in range(N)]
AB.sort(key=lambda x: x[0]/x[1], reverse=True)

INF = 10 ** 18
dp = [INF] * (H + 1)
dp[0] = 0
for A, B in AB :
  for i in range(H + 1) :
    if dp[i] != INF :
      ni = min(H, i + A)
      dp[ni] = min(dp[ni], dp[i] + B)
      
print(dp[-1])

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

D=[1<<31]*(H+10**4)
D[0]=0

for hp in range(H):
    for a,b in M:
        D[hp+a]=min(D[hp+a],D[hp]+b)
print(min(D[H:]))

Solution (Python):

import sys
INF = float("inf")


def solve(H: int, N: int, A: "List[int]", B: "List[int]"):
    DP = [INF]*(H+1)
    DP[0] = 0
    for i in range(H):
        for a, b in zip(A, B):
            m = min(i+a, H)
            DP[m] = min(DP[i]+b, DP[m])
    print(DP[-1])
    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 = [int(i) for i in input().split()]
AB = [[int(i) for i in input().split()] for j in range(N)]
DP = [10000000000]*(H+1)
DP[0] = 0
for h,curmp in enumerate(DP):
    for a,b in AB:
        goal = min(h+a,H)
        DP[goal] = min(DP[goal],curmp+b)
print(DP[-1])

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

Y = [0] + [1<<30] * H
for i in range(H):
    for a, b in X:
        j = min(i+a, H)
        Y[j] = min(Y[j], Y[i] + b)

print(Y[-1])

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(H: int, N: int, A: "List[int]", B: "List[int]"):
    dp = [float('inf')] * (H + 1)
    dp[0] = 0
    for i in range(N):
        for j in range(H + 1):
            tmp = min(H, j + A[i])
            dp[tmp] = min(dp[tmp], dp[j] + B[i])
    print(dp[H])
    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())
AB = [tuple(map(int, input().split())) for _ in range(N)]

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

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

print(dp[0])

Solution (Python):

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


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



if __name__ == '__main__':
    solve()

Solution (Python):
import sys

stdin = sys.stdin

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

h, n = na()
ab = []
for i in range(n):
    ab.append(na())

dp = [99999999999999999] * (h+1)
dp[h] = 0
ans = 9999999999999999999
for u in ab:
    for i in range(h, u[0]-1, -1):
        dp[i-u[0]] = min(dp[i-u[0]], dp[i] + u[1])
    for i in range(min(u[0]+1, h+1)):
        ans = min(ans, dp[i] + u[1])
print(ans)