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: Wrong Answer

Solution code:
h, n = map(int, input().split())
inf = 10**12
data = []
max_a = 0
for i in range(n):
    data.append(list(map(int, input().split())))
    max_a = max(max_a, data[i][0])
dp = [[inf for i in range(h+1 + max_a)] for j in range(n+1)]
dp[0][0] = 0
for i in range(n):
    a, b = data[i]
    for j in range(h):
        if j - a >= 0:
            dp[i+1][j] = min(dp[i+1][j-a] + b, dp[i][j])
        else:
            dp[i+1][j] = b
print(dp[-1][h-1])

Submission status: Wrong Answer

Solution code:
H, N = map(int, input().split())
A, B = [], []
ans = 0
max_eff = 0
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    max_eff = max(max_eff, a/b)
    if max_eff == a/b:
        max_i =  i

if int(H/max_eff) == H/max_eff:
    print(int(H//max_eff))
else:
    print(int(H//max_eff)+1)

Submission status: Wrong Answer

Solution code:
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
A = [AB[i][0] for i in range(N)]
B = [AB[i][1] for i in range(N)]
dp = [10**9]*(2*10**4+1)

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

ans = 10**9
for i in range(H,2*H+1):
    ans = min(ans,dp[i])

print(ans)

Submission status: Wrong Answer

Solution code:
h, n = map(int, input().split())
m = [list(map(int, input().split())) for _ in range(n)]
INF = 10 ** 9
dp = [INF for _ in range(h + 1)]
dp[0] = 0
for a, b in m:
    for i in range(h + 1):
        if i + a > h:
            dp[h] = min(dp[h], dp[i] + b)
            break
        else:
            dp[i + a] = min(dp[i + a], dp[i] + b)
print(dp[h])

Submission status: Wrong Answer

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

INF = 1000000
dp = [INF] * (H+1 + 10000)
dp[H] = 0
for h in range(H-1, -10000, -1):
    tmp = INF
    for a, b in L:
        if h + a <= H:
            c = dp[h+a] + b
            tmp = tmp if tmp < c else c
    dp[h] = tmp

print(min(dp[0], min(dp[H+1:])))

Submission status: Wrong Answer

Solution code:
h,n=map(int,input().split())
a=[]
b=[]
for i in range(n):
  aa,bb=map(int,input().split())
  a.append(aa)
  b.append(bb)
inf=10**10
f=h+max(a)+1
dp=[f*[inf]for _ in range(n+1)]
dp[0][0]=0
for i in range(1,n+1):
  dp[i][0]=0
  for j in range(h):
    dp[i][j+a[i-1]]=min(dp[i-1][j+a[i-1]],dp[i][j]+b[i-1])
print(min(dp[-1][h:]))

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 9 + 7
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):
        if dp[j] == INF: continue
        if j + ab[i][0] <= H:
            dp[j + ab[i][0]] = min(dp[j + ab[i][0]], dp[j] + ab[i][1])
        else:
            dp[H] = min(dp[H], dp[j] + ab[i][1])

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int, input().split())
ab = []
amax = 10**5
for _ in range(n):
  ab.append(tuple(map(int,input().split())))
  amax = max(amax,ab[-1][1])
p = [10**10]*(h+amax+1)
q = [(i,j) for i,j in ab]
while q:
  a,b = q.pop()
  if b<p[a]: p[a]=b
  else: b=p[a]
  if a>=h: continue
  for i,j in ab:
    if b+j<p[a+i]:
      p[a+i]=b+j
      q.append((a+i,b+j))
print(min(p[h:]))

Submission status: Time Limit Exceeded

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

m = 10**4 + 100

dp = [float("INF")] * m

dp[0] = 0

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

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

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

for i in range(h):
  for d,c in ab:
    hp = min(i+d,h)
    dp[hp] = min(dp[hp],dp[i]+c)

print(dp[-1])

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 = map(list,zip(*ab))

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

for i in range(h):
  for d,c in ab:
    hp = min(i+d,h)
    dp[hp] = min(dp[hp],dp[i]+c)

print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
INF=1000000000
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)
f=[INF]*(H+1)
for i in range(1,H+1):
    for j in range(N):
        if i>A[j]:
            f[i]=min(f[i],f[i-A[j]]+B[j])
        else:
            f[i]=min(f[i],B[j])
print(f[H])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000)
H, N = map(int, input().split())
A = []
B = []
for _ in range(N):
  a, b = map(int, input().split())
  A.append(a)
  B.append(b)
ls = [[-1]*(H+1) for _ in range(N)]
for h in range(H+1):
  tmp1 = h // A[0]
  tmp2 = h % A[0]
  if tmp2 == 0:
    ls[0][h] = B[0]*tmp1
  else:
    ls[0][h] = B[0]*(tmp1 + 1)
def battle(h, n):
  if ls[n][h] != -1:
    return ls[n][h]
  else:
    if h == 0:
      ls[n][h] = 0
      return ls[n][h]
    else:
      tmp_ls = []
      tmp = h
      item = 0
      while tmp >= 0:
        tmp_ls.append(battle(tmp, n-1)+item*B[n])
        item += 1
        tmp -= A[n]
      tmp_ls.append(item*B[n])
      ls[n][h] = min(tmp_ls)
      return ls[n][h]

print(battle(H, N-1))

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

dp = [math.inf]*(h+10000)
dp[0] = 0
for a, b in ab:
    for i in range(h+10000):
        dp[i] = min(dp[i], dp[i-a]+b)
print(min(dp[h:]))

# print(dp[:h+100])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
from itertools import accumulate
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


H, N = zz()
A, B = [], []
for i in range(N):
    a, b = zz()
    A.append(a)
    B.append(b)
# B, A = zip(*sorted(zip(B, A)))

max_a = max(A)
dp = [[100000000]*(H + max_a + 1)] * (N+1)  # dp[i][j]: 魔法i種類で、HPをj減らすのに必要な魔力
#  初期化
for j in range(H):
    if (j % A[0] == 0):
        dp[0][j] = (j//A[0])*B[0]
    else:
        dp[0][j] = (j//A[0]+1)*B[0]
# print(dp)
# print()

for j in range(1, H + max_a + 1):
    for i in range(0, N):

        if(j < A[i]):
            dp[i][j] = min(dp[i-1][j], B[i])
        else:
            # if (j > 8):
            #     print(dp[i - 1][j],
            #           dp[i][j - A[i]] + B[i],
            #           dp[i-1][j-A[i]] + B[i])

            dp[i][j] = min(dp[i - 1][j],
                           dp[i][j - A[i]] + B[i],
                           dp[i-1][j-A[i]] + B[i])


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

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
INF = 10 ** 9
dp = [INF for _ in range(2*10**4)]
AB = [tuple(map(int, input().split())) for _ in range(n)]
dp[0] = 0
for a, b in AB:
    for i in range(h):
        dp[i + a] = min(dp[i + a], dp[i] + b)
print(min(dp[h:]))

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
inf = 1 << 30
dp = [inf] * (h + 1)
dp[h] = 0

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

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
AB=[]
for i in range(N):
  AB.append(list(map(int,input().split())))
dp=[0]*(10**8)
dp[0]=0
i=0

while True:
  i+=1
  for A,B in AB:
    if i-B>=0:
      dp[i]=max(dp[i-B]+A,dp[i])
  if dp[i]>=H:
    break
print(i)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

INF = 10**15 
dp = np.full((H+1), INF, dtype=np.int64)
dp[0] = 0

for i, (a, b) in enumerate(magic):
    for h in range(H):
        dp[min(H, h+a)] = min(dp[min(H, h+a)], dp[h]+b)

print(dp[-1])

Submission status: Time Limit Exceeded

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

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

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

for i, (a, b) in enumerate(magic):
    for h in range(H):
        dp[min(H, h+a)] = min(dp[min(H, h+a)], dp[h]+b)

print(dp[-1])

Submission status: Time Limit Exceeded

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

dpl = H+max(A)
x = 10**10
dp = [x]*dpl
dp[0] = 0

for a, b in zip(A, B):
    for i in range(a, dpl):
        dp[i] = min(dp[i-a]+b, dp[i])
    for i in range(dpl-2, -1, -1):
        dp[i] = min(dp[i+1], dp[i])

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)]
INF = float("inf")
dp0 = [INF]*(H+1)
dp1 = [INF]*(H+1)
dp0[0] = 0
for i in range(N):
    a,b = AB[i]
    dp1[H] = dp0[H]
    for h in range(H-1, -1, -1):
        dp1[h] = min(dp0[h], dp1[h+1])
    assert dp1[0] == 0
    for h in range(H+1):
        dp1[h] = min(dp1[h], dp1[max(h-a,0)]+b)
    t = dp1
    dp1 = dp0
    dp0 = t
print(dp0[H])

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N)]
dp = [[0]*(N+1) for _ in range(2*10**4+5)]

for j in range(1,H+1):
  for i in range(1,N+1):
    if AB[i-1][0] < j:
      dp[j][i] = dp[j - AB[i-1][0]][i] + AB[i-1][1]
    else:
      dp[j][i] = AB[i-1][1]
  MIN = min(dp[j][1:])
  dp[j][1:] = [MIN]*(len(dp[j])-1)

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

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = []
for i in range(N):
    A, B = map(int, input().split())
    AB.append((A, B))
dp = [INF] * (H + 1)
dp[0] = 0
for A, B in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for A, B in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
def main():
    H, N = map(int, input().split())
    AB = [map(int, input().split()) for i in range(N)]
    dp = [INF] * (H + 1)
    dp[0] = 0
    for (A, B) in AB:
        for j in range(H+1):
            dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
    print(dp[H])
main()

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for (A, B) in AB:
    for j in range(H+1):
        k = j - A
        if k < 0:
            k = 0
        dp[j] = min(dp[j], dp[k] + B)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 10**10 # 無限大を表す値
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for (A, B) in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for (A, B) in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
dp = [INF] * (H + 1)
dp[0] = 0
for (A, B) in AB:
    for j in range(H+1):
        dp[j] = min(dp[j], dp[max(j-A, 0)] + B)
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
W = H + max(A for (A, B) in AB)
dp = [INF] * (W + 1)
dp[0] = 0
for (A, B) in AB:
    for j in range(A, W+1):
        dp[j] = min(dp[j], dp[j-A] + B)
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
W = H + max(A for (A, B) in AB)
dp = [INF] * (W + 1)
dp[0] = 0
for i, (A, B) in enumerate(AB):
    for j in range(A, W+1):
        dp[j] = min(dp[j], dp[j-A] + B)
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
W = H + max(A for (A, B) in AB)
dp = np.full(W+1, INF, dtype = np.int64)
dp[0] = 0
for i, (A, B) in enumerate(AB):
    for j in range(A, W+1):
        dp[j] = np.minimum(dp[j], dp[j-A] + B)
print(np.min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
W = H + max(A for (A, B) in AB)
dp = np.full((N+1, W+1), INF, dtype = np.int64)
dp[0][0] = 0
for i, (A, B) in enumerate(AB):
    dp[i+1] = np.minimum(dp[i+1], dp[i])
    for j in range(A, W+1):
        dp[i+1][j] = np.minimum(dp[i+1][j], dp[i+1][j-A] + B)
print(np.min(dp[N][H:W+1]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(N)]
W = H + max(A for (A, B) in AB)
dp = np.full((N+1, W+1), INF, dtype = np.int64)
dp[0][0] = 0
for i, (A, B) in enumerate(AB):
    for j in range(W+1):
        dp[i+1][j] = np.minimum(dp[i+1][j], dp[i][j])
        if j-A >= 0:
            dp[i+1][j] = np.minimum(dp[i+1][j], dp[i+1][j-A] + B)
print(np.min(dp[N][H:W+1]))

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
magics = []
min_cost = (0, 100000)
max_attack = (0, 100000)
for i in range(N):
    attack, cost = map(int, input().split())
    magics.append((attack,cost))
    if cost < min_cost[1]:
        min_cost = (attack, cost)
    if max_attack[0] < attack:
        max_attack = (attack, cost)
    

dp = [0]
dp.extend([min_cost[1]] * min_cost[0])
for i in range(min_cost[0]+1, H + max_attack[0] + 1):
    minc = 100000000000
    for magic in magics:
        cost = magic[1]
        if 0 < i - magic[0]:
            cost += dp[i - magic[0]]
        if cost < minc:
            minc = cost
    dp.append(minc)
print(min(dp[H:]))

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 = [float("INF")] * 10 ** 5
dp[0] = 0

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

print(min(dp[H:]))

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=[[10**9+1]*(h+1) for i in range(n+1)]
for i in dp:
  i[0]=0

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

Submission status: Time Limit Exceeded

Solution code:
H, N, *AB = map(int, open(0).read().split())
magics = [(A, B) for A, B in zip(*[iter(AB)] * 2)]
dp = [float("inf")] * (H + 1)
dp[0] = 0
for i in range(N):
    A, B = magics[i]
    for h in range(H):
        dp[min(h + A, H)] = min(dp[min(h + A, H)], dp[h] + B)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:

import sys
input = sys.stdin.buffer.readline
H, N = map(int, input().split())

INF = 1 << 40
dp = [INF]*(H+1)
dp[H] = 0
for i in range(N):
    a, b = map(int, input().split())
    for j in range(H, -1, -1):
        x = max(0, j-a)
        dp[x] = min(dp[x], dp[j]+b)

print(dp[0])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
A_list = []
B_list = []
for i in range(N):
    A, B = map(int, input().split())
    A_list.append(A)
    B_list.append(B)

dp = [[10**8+1]*(H) for _ in range(N+1)] # DPテーブルの作成

for i in range(N):
    for j in range(H):
        if j < A_list[i]: # 選ばない時
            #dp[i+1][j] = dp[i][j]
            dp[i+1][j] = min(dp[i][j],B_list[i])
        else: # 選ぶ時
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-A_list[i]]+B_list[i])
        #print(i,j,dp[i+1]) #debug

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

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
ma=[]
for _ in range(n):
  a=list(map(int,input().split()))
  ma.append(a)
  
inf=float('inf')
dp=[inf for _ in range(h+1) ]

dp[h]=0

for i in range(1,h+1):
  j=h+1-i
  for x in ma:
    p=x[0]
    q=x[1]
    if j-p>=1:
      dp[j-p]=min(dp[j-p],dp[j]+q)
    else:
      dp[0]=min(dp[0],dp[j]+q)
      
print(dp[0])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

h, n = map(int, input().split())
ab = set([])
for _ in range(n):
    ab.add(tuple(map(int,input().split())))
l = h + max(a for a, _ in ab)

#DP[i] = i までの魔法でモンスターの体力を減らすため消耗する魔力の最小値
dp = [10**8+1] * l
dp[0] = 0

for i in range(l):  #最大L回繰り返せばよい
    for a, b in ab:
        dp[i] = min(dp[i-a] + b, dp[i])
print(min(dp[h:]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

h, n = map(int, input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
l = h + max(a for a, _ in ab)

#DP[i] = i までの魔法でモンスターの体力を減らすため消耗する魔力の最小値
dp = [10**8+1] * l
dp[0] = 0

for i in range(l):  #最大L回繰り返せばよい
    for a, b in ab:
        dp[i] = min(dp[i-a] + b, dp[i])
print(min(dp[h:]))

Submission status: Time Limit Exceeded

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

h,n=map(int,input().split())
magic=[]
for i in range(n):
  magic.append(tuple(map(int,input().split())))

memo={}
def d(hp):
  if hp<=0:
    return 0
  if hp in memo:
    return memo[hp]
  mn=10**10
  for i in magic:
    mn=min(mn,d(hp-i[0])+i[1])
  memo[hp]=mn
  return mn

print(d(h))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
import math
h,n=map(int,input().split())
A=[]
dp=[math.inf for _ in range(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:
h,n=map(int,input().split())
c=[list(map(int,input().split()))for _ in range(n)]
d=[0]+[10**9]*h
for i in range(h):
    #d[i]=min(d[i-a]+b for a,b in c)
    for a,b in c:
        a=min(a,h-i)
        d[i+a]=min(d[i+a],d[i]+b)
print(d[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()

ans=[10**9]*(H+1)
ans[0]=0
i=0
while i<=H:
    for a,b in ab:
        if a+i>H:
            a=H-i
        if ans[i+a]>ans[i]+b:
            ans[i+a]=ans[i]+b
    i+=1

print(ans[H])

Submission status: Time Limit Exceeded

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

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

dp[0] = 0

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

res = dp[H]

print(res)

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
ab=[list(map(int,input().split()))for _ in range(N)]
from heapq import*

count=[0]+[1]*H
ans=[10**9]*(H+1)
ans[0]=0
que=[0]
heapify(que)
while que:
    x=heappop(que)
    for a,b in ab:
        a=min(a,H-x)
        if ans[x+a]>ans[x]+b:
            ans[x+a]=ans[x]+b
            if count[x+a]:
                heappush(que,x+a)
                count[x+a]=0

print(ans[H])

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
ab=[list(map(int,input().split()))for _ in range(N)]
from collections import deque

ans=[10**9]*(H+1)
ans[0]=0
que=deque([0])
while que:
    x=que.popleft()
    for a,b in ab:
        a=min(a,H-x)
        if ans[x+a]>ans[x]+b:
            ans[x+a]=ans[x]+b
            que.append(x+a)

print(ans[H])

Submission status: Time Limit Exceeded

Solution code:
H,N=map(int,input().split())
L=[tuple(map(int,input().split())) for i in range(N)]
dp=[[-1]*(H+1) for i in range(N+1)]
for d in dp:
    d[0]=0
dp[0]=[10**9]*(H+1)
i=1
for a,b in L:
    for j in range(H+1):
        if j<a:
            dp[i][j]=min(dp[i-1][j],b)
        else:
            dp[i][j]=min(dp[i-1][j],dp[i][j-a]+b)
    i+=1
print(dp[N][H-1])

Submission status: Time Limit Exceeded

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

DP = [float("inf") for _ in range(h+1)]
DP[0] = 0

for i in range(h):
    for damage, cost in AB:
        next_index = min(i+damage, h)
        DP[next_index] = min(DP[next_index], DP[i]+cost)

print(DP[-1])

Submission status: Time Limit Exceeded

Solution code:
INF = 10**8 + 1

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

dp = [[INF for _ in range(H + 1)] for _ in range(N + 1)]
dp[0][0] = 0
for i in range(1, N + 1):
    A, B = [int(x) for x in input().split()]
    for j in range(H + 1):
        dp[i][j] = min(dp[i - 1][j], dp[i][max(j - A, 0)] + B)

ans = dp[N][H]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
INF = 10**8 + 1

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

dp = [[INF for _ in range(H + 1)] for _ in range(N + 1)]
dp[0][H] = 0
for i in range(1, N + 1):
    A, B = [int(x) for x in input().split()]
    for j in range(H + 1):
        dp[i][j] = min(dp[i - 1][min(j + k * A, H)] + k * B for k in range((H - j) // A + 2))

ans = dp[N][0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

h, n = list(map(int, input().split(' ')))
d ={}
for i in range(n):
  a, b = list(map(int, input().split(' ')))
  d[b] = max(a, d.get(b, 0))

lld = [(v,k) for k, v in d.items()]
ld = [(v,k) for (k, v) in sorted(lld)[::-1]]
len_d = len(ld)
memo = {}
ans = 99999999999999999999999999
def f(index, current, damage):
  global ans
  if memo.get(current, 0) > damage or current >= ans:
    return
  memo[current] = damage
  if damage >= h:
    ans = min(ans, current)
  else:
    for i in range(index, len_d):
      f(i, current+ld[i][0], damage+ld[i][1])

f(0, 0, 0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
from sys import stdin

@jit
def num():
    h, n = map(int, input().split())
    magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
    INF = float('inf')
    ans = [INF]*(h+1)
    ans[-1] = 0

    for i in range(h, 0, -1):
        if ans[i] != INF:
            for j in magic:
                if i-j[0] < 0:
                    num = ans[i]+j[1]
                    if ans[0] > num:
                        ans[0] = num
                else:
                    num = ans[i]+j[1]
                    if ans[i-j[0]] > num:
                        ans[i-j[0]] = num
    return ans[0]
print(num())

Submission status: Time Limit Exceeded

Solution code:
def num():
    from sys import stdin
    h, n = map(int, input().split())
    magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
    INF = float('inf')
    ans = [INF]*(h+1)
    ans[-1] = 0

    for i in range(h, 0, -1):
        if ans[i] != INF:
            for j in magic:
                if i-j[0] < 0:
                    num = ans[i]+j[1]
                    if ans[0] > num:
                        ans[0] = num
                else:
                    num = ans[i]+j[1]
                    if ans[i-j[0]] > num:
                        ans[i-j[0]] = num
    return ans[0]
print(num())

Submission status: Time Limit Exceeded

Solution code:
def num():
    from sys import stdin
    h, n = map(int, input().split())
    magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
    INF = float('inf')
    ans = [INF]*(h+1)
    ans[-1] = 0

    for i in range(h, 0, -1):
        if ans[i] != INF:
            for j in magic:
                if i-j[0] < 0:
                    if ans[0] > ans[i]+j[1]:
                        ans[0] = ans[i]+j[1]
                elif ans[i-j[0]] > ans[i]+j[1]:
                    ans[i-j[0]] = ans[i]+j[1]
    return ans[0]
print(num())

Submission status: Time Limit Exceeded

Solution code:
def num():
    from sys import stdin
    h, n = map(int, input().split())
    magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
    INF = float('inf')
    ans = [INF]*(h+1)
    ans[-1] = 0

    for i in range(h, 0, -1):
        if ans[i] == INF:
          continue
        else:
            for j in magic:
                if i-j[0] < 0:
                    if ans[0] > ans[i]+j[1]:
                        ans[0] = ans[i]+j[1]
                elif ans[i-j[0]] > ans[i]+j[1]:
                    ans[i-j[0]] = ans[i]+j[1]
    return ans[0]
print(num())

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def num():
    h, n = map(int, input().split())
    magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
    INF = float('inf')
    ans = [INF]*(h+1)
    ans[-1] = 0

    for i in range(h, 0, -1):
        if ans[i] == float('inf'):
            continue
        else:
            for j in magic:
                if i-j[0] < 0:
                    if ans[0] > ans[i]+j[1]:
                        ans[0] = ans[i]+j[1]
                elif ans[i-j[0]] > ans[i]+j[1]:
                    ans[i-j[0]] = ans[i]+j[1]
    return ans[0]
print(num())

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
from sys import stdin
h, n = map(int, input().split())
magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
INF = float('inf')
ans = [INF]*(h+1)
ans[-1] = 0

@jit
def num(ans):
    for i in range(h, 0, -1):
        if ans[i] == float('inf'):
            continue
        else:
            for j in magic:
                if i-j[0] < 0:
                    if ans[0] > ans[i]+j[1]:
                        ans[0] = ans[i]+j[1]
                elif ans[i-j[0]] > ans[i]+j[1]:
                    ans[i-j[0]] = ans[i]+j[1]
    return ans[0]
print(num(ans))

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
from sys import stdin
h, n = map(int, input().split())
magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
INF = float('inf')
ans = [INF]*(h+1)
ans[-1] = 0

def num(ans):
    for i in range(h, 0, -1):
        if ans[i] == float('inf'):
            continue
        else:
            for j in magic:
                if i-j[0] < 0:
                    if ans[0] > ans[i]+j[1]:
                        ans[0] = ans[i]+j[1]
                elif ans[i-j[0]] > ans[i]+j[1]:
                    ans[i-j[0]] = ans[i]+j[1]
    return ans[0]
print(num(ans))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
h, n = map(int, input().split())
magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
INF = float('inf')
ans = [INF]*(h+1)
ans[-1] = 0

for i in range(h, 0, -1):
    if ans[i] == float('inf'):
        continue
    else:
        for j in magic:
            if i-j[0] < 0:
                if ans[0] > ans[i]+j[1]:
                    ans[0] = ans[i]+j[1]
            elif ans[i-j[0]] > ans[i]+j[1]:
                ans[i-j[0]] = ans[i]+j[1]
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
h, n = map(int, input().split())
magic = [list(map(int, stdin.readline().split())) for _ in range(n)]
ans = [float('inf')]*(h+1)
ans[-1] = 0

for i in range(h, 0, -1):
    if ans[i] == float('inf'):
        continue
    else:
        for j in magic:
            if i-j[0] <= 0:
                ans[0] = min(ans[i]+j[1], ans[0])
            else:
                ans[i-j[0]] = min(ans[i]+j[1], ans[i-j[0]])
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
h, n = map(int, input().split())
magic = []
for _ in range(n):
    magic += [list(map(int, input().split()))]
ans = [float('inf')]*(h+1)
ans[-1] = 0

@jit
def num(h, ans):
    for i in range(h, 0, -1):
        if ans[i] == float('inf'):
            continue
        else:
            for j in magic:
                if i-j[0] < 0:
                    if ans[0] > ans[i]+j[1]:
                        ans[0] = ans[i]+j[1]
                elif ans[i-j[0]] > ans[i]+j[1]:
                    ans[i-j[0]] = ans[i]+j[1]
    return ans[0]
print(num(h, ans))

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
magic = []
for _ in range(n):
    magic += [list(map(int, input().split()))]
ans = [float('inf')]*(h+1)
ans[-1] = 0

for i in range(h, 0, -1):
    if ans[i] == float('inf'):
        continue
    else:
        for j in magic:
            if i-j[0] < 0:
                if ans[0] > ans[i]+j[1]:
                    ans[0] = ans[i]+j[1]
            elif ans[i-j[0]] > ans[i]+j[1]:
                ans[i-j[0]] = ans[i]+j[1]
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,N = map(int,input().split())
spells = [list(map(int,input().split())) for i in range(N)]
INF = 10**10
dp = [INF]*(H+1)
dp[0] = 0
for use in spells:
    damage = use[0]
    mp = use[1]
    for i in range(1,H+1):
        dp[i] = min(dp[max(0,i-damage)] + mp, dp[i])
print(dp[-1])