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 sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines

#mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np


def main():
	h,n=map(int,input().split())
	AB=[list(map(int,input().split())) for _ in range(n)]
	
	li=[10**10]*(10**5)
	li[0]=0
	
	for i in range(h):
		if li[i]==10**10:
			continue
		else:
			for a,b in AB:
				li[i+a]=min(li[i+a],li[i]+b)
	
	print(min(li[h:]))
	
	
	
	
	
	

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

INF = 1000100001001

dp = [[INF]* (h + 1) for i in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1 ):
    for j in range(h + 1):
        inx = max(0, j - m[i -1 ][0])
        dp[i][j] = min(dp[i -1][j], dp[i][inx] + m[i -1 ][1])


print(dp[n ][h])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

#整数値入力 1文字の入力
def input_one_number():
    return int(input())

#整数値龍力　複数の入力
def input_multiple_number():
    return map(int, input().split())

#整数値龍力　複数の入力(配列)
def input_multiple_number_as_list():
    return list(map(int, input().split()))

#整数値龍力　複数の入力(配列で1つずつ渡される)
def input_multiple_number_as_list_sep(N):
    ins = []
    for i in range(N):
        ins.append(input())
    return ins

#2次元配列入力
def input_map():
    return [list(map(int,list(input()))) for i in range(h)]


H,N = input_multiple_number()
magics = []
maxA = 0

for i in range(N):
    A,B = input_multiple_number()
    cosper = A/B
    magics.append([A,B,cosper])
    if maxA < A: maxA = A

#magics.sort(key=lambda x:x[2],reverse=True)

inf=float("inf")
dp=[[inf for i in range(N+1)] for j in range(H+maxA+1)]
for i in range(N+1): dp[0][i]=0

for j in range(N):
    for i in range(H+maxA+1):
        dp[i][j+1] = min(dp[i-magics[j][0]][j+1]+magics[j][1],dp[i][j])

ds = dp[H:H+maxA][:]
mmm = []
for i in range(len(ds)):
    mmm.append(min(ds[i]))
print(min(mmm))

Submission status: Time Limit Exceeded

Solution code:
import sys
H, N = map(int, sys.stdin.readline().split())
m = map(int, sys.stdin.read().split())
AB = zip(m, m)
INF = 10 ** 20
dp = [[INF]*(H+10**4) for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
    a, b = next(AB)
    for j in range(H+10**4):
        dp[i][j] = min(dp[i-1][j], dp[i][j-a] + b if j-a >= 0 else INF)
print(min(dp[-1][H:]))

Submission status: Time Limit Exceeded

Solution code:
import sys
H, N = map(int, sys.stdin.readline().split())
m = map(int, sys.stdin.read().split())
AB = zip(m, m)
INF = float('inf')
dp = [[INF]*(H+10**4) for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
    a, b = next(AB)
    for j in range(H+10**4):
        dp[i][j] = min(dp[i-1][j], dp[i][j-a] + b if j-a >= 0 else INF)
# print(min([min(i[H:]) for i in dp]))
print(min(dp[-1][H:]))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(1000000)

H, N = [int(n) for n in input().split()]

A = [0] * N
B = [0] * N

ami = 0
for i in range(N):
    A[i], B[i] = [int(n) for n in input().split()]
    if A[i] > A[ami]:
        ami = i

i_s = list(range(N))
lmpis = sorted(i_s, key=lambda i: B[i])

M = {}
mi = 100000000
def m(h):
    global mi
    if h in M:
        return M[h]
    if h == 0:
        return 0
    for i in range(N):
        j = lmpis[i]
        if A[j] <= h:
            c = m(h-A[j])
            if c is not None:
                b = B[j] + c
                if (h not in M or (M[h] and M[h] > b)) and b < mi:
                    M[h] = b
                    if h >= H and b < mi:
                        mi = b
    if h not in M:
        M[h] = None
    return M[h]

for h in range(H, H + A[ami]):
    m(h)

print(mi)

Submission status: Time Limit Exceeded

Solution code:
import sys
H, N = map(int, sys.stdin.readline().split())
m = map(int, sys.stdin.read().split())
AB = zip(m, m)
INF = float('inf')
dp = [[INF]*(H+10**4) for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
    a, b = next(AB)
    for j in range(H+10**4):
        dp[i][j] = min(dp[i-1][j], dp[i][j-a] + b if j-a >= 0 else INF)
print(min([min(i[H:]) for i in dp]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

#整数値入力 1文字の入力
def input_one_number():
    return int(input())

#整数値龍力　複数の入力
def input_multiple_number():
    return map(int, input().split())

#整数値龍力　複数の入力(配列)
def input_multiple_number_as_list():
    return list(map(int, input().split()))

#整数値龍力　複数の入力(配列で1つずつ渡される)
def input_multiple_number_as_list_sep(N):
    ins = []
    for i in range(N):
        ins.append(input())
    return ins

#2次元配列入力
def input_map():
    return [list(map(int,list(input()))) for i in range(h)]


H,N = input_multiple_number()
magics = []
maxA = 0

for i in range(N):
    A,B = input_multiple_number()
    cosper = A/B
    magics.append([A,B,cosper])
    if maxA < A: maxA = A

magics.sort(key=lambda x:x[2],reverse=True)

inf=float("inf")
dp=[[inf for i in range(N+1)] for j in range(H+maxA+1)]
for i in range(N+1): dp[0][i]=0

for j in range(N):
    for i in range(H+maxA+1):
        dp[i][j+1] = min(dp[i-magics[j][0]][j+1]+magics[j][1],dp[i-magics[j][0]][j]+magics[j][1],dp[i][j])

ds = dp[H:H+maxA][:]
mmm = []
for i in range(len(ds)):
    mmm.append(min(ds[i]))
print(min(mmm))

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
A = [None] * N
B = [None] * N

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

lim = H + max(A)
dp = [10**8] * lim
dp[0] = 0

for h in range(lim):
    for n in range(N):
        rem = h - A[n]
        cns = B[n]

        if 0 < rem:
            cns += dp[rem]
        dp[h] = min(cns, dp[h])

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

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
A = [None] * N
B = [None] * N

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

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

for h in range(lim + 1):
    for n in range(N):
        rem = h - A[n]
        cns = B[n]
        if 0 < rem:
            cns += dp[rem]

        dp[h] = min(cns, dp[h])

print(min(dp[H:lim+1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, N = map(int, input().split())
A, B = np.zeros(N, dtype=np.int), np.zeros(N, dtype=np.int)

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

C = np.zeros(H+1, dtype=np.int)
min_cost_A = int(A[B.argmin()])
C[1:min_cost_A+1] = min(B)
sa = np.zeros(N, dtype=np.int)

def hoggg(C, A, B, i, N):
    for j in range(N):
        if A[j] >= i:
            sa[j] = B[j]
        else:
            sa[j] = C[i - A[j]] + B[j]
    return min(sa)

for i in range(min_cost_A, H+1):
    C[i] = hoggg(C, A, B, i, N)

print(int(C[H]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, N = map(int, input().split())
A, B = np.zeros(N, dtype=np.int), np.zeros(N, dtype=np.int)

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

C = np.zeros(H+1, dtype=np.int)
min_cost_A = int(A[B.argmin()])
C[1:min_cost_A+1] = min(B)


def hoggg(C, A, B, i, N):
    sa = np.full(N, 9999999999999999999999999999999999)
    for j in range(N):
        if A[j] >= i:
            sa[j] = B[j]
        else:
            sa[j] = C[i - A[j]] + B[j]
    return min(sa)

for i in range(min_cost_A, H+1):
    C[i] = hoggg(C, A, B, i, N)

print(int(C[H]))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
h, n = map(int, input().split())
INF = 10 ** 20
masics = []
for i in range(n):
  a, b = map(int, input().split())
  masics.append([a, b])
dp = [0] + [INF] * h

for masic in masics:
  for j in range(h):
    k = min(j+masic[0], h)
    dp[k] = min(dp[k], dp[j] + masic[1])

print(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)]
# ありえない値にしたいと仮定すると、最大値が10**7取りうるので10**7+1にする？
dp = [10**8] * (h+1)
dp[0] = 0

for i in range(1,h+1):
    for a,b in ab:
        if i >= a:
            # HPより小さいとき、減らした後のHPに対して消費するMPを足す
            atk = b + dp[i-a]
            if atk < dp[i]:
                dp[i] = atk
        elif b < dp[i]:
            # どの攻撃よりも小さいHPであれば、一番MP消費が少ない攻撃のMPを記録する
            dp[i] = b
# 配列の最後が、求めたいHP
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
#ボトムアップなDPで解決する。
H, N = map(int, input().split())
M = []
for i in range(N):
    M.append(list(map(int, input().split())))
toki = [0]
for i in range(1, H + 1):
    candy = []
    for j in M:
        if j[0] > i:
            candy.append(j[1])
        else:
            candy.append(j[1] + toki[i - j[0]])
    toki.append(min(candy))
print(toki[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

INF = 10**18
dp = [INF] * (20000)
dp[0] = 0

for a, b in AB:
    for n in range(H):
        if dp[n + a] > dp[n] + b:
            dp[n + a] = dp[n] + b

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter

def main():
    H,N = map(int,input().split())
    INF = 10**10
    at = []
    for _ in range(N):
        A,B = map(int,input().split())
        at.append((A,B))
        
    at.sort(key=itemgetter(0))
    
    dp = [INF for _ in range(H+1)]
    dp[0] = 0
    for i in range(N):
        use,a = at[i]
        for j in range(1,H+1):
            if j < use:
                dp[j] = min(dp[j],a)
            else:
                dp[j] = min(dp[j],dp[j-use]+a)
        
    print(dp[-1])
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
from operator import itemgetter

def main():
    H,N = map(int,input().split())
    INF = 10**10
    at = []
    for _ in range(N):
        A,B = map(int,input().split())
        at.append((A,B))
        
    at.sort(key=itemgetter(0))
    
    dp = [INF for _ in range(H+1)]
    dp[0] = 0
    for i in range(N):
        use,a = at[i]
        for j in range(1,H+1):
            if j < use:
                dp[j] = min(dp[j],a)
            else:
                dp[j] = min(dp[j],dp[j-use]+a)
        
    print(dp[-1])
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# ダイナミック・プログラミング

INF = float('inf')

H, N = map(int, input().split())
A = []
B = []

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

dp = [[INF]*(H+1) for i in range(N+1)] # DP用配列
dp[0][0] = 0 # 初期化処理
for i in range(0,N):
    for j in range(0,H+1):
        a, b = j+A[i], H
        min_h = a
        if a>b:
            min_h = b
        a, b = dp[i+1][j], dp[i][j]
        if a>b:
            dp[i+1][j] = b
        a, b = dp[i+1][min_h], dp[i+1][j]+B[i]
        if a>b:
            dp[i+1][min_h] = b

print(dp[N][H])

Submission status: Time Limit Exceeded

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

magic = [list(map(int, input().split())) for _ in range(N)]
inf = float('inf')
dp = [inf] * (H + 1)
dp[0] = 0
for i in range(H):
    if dp[i] == inf:
        continue
    currCost = dp[i]
    for dmg, cost in magic:
        totalDmg = i + dmg
        if totalDmg > H:
            totalDmg = H
        totalCost = currCost + cost
        if totalCost < dp[totalDmg]:
            dp[totalDmg] = totalCost
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
# ダイナミック・プログラミング

INF = float('inf')

H, N = map(int, input().split())
A = []
B = []

def chmin(a, b):
    if a>=b:
        return b
    else:
        return a

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

dp = [[INF]*(H+1) for i in range(N+1)] # DP用配列
dp[0][0] = 0 # 初期化処理
for i in range(0,N):
    for j in range(0,H+1):
        dp[i+1][j] = chmin(dp[i+1][j], dp[i][j])
        dp[i+1][min(j+A[i],H)] = chmin(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:
from sys import stdin
readline = stdin.readline
H,N=map(int,readline().split())
INF=int(10**8)
dp=[INF]*(H+1)
dp[0]=0
for _ in [0]*N:
    a,b=map(int,readline().split())
    for k in range(0,H):
        if k+a<H:
            nj=k+a
        else:nj=H
        if dp[nj]<dp[k]+b:
            dp[nj]=dp[nj]
        else:dp[nj]=dp[k]+b
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# sys.stdin.readline()
import sys
input = sys.stdin.readline
H,N=map(int,input().split())
INF=int(10**8)
dp=[INF]*(H+1)
dp[0]=0
for i in range(N):
    a,b=map(int,input().split())
    for j in range(0,H):
        nj=min(j+a,H)
        dp[nj]=min(dp[nj],dp[j]+b)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
# import math
# import numpy as np

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

# 二次元配列
# [[inf, inf, inf], [inf, inf, inf]]
# dp = [[float('inf')] * (H + 1) for i in range(N + 1)]

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

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

        for j in range(H):
            nj = min(j + a, H)
            dp[nj] = min(dp[nj], dp[j] + b)
    return dp[H]

print(solve())

Submission status: Time Limit Exceeded

Solution code:
# import math
# import numpy as np

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

# 二次元配列
# [[inf, inf, inf], [inf, inf, inf]]
# dp = [[float('inf')] * (H + 1) for i in range(N + 1)]

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

def solve():
    for i in range(N):
        for j in range(H):
            nj = min(j + A[i], H)
            dp[nj] = min(dp[nj], dp[j] + B[i])
    return dp[H]

print(solve())

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
H,N=map(int,input().split())
INF=int(10**8)
dp=[INF]*(H+1)
dp[0]=0
for i in range(N):
    a,b=map(int,input().split())
    for j in range(0,H):
        nj=min(j+a,H)
        dp[nj]=min(dp[nj],dp[j]+b)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
# import math
# import numpy as np

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

# 二次元配列
# [[inf, inf, inf], [inf, inf, inf]]
# dp = [[float('inf')] * (H + 1) for i in range(N + 1)]

INF = 10 ** 18
dp = [INF] * (20201)
dp[0] = 0

def solve():
    for i in range(H):
        for j in range(N):
            if dp[i] == INF:
                continue
            dp[i + A[j]] = min(dp[i + A[j]], dp[i] + B[j])
    return min(dp[H:])

print(solve())

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
H,N=map(int,input().split())
A=[]
B=[]
for _ in range(N):
     a,b= map(int, input().split())
     A.append(a)
     B.append(b)

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

print(M[H])

Submission status: Time Limit Exceeded

Solution code:
import sys
dp=[-1]*10005
sys.setrecursionlimit(10**5)
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())
def solve(health):
    if(health<=0):
        return 0
    if dp[health]!=-1:
        return dp[health]
    ans=100000000000
    for i in range(n):
        ans=min(ans,solve(health-a[i])+b[i])
        dp[health]=ans
    return ans
print(solve(h))

Submission status: Time Limit Exceeded

Solution code:
import sys
dp=[-1]*10005
sys.setrecursionlimit(10**7)
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())
def solve(health):
    if(health<=0):
        return 0
    if dp[health]!=-1:
        return dp[health]
    ans=100000000000
    for i in range(n):
        ans=min(ans,solve(health-a[i])+b[i])
        dp[health]=ans
    return ans
print(solve(h))

Submission status: Time Limit Exceeded

Solution code:
INF = 100100100

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

dp = [[INF for i in range(h+1)] for j 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+ab[i][0], h)] = min(dp[i+1][min(j+ab[i][0], h)], dp[i+1][j]+ab[i][1])

#for i in range(n+1) :
#    print(dp[i])

print(dp[n][h])

Submission status: Time Limit Exceeded

Solution code:
import sys
dp={}
sys.setrecursionlimit(10**7)
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())
def solve(health):
    if(health<=0):
        return 0
    if health in dp:
        return dp[health]
    ans=100000000000
    for i in range(n):
        ans=min(ans,solve(health-a[i])+b[i])
        dp[health]=ans
    return ans
print(solve(h))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(1000000)

H, N = [int(n) for n in input().split()]

A = [0] * N
B = [0] * N

ami = 0
for i in range(N):
    A[i], B[i] = [int(n) for n in input().split()]
    if A[i] > A[ami]:
        ami = i

i_s = list(range(N))
lmpis = sorted(i_s, key=lambda i: B[i])

M = {}
mi = 100000000
def m(h):
    global mi
    if h in M:
        return M[h]
    if h == 0:
        return 0
    for i in range(N):
        j = lmpis[i]
        if A[j] <= h:
            c = m(h-A[j])
            if c is not None:
                b = B[j] + c
                if h not in M or (M[h] and M[h] > b and b < mi):
                    M[h] = b
                    if h >= H and b < mi:
                        mi = b
    if h not in M:
        M[h] = None
    return M[h]

for h in range(H, H + A[ami]):
    m(h)

print(mi)

Submission status: Time Limit Exceeded

Solution code:
#import time
#start = time.time()
import numpy as np
H, N = map(int, input().split())
arr = np.array([tuple(map(int,input().split())) for _ in range(N)])
dp = np.full(H+1,np.inf)
dp[0] = 0
# O(NH)
for i in range(H):
    for j in range(N):
        #if dp[i+1] < arr[j,1]:
        #    break
        if i >= arr[j,0]:
            dp[i+1] = min(dp[i+1], arr[j,1] + dp[i+1-arr[j,0]])
        else:
            dp[i+1] = min(dp[i+1], arr[j,1])
print(int(dp[H]))
#print('ellapsed time:', time.time() - start)

Submission status: Time Limit Exceeded

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

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

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

for i in range(H+1):
    for j in range(N):
        a, b = spells[j]
        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 i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)


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

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import math
h, n = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]
A, B = map(list, zip(*AB))
yobu = max(A)
memo = [math.ceil(j / A[0]) * B[0] for j in range(h+yobu+1)]
for i in range(n):
    for j in range(h+1+yobu):
        if j >= A[i]:
            memo[j] = min(memo[j], memo[j-A[i]]+B[i])
print(min(memo[-yobu-1:-1]))

Submission status: Time Limit Exceeded

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

abKeys = set([])
result = [0] * (10 ** 8)
for _ in range(n):
    a, b = map(int, input().split())
    result[b] = a

for i in range(10 ** 8):
    j = 1
    while j <= i // 2:
        result[i] = max(result[i], result[j] + result[i - j])

        j += 1

    #if i % 100 == 0:
        #print(result[i], i, j, i - j)
    if result[i] >= h:
        print(i)
        exit()

#print(max(result))

Submission status: Time Limit Exceeded

Solution code:
#import time
#start = time.time()
from numpy import inf

H, N = map(int, input().split())
array = sorted([list(map(int, input().split())) for i in range(N)], key=lambda x: x[1])

d = [i[0] for i in array]
m = [i[1] for i in array]
dp = [inf for i in range(H+1)]
dp[0] = 0
# O(NH)
for i in range(H):
    for j in range(N):
        if dp[i+1] < m[j]:
            break
        if i >= d[j]:
            dp[i+1] = min(dp[i+1], m[j] + dp[i+1-d[j]])
        else:
            dp[i+1] = min(dp[i+1], m[j])

print(dp[H])
#print('ellapsed time:', time.time() - start)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

H,N = map(int, input().split())
table = []
dp = [10**9]*(H+1)
dp[0] = 0

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

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

Submission status: Time Limit Exceeded

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

a=[]
b=[]
INF=10**9
dp=[INF for i in range(h+10**4)]

for i in range(n):
  p,q=map(int,input().split())
  a.append(p)
  b.append(q)

dp[0]=0
for i in range(h):
  if dp[i]==INF:
    continue
  for j in range(n):
    if dp[i+a[j]]>=dp[i]+b[j]:
      dp[i+a[j]]=dp[i]+b[j]
print(min(dp[h:]))

Submission status: Time Limit Exceeded

Solution code:
#import time
#start = time.time()
from numpy import inf

H, N = map(int, input().split())
array = [list(map(int, input().split())) for i in range(N)]
d = [i[0] for i in array]
m = [i[1] for i in array]
dp = [inf for i in range(H+1)]
dp[0] = 0
# O(NH)
for i in range(H):
    for j in range(N):
        if i >= d[j]:
            dp[i+1] = min(dp[i+1], m[j] + dp[i+1-d[j]])
        else:
            dp[i+1] = min(dp[i+1], m[j])

print(dp[H])
#print('ellapsed time:', time.time() - start)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

A = np.zeros(N, dtype = int)
B = np.zeros(N, dtype = int)

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

Submission status: Time Limit Exceeded

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

    INF = 10 ** 18

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

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

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

    # for d in dp:
    #     print (d)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

INF = 10 ** 18

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

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

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

# for d in dp:
#     print (d)

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 18


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

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

# for d in dp:
#     print (d)

Submission status: Time Limit Exceeded

Solution code:
import sys
h, n = map(int, input().split())
a = [0]*n
b = [0]*n
ma = 0
for i in range(n):
    a[i], b[i] = map(int, input().split())
    ma = max(a[i], ma)

H = h + ma + 1
dp = [10**9]*H

dp[0] = 0
ans = 10**9
for i in range(1, H):
    for j in range(n):
        dp[i] = min(dp[max(i-a[j], 0)]+b[j], dp[i])
    if i >= h:
        ans = min(ans, dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
inf = 1001001001
dp = [inf for _ in range(h+1)]
dp[0] = 0
ans = inf
for i in range(n):
    a, b = map(int, input().split())
    for j in range(h + 1):
        # 外側参照しないように
        nj = min(j+a, h)
        dp[nj] = min(dp[nj], dp[j]+b)

print(dp[h])

Submission status: Time Limit Exceeded

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

# dp[i][j] = 魔法iまでからダメージの総和がj以上になるように選んだ時の魔力の最小値
# dp[i][j] = min(dp[i][j - a] + b, dp[i - 1][j])

dp = [[float('inf') 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):
    a, b = map(int, input().split())
    for j in range(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:
h, n = map(int, input().split())
atk = []
mp = []

for i in range(n):
  tmp_a, tmp_b = map(int, input().split())
  atk.append(tmp_a)
  mp.append(tmp_b)
  
inf = pow(2,64)
dp = [0] * (h+1)
max_atk = [0] * (h+1)
for i in range(1,h+1):
  min_dp = inf
  tmp_atk = [0] * n
  for j in range(n):
    if i <= max_atk[i-1]:
      dp[i] = dp[i-1]
      tmp_atk[j] = max_atk[i-1]
    elif i - atk[j] <= 0:
      dp[i] = mp[j]
      tmp_atk[j] = atk[j]
    else:
      dp[i] = dp[i - atk[j]] + mp[j]
      tmp_atk[j] = max_atk[i - atk[j]] + atk[j]
      
    if dp[i] < min_dp:
      min_dp = dp[i]
      ind = j
  max_atk[i] = tmp_atk[ind]
  dp[i] = min_dp
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
INF = 10**9
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 + 1)
dp[0] = 0
for i in range(n):
    for j in range(h + 1):
        m = min(h, j + a[i])
        dp[m] = min(dp[m], dp[j] + b[i])
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
a = [None] * (N + 1)
b = [None] * (N + 1)
for i in range(1, N + 1):
    a[i], b[i] = map(int, input().split())

# dp[i][j] = 魔法iまでからダメージの総和がj以上になるように選んだ時の魔力の最小値
# dp[i][j] = min(dp[i][j - a] + b, dp[i - 1][j])

dp = [[float('inf') 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 j in range(H + 1):
        dp[i][j] = min(dp[i][max(0, j - a[i])] + b[i], dp[i - 1][j])
print(dp[N][H])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
atk = []
mp = []
for i in range(n):
  tmp_a, tmp_b = map(int, input().split())
  atk.append(tmp_a)
  mp.append(tmp_b)
  
dp = [0] * (h+1)
max_atk = [0] * (h+1)
for i in range(1,h+1):
  min_dp = 10000000000000
  tmp_atk = [0] * n
  for j in range(n):
    if i <= max_atk[i-1]:
      dp[i] = dp[i-1]
      tmp_atk[j] = max_atk[i-1]
    elif i - atk[j] <= 0:
      dp[i] = mp[j]
      tmp_atk[j] = atk[j]
    else:
      dp[i] = dp[i - atk[j]] + mp[j]
      tmp_atk[j] = max_atk[i - atk[j]] + atk[j]
      
    if dp[i] < min_dp:
      min_dp = dp[i]
      ind = j
  max_atk[i] = tmp_atk[ind]
  dp[i] = min_dp
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
inf = 1 << 32
dp = [inf] * (H + 1)
dp[0] = 0
for i in range(N):
    a,b = map(int, input().split())
    for j in range(H+1):
        dp[min(H,j + a)] = min(dp[min(H,j + a)], dp[j] + b)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
atk = []
mp = []
for i in range(n):
  tmp_a, tmp_b = map(int, input().split())
  atk.append(tmp_a)
  mp.append(tmp_b)
  
dp = [0] * (h+1)
max_atk = [0] * (h+1)
for i in range(1,h+1):
  min_dp = pow(2, 64)
  tmp_atk = [0] * n
  for j in range(n):
    if i <= max_atk[i-1]:
      dp[i] = dp[i-1]
      tmp_atk[j] = max_atk[i-1]
    elif i - atk[j] > 0:
      dp[i] = dp[i - atk[j]] + mp[j]
      tmp_atk[j] = max_atk[i - atk[j]] + atk[j]
    else:
      dp[i] = dp[0] + mp[j]
      tmp_atk[j] = atk[j]
    if dp[i] < min_dp:
      min_dp = dp[i]
      ind = j
  max_atk[i] = tmp_atk[ind]
  dp[i] = min_dp
print(dp[h])