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:
def calc():
  h,n=map(int,input().split())
  arr=[]
  for _ in range(n):
    arr.append(list(map(int,input().split())))
  dp=[10**10]*20001
  dp[0]=0
  for i in range(20001):
    for j in range(n):
      if i+arr[j][0]>20000:
        continue
      dp[i+arr[j][0]]=min(dp[i+arr[j][0]],dp[i]+arr[j][1])
  ans=10**18
  for i in range(h,20001):
    ans=min(ans,dp[i])
  print(ans)
calc()

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
MAX_NUM=9999999999
ab=[]
for _ in range(n):
    ab.append(list(map(int,input().split())))
dp=[MAX_NUM for _ in range(h+1)]
dp[0]=0

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

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
MAX_NUM=9999999999
ab=[]
for _ in range(n):
    ab.append(list(map(int,input().split())))
dp=[MAX_NUM for _ in range(h+1)]
dp[0]=0

for i in range(h):
    if(dp[i]==MAX_NUM):
        continue

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

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
MAX_NUM=9999999999
ab=[]
for _ in range(n):
    ab.append(list(map(int,input().split())))
dp=[MAX_NUM for _ in range(h+1)]
dp[0]=0

for i in range(h):
    if(dp[i]==MAX_NUM):
        continue

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

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
ab=[]
for _ in range(n):
    ab.append(list(map(int,input().split())))
dp=[9999999999 for _ in range(h+1)]
dp[0]=0

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
#a = int(input())
# スペース区切りの整数の入力
#b, c = map(int, input().split())
# 文字列の入力
#s = input()
# 出力
#print("{} {}".format(a+b+c, s))

import numpy as np

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

ab=[[0]*2 for i in range(n)]
maxa=0

for i in range(0,n):
  ab[i][0], ab[i][1] = map(int, input().split())
  if maxa<ab[i][0]:
    maxa=ab[i][0]

sab=sorted(ab,key=lambda x:x[0]*10**5-x[1])

ans=[0]*(h+maxa)
maxl=2*10**8

for i in range(1,h+maxa):
  ans[i]=maxl
  for j in range(0,n):
    if i<sab[j][0]:
      break
    ans[i] = min(ans[i], ans[i-sab[j][0]]+sab[j][1])
print(min(ans[h:h+maxa]))

Submission status: Time Limit Exceeded

Solution code:
H,N = [int(zz) for zz in input().split()]
mahou = [[int(zz) for zz in input().split()] for _ in range(N)]

dmg = [i[0] for i in mahou]
mp = [i[1] for i in mahou]

dp = [[0]*(H+1) for _ in range(N)]
for j in range(1,H+1):
    if j <= dmg[0]:
        dp[0][j] = mp[0]
    else:
        dp[0][j] = dp[0][j-dmg[0]]+mp[0]

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from sys import stdin
from copy import deepcopy
from functools import reduce
from fractions import gcd
import math
import itertools
from collections import Counter
from itertools import chain  ##list(chain.from_iterable(l)) ##flatten
from heapq import heappush, heappop

"""
N = int(input())
A, B = map(int,input().split())
A = list(map(int,input().split())) ## 横一列
A = [int(input()) for _ in range(N)] ## 縦一列
lst = [list(map(int,input().split())) for i in range(M)] ## 二次元
"""

## 計算量: H*N

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

inf = float("inf")
dp = [inf for i in range(H+1)] ## i: 体力
dp[0] = 0

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

Submission status: Time Limit Exceeded

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def ifn(): return float(input()) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def fmn(): return map(float, input().split()) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD

H,N = imn()
AB = [iln() for _ in range(N)]

DP = [10 ** 10 for _ in range(10**4*2)]
DP[0] = 0
for i in range(N):
    for j in range(10**4*2):
        if j - AB[i][0] >= 0:
            DP[j] = min(DP[j], DP[j-AB[i][0]] + AB[i][1])
ans = 10**10
for i in range(H, 10**4*2):
    ans = min(ans, DP[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m=list(map(int,input().split()))
li=[list(map(int,input().split())) for _ in range(m)]

ans=[0 for _ in range(10**7+1)]
li=sorted(li,key=lambda x:x[1])
result=0
for i in range(li[0][1],len(ans)):
  max_=0
  for j in range(len(li)):
    if li[j][1]>i:
      break
    if max_<li[j][0]+ans[i-li[j][1]]:
      max_=li[j][0]+ans[i-li[j][1]]
  ans[i]=max_
  if max_>=n:
    result=i
    break
print(result)

Submission status: Time Limit Exceeded

Solution code:
n,m=list(map(int,input().split()))
li=[list(map(int,input().split())) for _ in range(m)]

ans=[0 for _ in range(10**8+1)]
li=sorted(li,key=lambda x:x[1])
result=0
for i in range(li[0][1],len(ans)):
  max_=0
  for j in range(len(li)):
    if li[j][1]>i:
      break
    if max_<li[j][0]+ans[i-li[j][1]]:
      max_=li[j][0]+ans[i-li[j][1]]
  ans[i]=max_
  if max_>=n:
    result=i
    print(i)
    break
print(result)

Submission status: Time Limit Exceeded

Solution code:
import math
H, N = list(map(int, input().split()))
 
AB = [list(map(int, input().split())) for _ in range(N)]
 
dp = [float('inf')] *(H+1)
dp[H] = 0
for a,b in AB:
    for i in range(H,-1,-1):
        if i <= H - a:
            dp[i] = min(dp[i], dp[i+a] + b)
    dp[0] = min(dp[0], min(dp[:a+1]) + b) # if hp is not just 0
print(dp[0])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import sys

def main():
    h, n = [int(x) for x in sys.stdin.readline().split()]
    ms = [[int(x) for x in sys.stdin.readline().split()] for i in range(n)]
    atks, mps = zip(*ms)

    # dp[(敵のHP)] = そのHPを倒せるMPの最小値
    dp = [2 ** 31 for i in range(h + 1)]

    # Fill dp[] using above recursive formula
    for i in range(h + 1):
        for m in range(n):
            if (atks[m] >= i):
                dp[i] = min(dp[i], mps[m])
            else:
                dp[i] = min(dp[i], dp[i - atks[m]] + mps[m])

    print(dp[h])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(1,h+1):
    magic = 10 ** 10
    for j in range(n):
        magic = min(DP[max(0, i-A[j])] + B[j], magic)
    DP[i] = magic

print(DP[-1])

Submission status: Time Limit Exceeded

Solution code:
import math
H, N = list(map(int, input().split()))
 
AB = [list(map(int, input().split())) for _ in range(N)]
 
dp = [float('inf')] *(H+1)
dp[H] = 0
for a,b in AB:
    for i in range(H,-1,-1):
        if i <= H - a:
            dp[i] = min(dp[i], dp[i+a] + b)
        dp[0] = min(dp[0], min(dp[:a+1]) + b) # if hp is not just 0
print(dp[0])

Submission status: Time Limit Exceeded

Solution code:

import math
H, N = list(map(int, input().split()))

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

dp = [float('inf')] *(H+1)
dp[H] = 0
for a,b in AB:
    for i in range(H,-1,-1):
        if i <= H - a:
            dp[i] = min(dp[i], dp[i+a] + b)
        dp[0] = min(dp[0], min(dp[:a+1]) + b) # if hp is not just 0

print(dp[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import fractions
import bisect
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''


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


dp = [[INF] * (H+10) for i in range(N)] # 魔法iまでを使って体力をj減らすのに必要な最小のコスト

for hp in range(H+10):
    dp[0][hp] = ((hp//A[0]) + int(hp%A[0] != 0))*B[0]

for magic in range(1, N):
    for hp in range(H+10):

        if hp-A[magic] >= 0:
            dp[magic][hp] = min(dp[magic-1][hp], dp[magic][hp-A[magic]] + B[magic])
        else:
            dp[magic][hp] = min(dp[magic-1][hp], B[magic])


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

Submission status: Time Limit Exceeded

Solution code:
h,n,*L=map(int,open(0).read().split())
dp=[0]+[9e9]*h
ov=9e9
for a,b in zip(*[iter(L)]*2):
	for i in range(h):
		if a+i>h:
			ov=min(min(dp[i:])+b,ov)
			break
		t=dp[i]+b
		if t<dp[i+a]:
			dp[i+a]=t
print(min(ov,dp[h]))

Submission status: Time Limit Exceeded

Solution code:
h,n,*L=map(int,open(0).read().split())
dp=[0]+[9e9]*h
for a,b in zip(*[iter(L)]*2):
	for i in range(h):
		dp[min(i+a,h)]=min(dp[min(i+a,h)],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h,n,*L=map(int,open(0).read().split())
dp=[float('inf')]*(h+10100)
dp[0]=0
for a,b in zip(*[iter(L)]*2):
    for i in range(h):
        t=dp[i]+b
        if t<dp[i+a]:
            dp[i+a]=dp[i]+b
print(min(dp[h:]))

Submission status: Time Limit Exceeded

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

Magic = [list(map(int, input().split())) for i in range(N)]
MAX_COST = max(Magic)[1]

# dp[damage] := モンスターに damage を与えるために必要な最小コスト
dp = [float("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[-1])

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 = [pow(10, 10)] * (h + 1)
dp[0] = 0

for k in range(n):  # check magic
    for j in range(h):  # check dp
        next_index = min(j + a[k], h)
        dp[next_index] = min(dp[next_index], dp[j] + b[k])

print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
spells = [list(map(int, input().split())) for _ in range(n)]
INF = 10 ** 9
dp = [INF] * (h + 1)
dp[0] = 0

for i in range(h):
    for a, b in spells:
        ni = min(i + a, h)
        dp[ni] = min(dp[ni], dp[i] + b)

print(dp[h])

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 = [pow(10, 10)] * (h + 1)
dp[0] = 0

for k in range(n):
    for j in range(h):
        if j + a[k] <= h and dp[j + a[k]] > dp[j] + b[k]:
            dp[j + a[k]] = dp[j] + b[k]
        elif j + a[k] > h:
            if dp[h] > dp[j] + b[k]:
                dp[h] = dp[j] + b[k]

print(dp[h])

Submission status: Time Limit Exceeded

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

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

A, B = zip(*AB)

dp = [-1 for _ in range(H+max(A))]
dp[0] = 0

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

print(dp[H])

Submission status: Time Limit Exceeded

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

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

A, B = zip(*AB)

dp = [-1 for _ in range(H+max(A))]
dp[0] = 0

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

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(reverse=True)
dp = [10**10] * (10**4+5)
dp[0] = 0
# ぴったり倒せるときだけ更新して、答えを出力するときに最小値を出力する
for i in range(len(dp)):
    for a, b in AB:
        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 = [list(map(int, input().split())) for _ in range(N)]
AB.sort(reverse=True)
dp = [10**10] * (10**5)
dp[0] = 0
# ぴったり倒せるときだけ更新して、答えを出力するときに最小値を出力する
for i in range(len(dp)):
    for a, b in AB:
        dp[i] = min(dp[i], dp[i-a]+b)

print(min(dp[H:]))

Submission status: Time Limit Exceeded

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

dp = [10000000000000 for i in range(w+1)]

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

print(dp[w])

Submission status: Time Limit Exceeded

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

for i in range(N):	
  ATK.append(list(map(int, input().split())))
mp_min = [0]
for hp in range(1, H+1):
  mp_min.append(min([mp_min[max(0, hp-ATK[i][0])] + ATK[i][1] for i in range(N)]))

 # print(mp_min)

print(mp_min[-1])

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, M = map(int, input().split())
    A = []
    B = []
    INF = 10 ** 18
    dp = [INF] * (N + 20000)
    dp[0] = 0
    for _ in range(M):
        a, b = list(map(int, input().split()))
        for i in range(N + 10000):
            if dp[i] == INF:
                continue
            dp[i+a] = min(dp[i+a], dp[i] + b)
        
    return min(dp[N:])

print(solve())

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, M = map(int, input().split())
    A = []
    B = []
    dp = [float('inf')] * (N + 20000)
    dp[0] = 0
    for _ in range(M):
        a, b = list(map(int, input().split()))
        for i in range(N + 10000):
            if dp[i] == 'inf':
                continue
            dp[i+a] = min(dp[i+a], dp[i] + b)
        
    return min(dp[N:])

print(solve())

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, M = map(int, input().split())
    A = []
    B = []
    dp = [float('inf')] * (N + 30000)
    dp[0] = 0
    for _ in range(M):
        a, b = list(map(int, input().split()))
        for i in range(N + 10000):
            if dp[i] == 'inf':
                continue
            dp[i+a] = min(dp[i+a], dp[i] + b)
        
    return min(dp[N:])

print(solve())

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
ATK = []
dmg_max = []
mp = 0
for i in range(N):	
  ATK.append(list(map(int, input().split())))

while True:
  dmg_max.append(max([dmg_max[mp-ATK[i][1]] + ATK[i][0] for i in range(N) if mp - ATK[i][1] >= 0] + [0]))
  if dmg_max[-1] >= H:
    break
  mp += 1
  #print(dmg_max)

print(mp)

Submission status: Time Limit Exceeded

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

dp = [2e9] * (H+1)
dp[0] = 0

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

INF = sys.maxsize
MAX = max(x[0] for x in MAGICS)
DP = [INF] * (H+1)

DP[0] = 0

for h in range(H+1):
    for (damage, mp) in MAGICS:
        DP[h] = min(DP[max(h - damage, 0)] + mp, DP[h])

print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

INF = 1e9
MAX = max(x[0] for x in MAGICS)
DP = [INF for _ in range(H+1)]

DP[0] = 0

for h in range(H+1):
    for magic  in MAGICS:
        DP[h] = min(DP[max(h - magic[0], 0)] + magic[1], DP[h])

print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

INF = sys.maxsize
MAX = max(x[0] for x in MAGICS)
DP = [INF for _ in range(H+1)]

DP[0] = 0

for h in range(H+1):
    for (damage, mp)  in MAGICS:
        DP[h] = min(DP[max(h - damage, 0)] + mp, DP[h])

print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

INF = sys.maxsize
MAX = max(x[0] for x in MAGICS)
DP = [INF for _ in range(H+1)]

DP[0] = 0

for h in range(H+1):
    for j, magic in enumerate(MAGICS):
        (damage, mp) = magic
        DP[h] = min(DP[max(h - damage, 0)]+mp, DP[h])

print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

INF = sys.maxsize
MAX = max(x[0] for x in MAGICS)
DP = [INF for _ in range(H+MAX+1)]

DP[0] = 0

for h in range(H+1):
    for j, magic in enumerate(MAGICS):
        (damage, mp) = magic
        DP[h + damage] = min(DP[h + damage], DP[h] + mp)

for i in reversed(range(len(DP))):
    DP[i-1] = min(DP[i], DP[i-1])

print(DP[H])

Submission status: Time Limit Exceeded

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

INF = 10**18
dp = [INF]*(h+10001)

dp[0] = 0

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

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
AB = []
for _ in range(N):
    AB.append(tuple(map(int, input().split())))
max_a = max(ab[0] for ab in AB)
table = [0] + [float('inf')] * (H + max_a)

for i in range(N):
    for j in range(AB[i][0], H + max_a):
        table[j] = min(table[j], table[j - AB[i][0]] + AB[i][1])
print(min(table[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 = [10000000000] * 10001
dp[0] = 0
for i in range(N):
    for j in range(10001):
        next = min(j + A[i], H)
        dp[next] = min(dp[next], dp[j] + B[i])
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.update({i : a})
    B.update({i : b})
dp = dict(zip(range(10001), [10000000000] * 10001))
dp.update({0 : 0})
for i in range(N):
    for j in range(10001):
        next = min(j + A[i], H)
        dp.update({next : (min(dp[next], dp[j] + B[i]))})
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
dp = [int(1e18)] * (h+1)
dp[0] = 0
for i in range(n):
    a, b = map(int, input().split())
    for j in range(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 numpy as np
h,n = map(int,input().split())
dp = [np.inf] * (h+1)
dp[0] = 0
for i in range(n):
    a, b = map(int, input().split())
    for j in range(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 numpy as np
h,n = map(int,input().split())
magic = np.zeros([n+1,2],int)
for i in range(n):
    a,b = map(int,input().split())
    magic[i][0] = a
    magic[i][1] = b
magic[n] = [0,0]
max_hit = max(magic[:,0])
dp = [[1e18,1e18]] * (h+max_hit)
dp[0] = [0,0]
for i in range(1,len(dp)):
    if dp[i - 1][0] >= i:
        dp[i] = dp[i - 1]
    else:
        for j in range(i):
            for k in range(n+1):
                if dp[i-j-1][1] + magic[k][1] < dp[i][1] and i <= dp[i-j-1][0] + magic[k][0]:
                    dp[i] = [dp[i-j-1][0] + magic[k][0], dp[i-j-1][1] + magic[k][1]]
dp_ans = dp[h::]
ans = 1e18
for i in range(len(dp_ans)):
    ans = min(ans,dp_ans[i][1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
f_i = stdin

H, N = map(int, f_i.readline().split())

dp = [10 ** 8] * (H + 10 ** 4)
dp[0] = 0

for i in range(N):
    A, B = map(int, f_i.readline().split())
    for j, t in enumerate(zip(dp, dp[A:H+A]), start=A):
        dp_i, dp_j = t
        if dp_i + B < dp_j:
            dp[j] = dp_i + B

print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
f_i = stdin

H, N = map(int, f_i.readline().split())

dp = [10 ** 8] * (H + 10 ** 4)
dp[0] = 0

for i in range(N):
    A, B = map(int, f_i.readline().split())
    for i, t in enumerate(zip(dp, dp[A:H+A]), start=A):
        dp_i, dp_j = t
        dp[i] = min(dp_i + B, dp_j)

print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
H,N = list(map(int, input().split()))
Magic = [list(map(int, input().split())) for i in range(N)]
MAX_COST = max(Magic)[1]
 
# dp[damage] := モンスターに damage を与えるために必要な最小コスト
dp = [float('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[-1])

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):
        for a, b in ab:
            j = i+a
            if j > h:
                j = h
            dp[j] = min(dp[j], dp[i]+b)
    print(dp[-1])


main()

Submission status: Time Limit Exceeded

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

Ai = [0] * N
Bi = [0] * N

for x in range(N):
    Ai[x], Bi[x] = map(int, input().split())

max_ll = (H + max(Ai) + 1)
dp = [0] * max_ll

for i in range(1, max_ll):
    for j in range(N):
        idx = max(0,i-Ai[j])
        if dp[i] == 0:
            dp[i] = dp[idx] + Bi[j]
        else:
            dp[i] = min(dp[i], dp[idx]+Bi[j])

ans = dp[H]
for i in range(H, max_ll):
    ans = min(ans, dp[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, atan, degrees
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
import heapq

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, N = MAP()
A = [0]*N
B = [0]*N

for i in range(N):
    A[i], B[i] = MAP()

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

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

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

Submission status: Time Limit Exceeded

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

dp = np.zeros([N+1, H+1])
dp[0, 1:] = np.inf

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

Submission status: Time Limit Exceeded

Solution code:
def examA():
    H,A = LI()
    ans = (H-1)//A+1
    print(ans)
    return

def examB():
    H, N = LI()
    A = LI()
    if H>sum(A):
        print("No")
    else:
        print("Yes")
    return

def examC():
    N, K = LI()
    H = LI()
    H.sort()
    ans = sum(H[:max(0,N-K)])
    print(ans)
    return

def examD():
    H = I()
    ans = 2**(H.bit_length())-1
    print(ans)
    return

def examE():
    H, N = LI()
    A = [0]*N; B = [0]*N
    for i in range(N):
        A[i],B[i] = LI()
    dp = [inf]*(H+1)
    dp[0] = 0
    ans = inf
    for j in range(N):
        a,b = A[j],B[j]
        for i in range(H+1-a):
            dp[i + a] = min(dp[i + a], dp[i] + b)
        for i in range(min(a + 1, H + 1)):
            dp[H] = min(dp[H], dp[H-i] + b)
    ans = dp[H]
    print(ans)
    return

def examF():
    class segment_():
        def __init__(self, A, n, segfunc):
            #####単位元######要設定0or1orinf
            self.ide_ele = 0
            ####################
            self.num = 1 << (n - 1).bit_length()
            self.seg = [self.ide_ele] * 2 * self.num
            self.segfunc = segfunc
            # set_val
            for i in range(n):
                self.seg[i + self.num] = A[i]
                # built
            for i in range(self.num - 1, 0, -1):
                self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1])

        def update(self, k, r):
            k += self.num
            self.seg[k] = r
            while k:
                k >>= 1
                self.seg[k] = self.segfunc(self.seg[k * 2], self.seg[k * 2 + 1])

        # 値xに1加算
        def update_add(self, k,u):
            k += self.num
            self.seg[k] += u
            while k:
                k >>= 1
                self.seg[k] = self.segfunc(self.seg[k * 2], self.seg[k * 2 + 1])

        def updateneg1(self, k):
            k += self.num
            self.seg[k] -= 1
            while k:
                k >>= 1
                self.seg[k] = self.segfunc(self.seg[k * 2], self.seg[k * 2 + 1])

        def query(self, p, q):
            if q < p:
                return self.ide_ele
            p += self.num;
            q += self.num
            res = self.ide_ele
            while p < q:
                if p & 1 == 1:
                    res = self.segfunc(res, self.seg[p])
                    p += 1
                if q & 1 == 1:
                    q -= 1
                    res = self.segfunc(res, self.seg[q])
                p >>= 1;
                q >>= 1
            return res
    N, D, A = LI()
    XH = [LI() for _ in range(N)]
    XH.sort(key = lambda x:x[0])
    X = [0]*N
    for i in range(N):
        X[i] = XH[i][0]
    B = [0] * (N + 1)
    S = segment_(B,(N+1),lambda a, b: a+b)
    ans = 0
    for i in range(N):
        cur = (XH[i][1]-1)//A +1
        left = bisect.bisect_left(X,X[i]-D*2)
#        print(now)
        cur -= min(S.query(left,i),cur)
        ans +=cur
        S.update_add(i,cur)
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examE()

"""

"""

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 = [0]*(H+max(A)+1)
dp[0] = 0
for i in range(1, H+max(A)+1):
    for j in range(N):
        t = max(0,i-A[j])
        if dp[i]==0:
            dp[i] = dp[t] + B[j]
        else:
            dp[i] = min(dp[i], dp[t]+B[j])

ans = dp[H]
for i in range(H, H+max(A)+1):
    ans = min(ans, dp[i])
print(ans)

Submission status: Time Limit Exceeded

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


max_magic = 0
for a, _ in magic:
    max_magic = max(max_magic, a)

amount = H + 1
dp = [0] * (amount + max_magic)
dp[0] = 0

ret = float('inf')

for amt in range(1, amount + max_magic):
    t = [dp[amt - a] + b if amt - a >= 0 else float('inf') for a, b in magic]
    dp[amt] = min(t)

    if amt >= H:
        ret = min(dp[amt], ret)

print(ret)

Submission status: Time Limit Exceeded

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


max_magic = 0
for a, _ in magic:
    max_magic = max(max_magic, a)

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

ret = float('inf')

for amt in range(1, amount + max_magic):
    t = [dp[amt - a] + b if amt - a >= 0 else float('inf') for a, b in magic]
    dp[amt] = min(t)

    if amt >= H:
        ret = min(dp[amt], ret)

print(ret)