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:
h, n = map(int, input().split())
a = []
b = []
for i in range(n):
    a_tmp, b_tmp = map(int, input().split())
    a.append(a_tmp)
    b.append(b_tmp)

a_max = 10000
dp_max = 100000000
dp = [dp_max] * (h + a_max + 1)
dp[0] = 0

for i in range(h):
    if dp[i] == dp_max:
        continue
    for j in range(n):
        a_current = a[j]
        b_current = b[j]
        if dp[i] + b_current < dp[i + a_current]:
            dp[i + a_current] = dp[i] + b_current

dp_min = dp_max
for i in range(h, h + a_max + 1):
    if dp[i] < dp_min:
        dp_min = dp[i]

print(dp_min)

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
spells = sorted([tuple(map(int,input().split())) for _ in range(N)], key=lambda c: c[0])
max_a = spells[-1][0]

dp = [100000000 for _ in range(H + max_a + 1)]
dp[0] = 0

for i in range(1, H + max_a + 1):
  for spell in spells:
    if spell[0] <= i:
      dp[i] = min(dp[i], dp[i - spell[0]] + spell[1])
    else:
      break

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

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
dp = [10**10]*(h+1)
dp[0] = 0
for _ in range(n):
    a, b = map(int, input().split())
    for start in range(h):
        end = min(start+a, h)
        dp[end] = min(dp[end], dp[start]+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_tmp, b_tmp = map(int, input().split())
    a.append(a_tmp)
    b.append(b_tmp)

a_max = max(a)
dp_max = 100000000
dp = [dp_max] * (h + a_max + 1)
dp[0] = 0

for i in range(h):
    if dp[i] == dp_max:
        continue
    for j in range(n):
        a_current = a[j]
        b_current = b[j]
        if dp[i] + b_current < dp[i + a_current]:
            dp[i + a_current] = dp[i] + b_current

dp_min = dp_max
for i in range(h, h + a_max + 1):
    if dp[i] < dp_min:
        dp_min = dp[i]

print(dp_min)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = map(int,input().split())
dp = [10**10 for i in range(N+1)]
dp[0] = 0
for i in range(K):
  a,b = map(int,input().split())
  for j in range(N):
    nj = min(j + a, N)
    dp[nj] = min(dp[nj],dp[j] + b)
print(dp[N])

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
INF=10**9
A=[]; dp=[INF]*(h+10)
for i in range(n):
  x,y=map(int,input().split())
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    index=h if i+a>h else i+a
    dp[index]=dp[index]if dp[index]<dp[i]+b else dp[i]+b
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
INF=10**9
A=[]; dp=[INF]*(h+10)
for i in range(n):
  x,y=map(int,input().split())
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    if dp[i]==INF: continue
    index=h if i+a>h else i+a
    dp[index]=dp[index]if dp[index]<dp[i]+b else dp[i]+b
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
INF=10**9
A=[]; dp=[INF]*(h+10)
for i in range(n):
  x,y=map(int,input().split())
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    if dp[i]==INF: continue
    index=min(h,i+a)
    dp[index]=min(dp[index],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import math

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


dp=[[b[0]] * (h+2) for i in range(n+2)]
for j in range(h+2):
    dp[0][j]=math.ceil(j/a[0])*b[0]
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=[]; dp=[10**9]*(h+10)
for i in range(n):
  x,y=map(int,input().split())
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    if dp[i]==10**9: continue
    index=min(h,i+a)
    dp[index]=min(dp[index],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
A=[]; dp=[pow(10,9)]*(h+10)
for i in range(n):
  x,y=map(int,input().split())
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    if dp[i]==pow(10,9): continue
    index=min(h,i+a)
    dp[index]=min(dp[index],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def IN(): return map(int,input().split())
def main():
  h,n=IN()
  A=[]; dp=[pow(10,9)]*(h+10)
  for i in range(n):
    x,y=IN()
    A.append((x,y))
  dp[0]=0
  for a,b in A:
    for i in range(h):
      if dp[i]==pow(10,9): continue
      index=min(h,i+a)
      dp[index]=min(dp[index],dp[i]+b)
  print(dp[h])
main()

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
spells = sorted([tuple(map(int,input().split())) for _ in range(N)], key=lambda c: c[0])
max_a = max(spells,key=lambda t: t[0])[0]

dp = [10000000000 for _ in range(H + max_a + 1)]
dp[0] = 0

for i in range(1, H + max_a + 1):
  for spell in spells:
    if spell[0] <= i:
      dp[i] = min(dp[i], dp[i - spell[0]] + spell[1])
    else:
      break

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

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc153/tasks/abc153_e


def main():
    h, n = map(int, input().split(" "))
    dp = [float("inf")] * (h + 1)
    dp[0] = 0
    for i in range(n):
        damage, cost = map(int, input().split(" "))
        for total in range(h +1):
            # 現在の蓄積ダメージから、魔法iを使った場合に与えるダメージ
            next_damage = min(total + damage, h)
            dp[next_damage] = min(dp[next_damage], dp[total] + cost)
    print(dp[-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h,n=[int(item) for item in input().split()]
A=[]; dp=[pow(10,9)]*(h+10)
for i in range(n):
  x,y=map(int, input().split())
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    if dp[i]==pow(10,9): continue
    index=min(h,i+a)
    dp[index]=min(dp[index],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc153/tasks/abc153_e


def main():
    h, n = map(int, input().split(" "))
    d = []
    c = []
    for _ in range(n):
        damage, cost = map(int, input().split(" "))
        d.append(damage)
        c.append(cost)
    dp = [float("inf")] * (h + 1)
    dp[0] = 0
    for i in range(n):
        for total in range(h +1):
            # 現在の蓄積ダメージから、魔法iを使った場合に与えるダメージ
            damage = min(total + d[i], h)
            dp[damage] = min(dp[damage], dp[total] + c[i])
    print(dp[-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def IN(): return map(int,input().split())
h,n=IN()
A=[]; dp=[pow(10,9)]*(h+10)
for i in range(n):
  x,y=IN()
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    if dp[i]==pow(10,9): continue
    index=min(h,i+a)
    dp[index]=min(dp[index],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(H: int, N: int, A: "List[int]", B: "List[int]"):
    dp = [[INF] * (H+1) for i in range(N+1)]

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

    # for i in dp:
    #     print(i)
    print(dp[N][H])
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(H, N, A, B)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def IN(): return map(int,input().split())
h,n=IN()
A=[]; dp=[pow(2,31)]*(h+10)
for i in range(n):
  x,y=IN()
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    index=min(h,i+a)
    dp[index]=min(dp[index],dp[i]+b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
def IN(): return map(int,input().split())
ans = pow(2,31); mod = pow(10,9)+7
h,n=IN()
A=[]; dp=[pow(2,31)]*(h+9999)
for i in range(n):
  x,y=IN()
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h):
    dp[i+a]=min(dp[i+a],dp[i]+b)
for i in range(h,h+9999):
  ans=min(ans,dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
a=[0]*n
b=[0]*n

MAX_HP = 10**4
MAX_HP = h
for i in range(n):
  ta,tb = map(int,input().split())
  a[i] = ta
  b[i] = tb

INF = 10**9

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

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

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

Submission status: Time Limit Exceeded

Solution code:
s = input().rstrip().split(' ')
h = int(s[0])
n= int(s[1])

A = []
B = []

for i in range(n):
    AB = list(map(int, input().rstrip().split(' ')))
    A.append(AB[0])
    B.append(AB[1])

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

def min(a, b):
    if a < b:
        return a
    else:
        return b

for i in range(n):
    for j in range(h):
        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:
def IN(): return map(int,input().split())
ans = pow(2,31); mod = pow(10,9)+7
h,n=IN()
A=[]; dp=[pow(2,31)]*111111
for i in range(n):
  x,y=IN()
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(h+1111):
    dp[i+a]=min(dp[i+a],dp[i]+b)
for i in range(h,11111):
  ans=min(ans,dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

h,n=map(int, input().split())
items=[]
ans=float("inf")

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

dp = [[float("inf")]*(n+1) for _ in range(10**4*2)]
for i in range(n+1):
    dp[0][i]=0

for j in range(1,10**4*2):
    for i in range(1,n+1):
        if j < items[i-1][0]:
            dp[j][i] = dp[j][i-1]
        else:
            dp[j][i] = min(dp[j][i-1], dp[j-items[i-1][0]][i]+items[i-1][1])

for j in range(h,10**4*2):
    ans = min(ans, dp[j][-1])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

h,n=map(int, input().split())
items=[]
ans=float("inf")

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

dp = [[0]*(n+1)]+[[float("inf")]*(n+1) for _ in range(10**4*2-1)]
# for i in range(n+1):
#     dp[0][i]=0

for j in range(1,10**4*2):
    for i in range(1,n+1):
        if j < items[i-1][0]:
            dp[j][i] = dp[j][i-1]
        else:
            dp[j][i] = min(dp[j][i-1], dp[j-items[i-1][0]][i]+items[i-1][1])

for j in range(h,10**4*2):
    ans = min(ans, dp[j][-1])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

h,n=map(int, input().split())
items=[]
M=0
ans=float("inf")

for _ in range(n):
    a,b=map(int, input().split())
    items.append((a,b))
    M=max(M,a)

dp = [[float("inf")]*(n+1) for _ in range(h+M+1)]
for i in range(n+1):
    dp[0][i]=0

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

for j in range(h,h+M+1):
    ans = min(ans, dp[j][-1])

print(ans)

Submission status: Time Limit Exceeded

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

def main():
  def dp(i):
    for a,b in AB:
      C[i]=min(C[i],C[i-a if i>a else 0]+b)

  H,N=map(int,input().split())
  AB=[list(map(int,input().split())) for _ in range(N)]
  C=[0]+[10**8]*H

  for i in range(1,H+1):
    dp(i)

  print(C[H])

if __name__=='__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as lower_bound
def IN(): return map(int,input().split())
ans = pow(2,31); mod = pow(10,9)+7
h,n=IN()
A=[]; dp=[pow(2,31)]*111111
for i in range(n):
  x,y=IN()
  A.append((x,y))
dp[0]=0
for a,b in A:
  for i in range(101111):
    dp[i+a]=min(dp[i+a],dp[i]+b)
for i in range(h,111111):
  ans=min(ans,dp[i])
print(ans)

Submission status: Time Limit Exceeded

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

def dp(i):
  for a,b in AB:
    T=C[i-a if i>a else 0]+b
    C[i]=T if T<C[i] else C[i]

H,N=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]
C=[0]+[10**8]*H

for i in range(1,H+1):
  dp(i)

print(C[H])

Submission status: Time Limit Exceeded

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

def dp(i):
  for a,b in AB:
    C[i]=min(C[i],C[i-a if i>a else 0]+b)

H,N=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]
C=[0]+[10**8]*H

for i in range(1,H+1):
  dp(i)

print(C[H])

Submission status: Time Limit Exceeded

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

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

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

    dp[H] = 0

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

    print(dp[0])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  def dp(i):
    for a,b in AB:
      C[i]=min(C[i],C[i-a if i>a else 0]+b)

  H,N=map(int,input().split())
  AB=[list(map(int,input().split())) for _ in range(N)]
  C=[0]+[10**8]*H

  for i in range(1,H+1):
    dp(i)

  print(C[H])

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def dp(i):
  for a,b in AB:
    C[i]=min(C[i],C[i-a if i>a else 0]+b)

H,N=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N)]
C=[0]+[10**8]*H

for i in range(1,H+1):
  dp(i)

print(C[H])

Submission status: Time Limit Exceeded

Solution code:
def inpl():
    return list(map(int, input().split()))


H, N = inpl()
AB = [inpl() for i in range(N)]

# print(AB)
ans = [float("inf") for _ in range(H + 1)]
ans[0] = 0

for a, b in AB:
    # print(a, b)
    for i in range(H):
        ni = min(i + a, H)
        # print(i)
        ans[ni] = min(ans[ni], ans[i] + b)

print(ans[H])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

h,n = [int(x) for x in stdin.readline().rstrip().split()]
a = [0]*n ;b = [0]*n

for i in range(n):
    a[i],b[i] = [int(x) for x in stdin.readline().rstrip().split()]

#最高で削られる可能性がある体力
H = max(a)

inf = 10**10

#体力をちょうどi削るために必要な最低の魔力
dp = [inf for _ in range(h+H+1)];dp[0] = 0

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

print(min(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=[[1000000000]*(H+max(A)+1) for i in range(N+1)]


for i in range(N):
    DP[i+1][A[i]]=B[i]


for i in range(N):
    for h in range(H+max(A)+1):
        DP[i+1][h]=min(DP[i+1][h],DP[i][h])
        if h-A[i]>=0:
            DP[i+1][h]=min(DP[i+1][h-A[i]]+B[i],DP[i+1][h])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#

def main():
    H,N=mis()
    AB = tuple(tuple(mis()) for _ in range(N))
    DP = [INF] * (H+1)
    DP[0] = 0
    for h in range(1, H+1):
        for i in range(N):
            DP[h] = min(DP[h], (DP[h-AB[i][0]]+AB[i][1] if h-AB[i][0]>0 else AB[i][1]))

    print(DP[-1])

main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

h,n = [int(x) for x in stdin.readline().rstrip().split()]
a = [0]*n ;b = [0]*n

for i in range(n):
    a[i],b[i] = [int(x) for x in stdin.readline().rstrip().split()]

#最高で削られる可能性がある体力
H = max(a)

inf = 10**10

#体力をちょうどi削るために必要な最低の魔力
dp = [inf for _ in range(h+H+1)];dp[0] = 0

for i,j in zip(a,b):
    for k in range(h+1):
        if dp[k+i] > dp[k] + j:
            dp[k+i] = dp[k] + j

print(min(dp[h:]))

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 = [sys.maxsize//2]*H

dp[0] = 0
ans = sys.maxsize
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:
INF = 1001001001
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
dp = [INF] * (20000)
dp[0] = 0
for a,b in ab:
    for j in range(H):
        if dp[j+a] > dp[j] + b:
            dp[j+a] = dp[j] + b
print(min(dp[H:]))

Submission status: Time Limit Exceeded

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

INF = 10**20
dp = [0]
for i in range(1, H + 1):
    min_mp_cost = INF
    for damage, mp_cost in AB:
        parent = max(0, i - damage)
        new_cost = dp[parent] + mp_cost
        if new_cost < min_mp_cost:
            min_mp_cost = new_cost
    dp.append(min_mp_cost)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**5)

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
memo = [10**10]*(H+1)
memo[0] = 0
i = 0
while i <= H:
  for a, b in AB:
    memo[i] = min(memo[i], b+memo[max(0, i-a)])
  i += 1
print(memo[H])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

INF = 10**20
dp = [0]
for i in range(1, H + 1):
    min_mp_cost = INF
    for damage, mp_cost in AB:
        parent = max(0, i - damage)
        new_cost = dp[parent] + mp_cost
        if new_cost < min_mp_cost:
            min_mp_cost = new_cost
    dp.append(min_mp_cost)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

H, N = map(int, input().split())
AB = []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB = sorted(AB, key=lambda x: (x[0], x[1]))

INF = 10**20
dp = [INF for i in range(H + 1)]
dp[0] = 0
for i in range(1, H + 1):
    if dp[i] != INF:
        continue
    min_mp_cost = INF
    for damage, mp_cost in AB:
        parent = max(0, i - damage)
        new_cost = dp[parent] + mp_cost
        if new_cost < min_mp_cost:
            min_mp_cost = new_cost
    dp[i] = min_mp_cost
print(dp[-1])

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 = [float('inf')] * (H + 10 ** 4)

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

Submission status: Time Limit Exceeded

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

dp = [float("inf") for _ in range(h+m)]
dp[0] = 0
for i in range(1,h+m):
    for j in range(n):
        if i-aa[j] >= 0:
            dp[i] = min(dp[i-aa[j]]+bb[j],dp[i])

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

dp = [float('inf')] * (H + 10 ** 4)

dp[0] = 0
for j in range(H + 10 ** 4):
    for i in range(N):
        if j - A[i] < 0:
            continue
        dp[j] = min(dp[j], dp[j - A[i]] + B[i])
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
INF = 1001001001
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
dp = [INF] * (H+1)
dp[0] = 0
for i in range(N):
    for j in range(H+1):
        if j>ab[i][0]:
            dp[j] = min(dp[j], dp[j-ab[i][0]] + ab[i][1])
        else:
            dp[j] = min(dp[j], ab[i][1])
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

h, n = map(int, input().split())
ABs = [list(map(int, input().split())) for i  in range(n)] 

#  dp[i][h] 最初の i 種類の魔法でモンスターに h のダメージを与える場合の、消費魔力の最小値
dp = [[10**9]*(h+1)]*(n+1)
dp[0][0] = 0

for i in range(n):
    for j in range(h):
        a = ABs[i][0]
        b = ABs[i][1]
        # i種類目の魔法を使わない場合
        dp[i+1][j] = min(dp[i][j], dp[i+1][j])

        # i種類目の魔法を使う場合
        #min(j+A[i], H) -> hより大きいダメージを与える場合は、与えるダメージをhとして計算する。
        # dp[i+1][j] + b -> i+1種類目の魔法を使った場合の消費魔力
        dp[i+1][min([j+a,h])] = min([ dp[i+1][j] + b, dp[i+1][min([j+a,h])] ])

print(min([row[j+1] for row in dp]))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**5)

def solve(ab, hp, memo):
  if hp <= 0:
    return 0
  else:
    ans = 10**10
    if memo[hp] < 0:
      for attack, cost in ab:
        ans = min(ans, solve(ab, hp-attack, memo)+cost)
      memo[hp] = ans
    return memo[hp]

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
memo = [-1]*(H+1)
i = 0
while i <= H:
  solve(AB, i, memo)
  i += 1
print(memo[H])

Submission status: Time Limit Exceeded

Solution code:
h,n,*ab=map(int,open(0).read().split())
ans_list = [0] + [float('inf')]*h
for i in range(1,h+1):
    for a,b in zip(ab[::2],ab[1::2]):
        if a > i:
            ans_list[i] = min(ans_list[i], b)
        else:
            ans_list[i] = min(ans_list[i],ans_list[i-a]+b)
print(ans_list[h])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#

def main():
    H,N=mis()
    AB = [lmis() for _ in range(N)]
    @lru_cache(maxsize=None)
    def rec(h):
        if h <= 0:
            return 0
        return min(rec(h-a)+b for a, b in AB)
    print(rec(H))

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**5)

def solve(ab, hp, memo):
  if hp <= 0:
    return 0
  else:
    ans = 10**10
    if not hp in memo:
      for attack, cost in ab:
        ans = min(ans, solve(ab, hp-attack, memo)+cost)
      memo[hp] = ans
    return memo[hp]

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]
memo = {}
for i in range(H+1):
  solve(AB, i, memo)
print(memo[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)

dp = [float("inf") for _ in range(H + 1)]
dp[0] = 0

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)

H, N = map(int, input().split())
AB = []
for _ in range(N):
    a, b = map(int, input().split())
    AB.append((a, b))
AB = sorted(AB, key=lambda x: (x[0], x[1]))

INF = 10**20
dp = [INF for i in range(H + 1)]
dp[0] = 0
for i in range(1, H + 1):
    if dp[i] != INF:
        continue
    min_mp_cost = INF
    for damage, mp_cost in AB:
        parent = i - damage
        if parent <= 0:
            new_cost = mp_cost
        else:
            new_cost = dp[parent] + mp_cost
        if new_cost < min_mp_cost:
            min_mp_cost = new_cost
    dp[i] = min_mp_cost
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import functools


def inpl():
    return list(map(int, input().split()))


@functools.lru_cache(maxsize=None)
def solve(h):
    if h <= 0:
        return 0

    ret_val = float("inf")
    for a, b in AB:
        ret_val = min(ret_val, b + solve(h - a))
    return ret_val


sys.setrecursionlimit(100000)

H, N = inpl()
AB = [inpl() for i in range(N)]
AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
# print(AB)
print(solve(H))

Submission status: Time Limit Exceeded

Solution code:
import math

h,n=map(int,input().split())
arr=[]
brr=[]
for i in range(n):
	a,b=map(int,input().split())
	arr.append(a)
	brr.append(b)
dp=[1000000000000000]*(h+1)
dp[0]=0
for i in range(1,h+1):
	for j in range(n):
		if ((i-arr[j])>=0):
			dp[i]=min(dp[i],dp[i-arr[j]]+brr[j])
		else:
			dp[i]=min(dp[i],brr[j])
print(dp[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)

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, N=list(map(int, input().split()))
A, B = [], []
for n in range(N):
    temp=list(map(int, input().split()))
    A.append(temp[0]), B.append(temp[1])

dp = [ [np.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):
        if dp[i+1][j]>dp[i][j]:
            dp[i+1][j] = dp[i][j]
        if dp[i+1][min(j+A[i], H)] > dp[i+1][j]+B[i]:
            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())
l = []
k = 0
for i in range(n):
    a,b = map(int,input().split())
    l.append([a,b])
    k += a
ans = 0
i = 0
dp = [0 for i in range(h+1)]
for i in range(n):
    a,b = l[i]
    for j in range(h):
        if j+a <= h:
            if j == 0:
                if dp[j+a] == 0:
                    dp[j+a] = b
                else:
                    dp[j+a] = min(b,dp[j+a])
            else:
                if dp[j] != 0:
                    if dp[j+a] == 0:
                        dp[j+a] = dp[j] + b
                    else:
                        dp[j+a] = min(dp[j]+b,dp[j+a])
        else:
            if dp[j] != 0:
                if dp[h] == 0:
                    dp[h] = dp[j]+b
                else:
                    dp[h] = min(dp[h],dp[j]+b)
            else:
                if j == 0:
                    if dp[h] == 0:
                        dp[h] = dp[j]+b
                    else:
                        dp[h] = min(dp[h],dp[j]+b)
print(dp[h])

Submission status: Time Limit Exceeded

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

def main():
  h,n = map(int,input().split())

  dg = 10**5

  ba = []
  for _ in [0]*n:
      a,b = map(int,input().split())
      ba.append(b*dg+a)

  d = [10**9]*(h+1)

  d[0] = 0

  for i in range(1,h+1):
      for e in ba:
          a,b = e%dg,e//dg
          if i<=a:
              if d[i] > b:
                  d[i] = b
          else:
              if d[i] > d[i-a]+b:
                  d[i] = d[i-a]+b

  print(d[-1])
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

dg = 10**5

ba = []
for _ in [0]*n:
    a,b = map(int,input().split())
    if a >= h:
        a = h
    ba.append(b*dg+a)
ba.sort()

d = [10**9]*(h+1)

d[0] = 0

for i in range(1,h+1):
    for e in ba:
        a,b = e%dg,e//dg
        if i<=a:
            if d[i] > b:
                d[i] = b
        else:
            if d[i] > d[i-a]+b:
                d[i] = d[i-a]+b

print(d[-1])

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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 = [np.inf for i in range(10**7+1)]
dp[0] = 0
for i in range(H+1):
    for j in range(N):
        dp[i+A[j]] = min(dp[i+A[j]],dp[i]+B[j])
        
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
import math


def inputIntList():
  return [int(s) for s in input().split()]


def inputInt():
  return int(input())


inf = float('inf')


def main():
  H, N = inputIntList()
  AB = [inputIntList() for _ in range(N)]
  dp = [inf for _ in range(H + 1)]

  dp[0] = 0
  for (a, m) in AB:
    for h in range(H):
      nxt = min(H, h+a)
      dp[nxt] = min(dp[h]+m, dp[nxt])
  # print(dp)
  return dp[H]


if __name__ == "__main__":
  print(main())