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



Sample input:
9 3
8 3
4 2
2 1


Sample output:
4

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
np_ab = np.array(ab)
max_a = max(np_ab[:, 0])
dp = np.full(H+max_a, np.inf)
dp[:max_a] = 0
for i in range(max_a, H+max_a):
    for a, b in ab:
        dp[i] = min(dp[i], dp[i-a] + b)
print(int(dp[-1]))

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
mahou = []
a_max = 0
for _ in range(N):
    a, b = map(int, input().split())
    mahou.append([a, b])
    a_max = max(a_max, a)

dp = [[10 ** 9 for _ in range(H + a_max)] for _ 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(H + a_max):
        if j >= mahou[i - 1][0]:
            dp[i][j] = min(dp[i][j - mahou[i - 1][0]] + mahou[i - 1][1], dp[i - 1][j])
        else:
            dp[i][j] = dp[i - 1][j]

print(min(dp[N][H:]))

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for _ in range(N)]
dp = [float('inf')] * (H + 1)
dp[0] = 0
for h in range(H):
    if dp[h] == float('inf'):
        continue
    for a, b in M:
        idx = min(H, h + a)
        dp[idx] = min(dp[idx], dp[h] + b)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
a_list = []
b_list = []

for _ in range(n):
    a, b = map(int, input().split())
    a_list.append(a)
    b_list.append(b)

max_a = max(a_list)    
dp = [float('inf')] * (h + max_a + 1)

dp[0] = 0
cost = float('inf')
for i in range(h):
    for j in range(n):
        dp[i + a_list[j]] = min(dp[i] + b_list[j], dp[i + a_list[j]])
        if i + a_list[j] >= h:
            cost = min(cost, dp[i + a_list[j]])
print(cost)

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
a_list = []
b_list = []

for _ in range(n):
    a, b = map(int, input().split())
    a_list.append(a)
    b_list.append(b)

max_a = max(a_list)    
dp = [float('inf')] * (h + max_a + 1)

dp[0] = 0
for i in range(h):
    for j in range(n):
        dp[i + a_list[j]] = min(dp[i] + b_list[j], dp[i + a_list[j]])
print(min(dp[h:]))

Submission status: Time Limit Exceeded

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

    INF = 1 << 30

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

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

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()

h,n=MI()
lis=[LI() for i in range(n)]
mx=sorted(lis,reverse=True)[0][0]
#print(mx)
dp=[0 for i in range(h+mx)]
for i in range(1,h+mx):
    mn=inf
    #print(i,i)
    for j in range(n):
        u=dp[max(i-lis[j][0],0)]+lis[j][1]
        #print(u)
        if u<mn:
            mn=u
    dp[i]=mn
#print(dp)
z=[]
for i in range(mx):
    z.append(dp[h+i])
print(min(z))

Submission status: Time Limit Exceeded

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

ab_input = [[int(j) for j in input().split()] for i in range(n)]
#配るdp
dp =[10**10]*(h+1)
dp[0]=0
for i in range(h):
    for a,b in ab_input:
        next_index=min(h,i+a)
        dp[next_index]=min(dp[next_index],dp[i]+b)
print(dp[-1])

Submission status: Time Limit Exceeded

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

a_li = [0]*n
b_li = [0]*n

for i in range(n):
    a_li[i],b_li[i] = map(int,input().split())
#配るdp
dp =[10**10]*(h+1)
dp[0]=0
for i in range(h):
    for j in range(n):
        next_index=min(h,i+a_li[j])
        dp[next_index]=min(dp[next_index],dp[i]+b_li[j])
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
H,N=map(int, input().split())
M=[]
for i in range(N):
    a,b=map(int, input().split())
    M.append((a,b))

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

print(dp[H])
#print(dp)

Submission status: Time Limit Exceeded

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

a_li = [0]*n
b_li = [0]*n

for i in range(n):
    a_li[i],b_li[i] = map(int,input().split())
#もらうdp
dp =[10**10]*(h+1)
dp[-1]=0
for i in range(h,-1,-1):
    for j in range(n):
        if i-a_li[j]<=0:
            d=0
        else:
            d=i-a_li[j]
        dp[d]=min(dp[d],dp[i]+b_li[j])
print(dp[0])

Submission status: Time Limit Exceeded

Solution code:

def main():
    H, N = map(int, input().split())
    AB = [tuple(map(int, input().split())) for i in range(N)]

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

    for i in range(H, 0, -1):
        for a, b in AB:

            if dp[i] >= 0:
                next = max(i-a, 0)
                if dp[next] < 0:
                    dp[next] = dp[i] + b
                else:
                    if dp[next] > dp[i] + b:
                        dp[next] = dp[i] + b

    print(dp[0])
main()

Submission status: Time Limit Exceeded

Solution code:
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=[]
M.append(0)
for i in range(h):
  M.append(10000000000000)
for i in range(0,h+1):
  for j in range(n):
    c=min(h,a[j]+i)
    if M[c]>M[i]+b[j]:
      M[c]=M[i]+b[j]
print(M[h])

Submission status: Time Limit Exceeded

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

dp = [10**18] * (H + 1)
dp[0] = 0
for a, b in AB:
    for h in range(H + 1):
        r = min(H, h + a)
        if dp[r] > dp[h] + b:
            dp[r] = dp[h] + b
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:

h,n = list(map(int,input().split()))
inf = 100000000000000000000
dp = [inf]*(2*100000)
for i in range(-1,-100000,-1):
    dp[i] = 0
ls = [0]*n
for i in range(n):
    ls[i] = list(map(int,input().split()))
dp[0] = 0
for i in range(h):
    for j in range(n):
        dp[i+1] = min(dp[i+1],dp[i+1-ls[j][0]]+ls[j][1])
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    h,n=map(int,input().split())
    a=[]
    b=[]
    for i in range(n):
        ai,bi=map(int,input().split())
        a.append(ai)
        b.append(bi)
    inf=max(b)*h
    
    #dp[i]=体力をi減らすために消耗する魔力の最小値
    dp=[inf]*(h+max(a))
    dp[0]=0
    for i in range(h):
        for j in range(n):
            dp[i+a[j]]=min(dp[i+a[j]],dp[i]+b[j])
    print(min(dp[h:]))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# minimum power to give i damage to the enemy
import math

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

powlist=[]
for i in range(h+amax):
    powlist.append(math.ceil((i+1)/ablist[0][0])*ablist[0][1])

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

print(min(powlist[h-1:]))

Submission status: Time Limit Exceeded

Solution code:
h,n=(int(i) for i in input().split())
l=[]
for i in range(n):
  a,b=(int(i) for i in input().split())
  l.append([a,b])
pocket=[0]
for i in range(h):
  r=[]
  for j in range(n):
    a=l[j][0]
    b=l[j][1]
    r.append(pocket[max(0,i+1-a)]+b)
  pocket.append(min(r))
print(pocket[h])

Submission status: Time Limit Exceeded

Solution code:
h,n=(int(i) for i in input().split())
l=[]
for i in range(n):
  a,b=(int(i) for i in input().split())
  l.append([a,b])
pocket=[0]
for i in range(h):
  mini=pocket[max(0,i+1-l[0][0])]+l[0][1]
  for j in range(n-1):
    a=l[j+1][0]
    b=l[j+1][1]
    mini=min(mini,pocket[max(0,i+1-a)]+b)
  pocket.append(mini)
print(pocket[h])

Submission status: Time Limit Exceeded

Solution code:
h,n=(int(i) for i in input().split())
l=[]
for i in range(n):
  a,b=(int(i) for i in input().split())
  l.append([a,b])
pocket=[0]
for i in range(h):
  mini=pocket[max(0,i+1-l[0][0])]+l[0][1]
  for j in range(n-1):
    a=l[j+1][0]
    b=l[j+1][1]
    if pocket[max(0,i+1-a)]+b<mini:
      mini=pocket[max(0,i+1-a)]+b
  pocket.append(mini)
print(pocket[h])

Submission status: Time Limit Exceeded

Solution code:
INF = 10**15
h, n = map(int, input().split())
ab = []
for _ in range(n) : ab.append(list(map(int, input().split())))
dp = [INF]*(h+1)
dp[0] = 0
for i in range(n):
    for j in range(h+1):
        dp[min(j+ab[i][0], h)] = min(dp[min(j+ab[i][0], h)], dp[j] + ab[i][1])
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
H,n = map(int,input().split())
A = [0 for _ in range(n)]
B = [0 for _ in range(n)]
min_eff = 9999.
for i in range(n):
    A[i],B[i] = map(int,input().split())
    min_eff = min(min_eff,A[i]/B[i])

dp = [[int(H/min_eff+1) for _ in range(H+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+1):
        idx = max(0,h-A[i])
        dp[i+1][h] = min(dp[i+1][idx] + B[i],dp[i][h])

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

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

INF = 10 ** 10

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

A_max = max(map(itemgetter(0), AB))

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

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

print(min(dp[H:]))

Submission status: Time Limit Exceeded

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

dp = [0]

while dp[-1] < H:
    elem = 0
    for A, B in AB:
        if B <= len(dp): 
            elem = max(elem, dp[-B] + A)
    dp.append(elem)

print(len(dp) - 1)

Submission status: Time Limit Exceeded

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

inf = 10**9
magic = []
c = 0
for i in range(n):
	a, b = map(int, input().split())
	magic.append([a,b])
	c = max(c,a)

dp = [inf] * (h+c)

for i, j in magic:
	dp[i] = min(dp[i],j)

for i in range(len(dp)):
	for j in range(n):
		if i - magic[j][0] <= 0 or dp[i-magic[j][0]] == inf:
			continue
		else:
			dp[i] = min(dp[i],dp[i-magic[j][0]] + magic[j][1])

print (min(dp[h:]))

Submission status: Time Limit Exceeded

Solution code:
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 = [float("inf")] * (h+1)
dp[0] = 0

for i in range(h):
    for j in range(n):
        next_index = min(i + A[j], h)
        dp[next_index] = min(dp[next_index], dp[i] + B[j])

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split(' '))
magic = [[0 for _ in range(3)] for j in range(N)]
for i in range(N):
  temp_a, temp_b = map(int, input().split())
  magic[i][0] = temp_a
  magic[i][1] = temp_b

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

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split(' '))
magic = [[0 for _ in range(3)] for j in range(N)]
total_cost = 0
for i in range(N):
  temp_a, temp_b = map(int, input().split())
  magic[i][0] = temp_a
  magic[i][1] = temp_b
  magic[i][2] = temp_a / temp_b

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
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])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, n = map(int, readline().split())
ab = [list(map(int, readline().split())) for _ in range(n)]
inf = float('inf')
dp = [inf] * (h + 1)
dp[0] = 0
for i, (a, b) in enumerate(ab):
    for j in range(h):
        if j + a >= h:
            dp[-1] = min(dp[-1], dp[j] + b)
        else:
            dp[j + a] = min(dp[j + a], dp[j] + b)
print(dp[-1])

Submission status: Time Limit Exceeded

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

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

for h in range(H):
	for damage, cost in Magic:
		next_index = min(h + damage, H)
		dp[next_index] = min(dp[next_index], dp[h] + cost)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
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
INF = 1 << 30
dp = [INF] * (H + 1) # dp[i]: モンスターの体力を i 減らすため消耗する魔力の最小値
dp[0] = 0
for h in range(H + 1):
	for n in range(N):
		dp[h] = min(dp[h], B[n] + dp[max(h - A[n], 0)])

print(dp[H])

Submission status: Time Limit Exceeded

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

cost=[float("inf")]*(h+1)
cost[0]=0
for i in range(1,h+1):
    for j in range(0,n):
        cost[i]=min(cost[i],cost[max(i-a[j],0)]+b[j])

print(cost[-1])

Submission status: Time Limit Exceeded

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

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

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

print(dp[N][H])

Submission status: Time Limit Exceeded

Solution code:
import sys

import numpy as np

input = sys.stdin.readline


def main():
    H, N = map(int, input().split())
    A = np.zeros(N, dtype=np.int32)
    B = np.zeros(N, dtype=np.int32)
    for i in range(N):
        A[i], B[i] = map(int, input().split())

    dp = np.full(H + max(A), 10**8, dtype=np.int32)
    dp[0] = 0
    for h in range(H):
        for a, b in zip(A, B):
            if dp[h]+b < dp[h+a]:
                dp[h+a] = dp[h]+b
    ans = min(dp[H:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

INF = float("inf")


def main():
    input = sys.stdin.readline
    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] * (H + max(A))
    dp[0] = 0
    for h in range(H):
        for a, b in zip(A, B):
            dp[h+a] = min(dp[h+a], dp[h]+b)
    ans = min(dp[H:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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] * (H + max(A))
    dp[0] = 0
    for h in range(H):
        for a, b in zip(A, B):
            dp[h+a] = min(dp[h+a], dp[h]+b)
    ans = min(dp[H:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h, n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in [0]*n]
    dp = [10**10]*(h+1)
    dp[0] = 0
    for i in range(h):
        p = dp[i]
        for a, b in ab:
            j = i+a
            if j > h:
                j = h
            dp[j] = min(dp[j], p+b)
    print(dp[-1])


main()

Submission status: Time Limit Exceeded

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

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

DP = [0] + [10**9]*H

for h in range(H):
  if DP[h] == 10**9:
    continue
  for damage, mp in magic:
    DP[min(h + damage, H)] = min(DP[min(h + damage, H)], DP[h] + mp)
print(DP[H])

Submission status: Time Limit Exceeded

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

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

DP = [0] + [10**9]*H

for h in range(H):
  for damage, mp in magic:
    DP[min(h + damage, H)] = min(DP[min(h + damage, H)], DP[h] + mp)
print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


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

    max_A = max(A)
    dp = [float("inf")] * (H + max_A)
    dp[0] = 0
    for h in range(1, H + max_A):
        dp[h] = min(dp[max(0, h-a)]+b for a, b in zip(A, B))
    ans = min(dp[H:])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


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 = [float("inf")] * (H + 1 + max(A) - 1)
    dp[0] = 0
    for h in range(H):
        for a, b in zip(A, B):
            dp[h+a] = min(dp[h+a], dp[h]+b)
    ans = min(dp[H:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


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 = [float("inf")] * (H + 1)
    dp[0] = 0
    for h in range(H):
        for a, b in zip(A, B):
            next_h = min(H, h+a)
            dp[next_h] = min(dp[next_h], dp[h]+b)
    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
li = [[int(i) for i in input().split()] for j in range(N)]


def f(i, h):
    # i: index of magics
    # h: remaining HP
    if h <= 0:
        return 0
    A, B = li[i]
    k = -((-h) // A)
    ans = B * k
    if i == N - 1:
        return ans
    for j in range(k + 1):
        ans = min(ans, B * j + f(i + 1, h - A * j))
    return ans


print(f(0, H))

Submission status: Time Limit Exceeded

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

#dp配列
# dp=[[float('inf')]*(H+1) for _ in range(N+1)]
# for j in range(H+1):
#     dp[0][j] =0

# for i in range(N):
#     A_i,B_i = map(int,input().split())
#     for a in range(H+1):
#         if A_i<=a:
#             dp[i+1][a] = min(dp[i][a-A_i] + B_i , dp[i][a])
#         else:
#             dp[i+1][a] = dp[i][a]

# print(*dp,sep="\n")

# print(dp[N][H])
dp = [float('inf')]*(H+1)
dp[0] = 0

for i in range(N):
    A_i,B_i = map(int,input().split())
    for j in range(H+1):
        nj = min(j+A_i,H)
        dp[nj] = min(dp[nj],dp[j] + B_i)
# print(*dp,sep="\n")
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H,N = list(map(int,input().split()))
dp = [float("inf")] * (H+1)

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

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

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
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)
dp = [float("inf")] * (H+1)

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

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, N = map(int, input().split())
    M = [tuple(map(int, readline().split())) for _ in range(N)]
    INF = float('inf')
    dp = [INF] * (H + 1)
    dp[0] = 0
    
    for i in range(N):
        a, b = M[i]
        for j in range(H):
            nj = min(H, j + a)
            dp[nj] = min(dp[nj], dp[j] + b)

    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int,input().split())
a = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
    ai ,bi = map(int,input().split())
    a[i] = ai
    b[i] = bi


mh = [0 for i in range(10003)]
for i in range(1,h+1):
    mb = 10**9
    for j in range(n):
        if mb > mh[i-a[j]] + b[j]:
            mb = mh[i-a[j]] + b[j]
    mh[i] = mb

print(mh[h])

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
l=[]
s=0
for i in range(n):
    a,b=map(int,input().split())
    s=max(s,a)
    l.append((a,b))
dp=[10**10]*(h+s)
dp[0]=0
for i in range(h):
    for s,t in l:
        dp[i+s]=min(dp[i]+t,dp[i+s])
print(min(dp[h:]))

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
a = []
b = []
for _ in range(n):
  tmp = list(map(int,input().split()))
  a.append(tmp[0])
  b.append(tmp[1])

dp = [[10**9 for _ in range(h+1)] for _ in range(n+1)]

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

print(dp[n][h])

Submission status: Time Limit Exceeded

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

dp = list([9e9] * (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+a[i], h)] = min(dp[i+1][min(j+a[i], h)], dp[i+1][j] + b[i])

print(dp[n][h])

Submission status: Time Limit Exceeded

Solution code:
H,N =map(int, input().split())
a = [] 
M = 0 
for i in range(N):
	t = list(map(int, input().split()))
	a.append(t)
	M = max(M,t[0])
dp = [100000000]*(H+1+M)
dp[0] = 0
for i in range(H):
	for j,k in a:
		dp[i+j] = min(dp[i+j],dp[i]+k)

print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.buffer.readline
H,N = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N)]
dp = [10000000000]*(H+1)
dp[0] = 0
for i in range(1,H+1):
    for j in range(N):
        A = ab[j][0]
        B = ab[j][1]
        if i - A < 0:
            dp[i] = min(dp[i],dp[0]+B)
        else:
            dp[i] = min(dp[i],dp[i-A]+B)
print(dp[H])

Submission status: Time Limit Exceeded

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

# DP
l = [0]
for i in range(1,h+1):
    cand = []
    for j in range(n):
        if i - a[j] >= 0 and l[i - a[j]] != 'none':
            cand.append(l[i - a[j]] + b[j])
    if cand == []:
        l.append('none')
    else:
        l.append(min(cand))
for i in range(h+1, h+max(a)):
    cand = []
    for j in range(n):
        if 0 <= i - a[j] < h and l[i - a[j]] != 'none':
            cand.append(l[i - a[j]] + b[j])
    if cand == []:
        l.append('none')
    else:
        l.append(min(cand))
l = l[h:]
l = [i for i in l if type(i) == int]
print(min(l))

Submission status: Time Limit Exceeded

Solution code:
H ,N = [int(i) for i in input().split()]
AB = []
for _ in range(N):
    AB.append([int(i) for i in input().split(' ')])

MAX = 0
for a, b in AB:
    MAX = a if MAX < a else MAX

DP = [10**10] * (H+MAX)
DP[0] = 0
for i in range(H):
    for a,b in AB:
        if DP[i] != 10**10:
            DP[i+a] = min(DP[i+a],DP[i] + b)

print(min(DP[H:]))

Submission status: Time Limit Exceeded

Solution code:
import sys
f=lambda:map(int,sys.stdin.readline().split())
h,n=f()
dp=[10**9]*(h+1)
dp[0]=0
for i in range(n):
  a,b=f()
  for j in range(h+1):
    dp[j]=min(dp[j],dp[max(j-a,0)]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import sys
f=lambda:map(int,sys.stdin.readline().split())
h,n=f()
INF=10**9
dp=[[INF]*(h+1) for _ in range(n+1)]
dp[0][0]=0
for i in range(n):
  a,b=f()
  for j in range(h+1):
    dp[i+1][j]=min(dp[i][j],dp[i+1][max(j-a,0)]+b)
print(dp[n][h])

Submission status: Time Limit Exceeded

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

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

    for i in range(0, N):
        for h in range(0, H+1):
            dp[i+1][h] = min(dp[i+1][h], dp[i][h])
            dp[i+1][min(h+AB[i][0], H)] = min(dp[i+1][min(h+AB[i][0], H)], dp[i+1][h]+AB[i][1])

    ans = {}
    for i in range(N):
        ans[i] = dp[i][H]
    print(min(ans.values()))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int, input().split())
a = []
b = []
for i in range(n):
    tmp1, tmp2 = map(int, input().split())
    a.append(tmp1)
    b.append(tmp2)
dp = [100000000] * (h + max(a))
dp[0] = 0
for i in range(h + max(a)):
    if i == 0: continue
    for j in range(n):
        if  b[j] + dp[i - a[j]] < dp[i]:
            dp[i] = b[j] + dp[i - a[j]]
print(min(dp[h:h + max(a)]))

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int, input().split())
a = []
b = []
for i in range(n):
    tmp1, tmp2 = map(int, input().split())
    a.append(tmp1)
    b.append(tmp2)
dp = [100000000] * (h + max(a))
dp[0] = 0
for i in range(h + max(a)):
    if i == 0: continue
    for j in range(n):
        if  b[j] + dp[i - a[j]] < dp[i]:
            dp[i] = b[j] + dp[i - a[j]]
result = 100000000
for i in range(h,h + max(a)):
    if result > dp[i]:
        result = dp[i]
print(result)

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int, input().split())
a = []
b = []
for i in range(n):
    tmp1, tmp2 = map(int, input().split())
    a.append(tmp1)
    b.append(tmp2)
dp = [100000000] * (h + max(a))
dp[0] = 0
for i in range(h + max(a)):
    if i == 0: continue
    min = 100000000
    for j in range(n):
        if a[j] >= i and b[j] < min:
            min = b[j]
            continue
        elif a[j] < i and b[j] + dp[i - a[j]] < min:
            min = b[j] + dp[i - a[j]]
            continue
    dp[i] = min
result = 100000000
for i in range(h,h + max(a)):
    if result > dp[i]:
        result = dp[i]
print(result)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(30000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

dp_map = []
for i in range(N+1):
    temp_arr = [-1 for _ in range(H+1)]
    dp_map.append(temp_arr)
#dp_map = [[-1]*(H+1)]*(N+1)
#print(len(dp_map))
#print(len(dp_map[0]))

def DP(i, j):
    if j<=0:
        return 0
    if dp_map[i][j]!=-1:
        return dp_map[i][j]
    
    if i==1:
        dp_map[i][j] = DP(i,j-As[i-1])+Bs[i-1]
        return dp_map[i][j]

    dp_map[i][j] = min(DP(i-1,j),DP(i,j-As[i-1])+Bs[i-1])
    return dp_map[i][j]

print(DP(N,H))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(33000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

dp_map = []
for i in range(N+1):
    temp_arr = [-1 for _ in range(H+1)]
    dp_map.append(temp_arr)
#dp_map = [[-1]*(H+1)]*(N+1)
#print(len(dp_map))
#print(len(dp_map[0]))

def DP(i, j):
    if j<=0:
        return 0
    if dp_map[i][j]!=-1:
        return dp_map[i][j]
    
    if i==1:
        dp_map[i][j] = DP(i,j-As[i-1])+Bs[i-1]
        return dp_map[i][j]

    dp_map[i][j] = min(DP(i-1,j),DP(i,j-As[i-1])+Bs[i-1])
    return dp_map[i][j]

print(DP(N,H))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(60000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

dp_map = []
for i in range(N+1):
    temp_arr = [-1 for _ in range(H+1)]
    dp_map.append(temp_arr)
#dp_map = [[-1]*(H+1)]*(N+1)
#print(len(dp_map))
#print(len(dp_map[0]))

def DP(i, j):
    if j<=0:
        return 0
    if dp_map[i][j]!=-1:
        return dp_map[i][j]
    
    if i==1:
        dp_map[i][j] = DP(i,j-As[i-1])+Bs[i-1]
        return dp_map[i][j]

    dp_map[i][j] = min(DP(i-1,j),DP(i,j-As[i-1])+Bs[i-1])
    return dp_map[i][j]

print(DP(N,H))