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

ab=[None]*n
for i in range(n):
    ab[i]=tuple(map(int,input().split()))

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

for i in range(1,h+1):
    for a,b in ab:
        if 0 < i-a:
            dp[i]=min(dp[i], dp[i-a]+b)
        else:
            dp[i]=min(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):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
x = max(A)
dp = [10**9 for i in range(H+x+1)]
dp[0] = 0
for i in range(N):
    a = A[i]
    b = B[i]
    for j in range(H+x+1):
        if j < a:
            continue
        dp[j] = min(dp[j-a]+b,dp[j])
ans = 10**9
for i in range(H,H+x+1):
    ans = min(ans,dp[i])
print(ans)

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)
h = 2*10**4
dp = [10**9 for i in range(h+1)]
dp[0] = 0
for i in range(N):
    a = A[i]
    b = B[i]
    for j in range(h+1):
        if j < a:
            continue
        dp[j] = min(dp[j-a]+b,dp[j])
ans = 10**9
for i in range(H,h+1):
    ans = min(ans,dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(20000)

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

INF = 100000000000
DP_list = [INF]*(20010)
def DP(h):
    ans = INF
    if h <= 0:
        return 0
    elif DP_list[h] != INF:
        return DP_list[h]
    else:
        for i in range(N):
            k = magic[i][1] + DP(h - magic[i][0])
            ans = min(ans,k)
        DP_list[h] = ans
        return ans

print(DP(H))

Submission status: Time Limit Exceeded

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H, N = MI()
    AB = LLIN(N)

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

    print(dp[H])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

INF = 1 << 60
dp = [INF for _ in range(h+10001)]

dp[0] = 0

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

res = INF
for i in range(h, h+10001):
    res = min(res, dp[i])
print(res)

Submission status: Time Limit Exceeded

Solution code:
'''
# 以下はTLである
H, N = map(int, input().split())
magic = []
# Aは威力Bは消費魔力
for i in range(N):
    A, B = map(int, input().split())
    magic.append([A,B])

# DPで解く
# DP[i]ダメージをi与えた時の最低使用魔力
# DP[H]ダメージがH以上になったときの最低使用魔力
# DP[i] = -1 のときは計算しない
DP = [-1 for _ in range(H+1)]
DP[0] = 0

for i in range(H):
    if DP[i] == -1:
        pass
    else:
        for j in range(N):
            h = i + magic[j][0]
            mp = DP[i] + magic[j][1]
            if h <= H and DP[h] == -1:
                DP[h] = mp
            elif h <= H and DP[h] != -1:
                if DP[h] > mp:
                    DP[h] = mp
            elif h > H and DP[H] == -1:
                DP[H] = mp
            elif h >H and DP[H] != -1:
                if DP[H] > mp:
                    DP[H] = mp

#print(DP)
print(DP[H])
'''



def test_func(dp, A_list, B_list, i, N):
    # 参考return(min([(dp[i - A_list[j]] if i >= A_list[j] else 0) + B_list[j] for j in range(N)]))
    magic = []
    for j in range(N):
        # 考えたいiに対してその元となるdpが存在するか
        if i >= A_list[j]:
            magic.append(dp[i-A_list[j]] + B_list[j])
        # 初めての
        else:
            magic.append(B_list[j])
    # 一番魔力がかからなかったものを採用する
    return min(magic)

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 = [0 for _ in range(H+1)]

for i in range(1,H+1):
    dp[i] = test_func(dp, A_list, B_list, i, N)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
AB = []
Max_a = 0
for i in range(n):
    a,b = map(int,input().split())
    Max_a = max(Max_a,a)
    AB.append([a,b])

dp = [0 for i in range(h+Max_a)]
for i in range(1,len(dp)):
    ans = float('inf')
    for a,b in AB:
        #if i-a>=0:
        ans = min(dp[i-a]+b,ans)
    dp[i] = ans
print(dp[h])

Submission status: Time Limit Exceeded

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

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

INF = 1 << 60
dp = [INF for _ in range(h+10001)]

dp[0] = 0

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

res = INF
for i in range(h, h+10001):
    res = min(res, dp[i])
print(res)

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
inf = 10 ** 9
dp = [inf for i 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())
ab = list(list(map(int, input().split())) for _ in range(n) )

dp = [0] * (h + max(list( node[0] for node in ab )))

for i in range(1, len(dp)):
  d = 10 ** 9
  for a, b in ab:
    d = min(dp[i-a] + b, d)
  dp[i] = d
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
dp = [float("inf")]*(h+10001)
dp[0]=0

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

    for j in range(h+10001):
        dp[j] = min(dp[j],dp[j-a]+b)
ans = float("inf")
for i in range(h,h+10001):
    ans = min(ans,dp[i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,n = map(int,input().split())
dp = [[float("inf")]*(h+10001) for i in range(n+1)]
dp[0][0]=0

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

    for j in range(h+10001):
        dp[i+1][j] = min(dp[i][j],dp[i+1][j-a]+b)
ans = float("inf")
for i in range(h,h+10001):
    ans = min(ans,dp[n][i])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

magics = {}
for a, b in AB:
    for i in range(H // a + 2):
        if min(i * a, H) not in magics:
            magics[min(i * a, H)] = i * b
        else:
            magics[min(i * a, H)] = min(magics[min(i * a, H)], i * b)

sorted_keys = sorted(magics.keys())
dp = [float('inf') for _ in range(H+1)]

for key in sorted_keys:
    dp[key] = min(dp[key], magics[key])

for h in range(H):
    for key in sorted_keys:
        dp[min(h + key, H)] = min(dp[min(h + key, H)], dp[h] + magics[key])

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
# ref https://atcoder.jp/contests/abc153/submissions/9758833
# ref http://drken1215.hatenablog.com/entry/2020/01/26/225000

# coding: utf-8
H, N = map(int, input().split())
A = [0]
B = [0]

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

dp = [[float('inf') for i in range(H+1)] for j in range(N+1)]

for i in range(N+1):
    dp[i][0] = 0

for i in range(1, N+1):
    for j in range(1, H+1):
        dp[i][j] = min(dp[i-1][j],  # do not use magic
                       #    dp[i-1][max(j-A[i], 0)] + B[i],  # use i-th magic
                       dp[i][max(j-A[i], 0)] + B[i])   # use i-th magic
        #print(dp[i-1][j],dp[i-1][max(j-AB[i][0],0)] + AB[i][1],dp[i][max(j-AB[i][0],0)] + AB[i][1])


print(dp[N][H])

Submission status: Time Limit Exceeded

Solution code:
# ref https://atcoder.jp/contests/abc153/submissions/9758833
# ref http://drken1215.hatenablog.com/entry/2020/01/26/225000

# coding: utf-8
H, N = map(int, input().split())
A = [0]
B = [0]

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

dp = [[float('inf') for i in range(H+1)] for j in range(N+1)]

for i in range(N+1):
    dp[i][0] = 0

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


print(dp[N][H])

Submission status: Time Limit Exceeded

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

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

# 最初に入力の最大値と最大量でdp配列を作成
dp = [100000000] * (H + 10 ** 4 + 1)
dp[0] = 0

for i in range(H):
    if dp[i] == 100000000:
        continue
    for a, b in AB:
        # tは100000000 + 消費魔力
        t = dp[i] + b
        # dp[i + a] aはダメージ量
        if t < dp[i + a]:
            dp[i + a] = t

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

# 最初に入力の最大値と最大量でdp配列を作成
dp = [100000000] * (H + 10 ** 4 + 1)
dp[0] = 0

for i in range(H):
    if dp[i] == 100000000:
        continue
    for a, b in AB:
        # tは100000000 + 消費魔力
        t = dp[i] + b
        # dp[i + a] aはダメージ量
        if t < dp[i + a]:
            dp[i + a] = t

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)]
ab.sort(reverse=True)
inf=1000000000
ma=ab[0][0]
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=ab[i]
    for j in range(h):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                if j+x[0]>=h:
                    dp[h]=min(dp[h],dp[j]+x[1])
                else:
                    dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
            else:
                break
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
magic = []
# Aは威力Bは消費魔力
for i in range(N):
    A, B = map(int, input().split())
    magic.append([A,B])

# DPで解く
# DP[i]ダメージをi与えた時の最低使用魔力
# DP[H]ダメージがH以上になったときの最低使用魔力
# DP[i] = -1 のときは計算しない
DP = [-1 for _ in range(H+1)]
DP[0] = 0

for i in range(H):
    if DP[i] == -1:
        pass
    else:
        for j in range(N):
            h = i + magic[j][0]
            mp = DP[i] + magic[j][1]
            if h <= H and DP[h] == -1:
                DP[h] = mp
            elif h <= H and DP[h] != -1:
                if DP[h] > mp:
                    DP[h] = mp
            elif h > H and DP[H] == -1:
                DP[H] = mp
            elif h >H and DP[H] != -1:
                if DP[H] > mp:
                    DP[H] = mp

#print(DP)
print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10000000)

h, n = map(int, input().split())
m_list = []
max_a = 0
for i in range(n):
  a, b = map(int, input().split())
  m_list.append((a, b))
  if a > max_a:
    max_a = a
  
class Attack_Memo():
  def __init__(self):
    self.attack_memo = {}
  
  def attack(self, h):
    res_list = []
    if h <= 0:
      return 0
    if h in self.attack_memo:
      return self.attack_memo[h]
    for a, b in m_list:
      base_b = self.attack(h-a)
      res_list.append(base_b+b)
    self.attack_memo[h] = min(res_list)
    return min(res_list)

res = 100000000000
ins = Attack_Memo()
for i in range(h+a+1):
  tmp = ins.attack(i)
  if i >= h and tmp <= res:
    res = tmp
print(res)

Submission status: Time Limit Exceeded

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

# process
INF = 10**9
dp = [INF]*(H+max(a for a, _ in AB))
dp[0] = 0

for i in range(H):
    if dp[i] == INF:
        continue
    for a, b in AB:
        t = dp[i]+b
        if dp[i+a] > t:
            dp[i+a] = t

# output
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10000000)

h, n = map(int, input().split())
m_list = []
max_a = 0
for i in range(n):
  a, b = map(int, input().split())
  m_list.append((a, b))
  if a > max_a:
    max_a = a
  
class Attack_Memo():
  def __init__(self):
    self.attack_memo = {}
  
  def attack(self, h):
    res_list = []
    if h <= 0:
      return 0
    if h in self.attack_memo:
      return self.attack_memo[h]
    for a, b in m_list:
      base_b = self.attack(h-a)
      res_list.append(base_b+b)
    self.attack_memo[h] = min(res_list)
    return min(res_list)

res = 100000000000
ins = Attack_Memo()
for i in range(h+a+1):
  tmp = ins.attack(i)
  if i >= h:
    if tmp <= res:
      res = tmp
print(res)

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
    a_sub,b_sub=map(int,input().split())
    a.append(a_sub)
    b.append(b_sub)
inf=10000000000000
ma=max(a)
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=[a[i],b[i]]
    for j in range(h):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
                if j+x[0]>=h:
                    dp[h]=min(dp[h],dp[j]+x[1])
            else:
                break
print(dp[h])

Submission status: Time Limit Exceeded

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

    # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値
    # としたとき、更新順序を工夫することで添字 i をなくせる
    dp = [0] + [float('inf')] * H
    for a, b in Monsters:
        for j in range(H + 1):
            nj = min(j + a, H)
            dp[nj] = min(dp[nj], dp[j] + b)
    return dp[H]

print(e_crested_ibis_vs_monster_in_place())

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
    a_sub,b_sub=map(int,input().split())
    a.append(a_sub)
    b.append(b_sub)
inf=10000000000000
ma=max(a)
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=[a[i],b[i]]
    for j in range(h):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
            else:
                break
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)]

A_max=max(AB)[0]
DP=[0]+[float("inf")]*(h+A_max)
for i in range(h):
    if DP[i]==float("inf"):continue
    for a,b in AB:
        t=DP[i]+b
        if t<DP[i+a]:
            DP[i+a]=t
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+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[n][h])

Submission status: Time Limit Exceeded

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

# process
max_a = max(a for a, _ in AB)
dp = [10**9]*(H+max_a)
dp[0] = 0

for i in range(H):
    if dp[i] == 10**9:
        continue
    for a, b in AB:
        t = dp[i]+b
        if dp[i+a] > t:
            dp[i+a] = t

# output
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
h, ｎ = map(int, input().split())
dp = [-1] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  if dp[i] == -1: continue
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    if dp[ht] != -1:
      dp[ht] = min(dp[ht], dp[i] + j[1])
    else:
      dp[ht] = dp[i] + j[1]

print(dp[h])

Submission status: Time Limit Exceeded

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

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

dp = [INF] * (H + 1)
dp[0] = 0
for a, b in AB:
  for i in range(H):
    if dp[i] != INF:
      dp[min(i + a, H)] = min(dp[min(i + a, H)], dp[i] + b)

print(dp[-1])

Submission status: Time Limit Exceeded

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

# process
max_a = max(A)
dp = [10**9]*(H+max_a)
dp[0] = 0

for i in range(H):
    if dp[i] == 10**9:
        continue
    for j in range(N):
        t = dp[i]+B[j]
        if dp[i+A[j]] > t:
            dp[i+A[j]] = t

# output
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
h, ｎ = map(int, input().split())
dp = [10**10] * (h + 1)
dp[0]=0
ab = []

for i in range(n):
  temp = list(map(int, input().split()))
  ab.append(temp)

for i in range(h):
  t = 10 ** 10
  for j in ab:
    ht = i + j[0]
    if ht >= h: ht = h
    dp[ht] = min(dp[ht], dp[i] + j[1]) 

print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
#!python3

from heapq import heappush, heappop, heapify

iim = lambda: map(int, input().rstrip().split())

def resolve():
    H, N = iim()
    AB = [tuple(iim()) for i in range(N)]

    AB.sort(key=lambda a: (a[0]/a[1], a[0]), reverse=True)

    S = []
    for a, b in AB:
        n, m = divmod(H, a)
        i = 1
        while n >= i:
            S.append((a*i, b*i))
            n -= i
            i <<= 1
        if m:
            n += 1
        if n:
            S.append((a*n, b*n))

    N2 = len(S)
    def ubound(b, a, i):
        for j in range(i, N2):
            aj, bj = S[j]
            if a + aj >= H:
                return (b+bj, b + (H-a) * bj / aj)
            a += aj
            b += bj

        return (inf, inf)

    q = []
    u0, u1 = ubound(0, 0, 0)

    inf = float("inf")
    heappush(q, (u1, u0, 0, 0, 0))
    um = u0
    #print(AB)
    #print(H, S)
    while q:
        #print(q)
        uq0, uq1, aq, bq, i = heappop(q)

        if uq0 >= um:
            break

        if i >= N2: continue

        ai, bi = S[i]
        if aq + ai < H:
            heappush(q, (uq0, uq1, aq + ai, bq + bi, i + 1))

        u0, u1 = ubound(bq, aq, i + 1)
        if u1 <= um:
            if u0 < um:
                um = u0
            heappush(q, (u1, u0, aq, bq, i + 1))

    print(um)



if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
dp = [10 ** 9] * (h+1)
dp[0] = 0

for ni in range(n):
  a, b = map(int, input().split())
  for hi in range(h):
    nj = min(hi+a, h)
    dp[nj] = min(dp[nj], dp[hi] + b)

print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
#!python3

from heapq import heappush, heappop, heapify

iim = lambda: map(int, input().rstrip().split())

def resolve():
    H, N = iim()
    AB = [tuple(iim()) for i in range(N)]

    AB.sort(key=lambda a: (a[0]/a[1], a[0]), reverse=True)

    S = []
    for a, b in AB:
        h = H
        i = 1
        while h >= i:
            S.append((a*i, b*i))
            h -= i
            i <<= 1
        if h:
            S.append((a*h, b*h))

    N2 = len(S)
    def ubound(b, a, i):
        for j in range(i, N2):
            aj, bj = S[j]
            if a + aj >= H:
                return (b+bj, b + (H-a) * bj / aj)
            a += aj
            b += bj

        return (inf, inf)

    q = []
    u0, u1 = ubound(0, 0, 0)

    inf = float("inf")
    heappush(q, (u1, u0, 0, 0, 0))
    um = u0
    #print(AB)
    #print(H, S)
    while q:
        #print(q)
        uq0, uq1, aq, bq, i = heappop(q)

        if uq0 >= um:
            break

        if i >= N2: continue

        ai, bi = S[i]
        if aq + ai < H:
            heappush(q, (uq0, uq1, aq + ai, bq + bi, i + 1))

        u0, u1 = ubound(bq, aq, i + 1)
        if u1 <= um:
            if u0 < um:
                um = u0
            heappush(q, (u1, u0, aq, bq, i + 1))

    print(um)



if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


max_size = 2 * 10**4
h, n = map(int, input().split())
DP = np.ones(max_size, dtype=np.int64) * 10**9
DP[0] = 0

for _ in range(n):
    power, cost = map(int, input().split())
    for i in range((h+power-1)//power):
        DP[(i+1)*power: (i+2)*power] = np.minimum(DP[(i+1)*power: (i+2)*power], DP[(i)*power: (i+1)*power] + cost)

print(DP[h:].min())

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    h,n=map(int,input().split())
    a=[[int(i) for i in input().split()]for i in range(n)]
    
    dp=[float('inf')]*(h+1) #0-9
    dp[0]=0
    mini=float('inf')
    for i in range(h+1):
        if dp[i]==float('inf'):
            continue
        for ata,mp in a:
            if i+ata>=h:
                tmp=dp[i]+mp
                mini=min(mini,tmp)
                continue
            dp[i+ata]=min(dp[i+ata],dp[i]+mp)
    print(mini)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input=sys.stdin.readline
    h,n=map(int,input().split())
    a=[[int(i) for i in input().split()]for i in range(n)]
    
    dp=[float('inf')]*(h+1) #0-9
    dp[h]=0
    mini=float('inf')
    for i in reversed(range(0,h+1)):
        if dp[i]==float('inf'):
            continue
        for ata,mp in a:
            if i-ata<=0:
                tmp=dp[i]+mp
                mini=min(mini,tmp)
                continue
            dp[i-ata]=min(dp[i-ata],dp[i]+mp)
    print(mini)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

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

dp=[float('inf')]*(h+1) #0-9
dp[h]=0
mini=float('inf')
for i in reversed(range(0,h+1)):
    if dp[i]==float('inf'):
        continue
    for ata,mp in a:
        if i-ata<=0:
            tmp=dp[i]+mp
            mini=min(mini,tmp)
            continue
        dp[i-ata]=min(dp[i-ata],dp[i]+mp)
print(mini)

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())))
# print(AB)
f_inf = float('inf')
dp = [f_inf] * (H + 1)
dp[0] = 0
for h in range(H):
    for damage, cost in AB:
        next_index = min(h + damage, H)
        dp[next_index] = min(dp[next_index], dp[h] + cost)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

dp = [sys.maxsize for i in range(H+1)]
dp[0] = 0

# 各ダメージ段階の最小値を求める
for i in range(H):
    if dp[i] == sys.maxsize:
        continue
    # 各魔法を使って最小値なら更新
    for j in range(N):
        a = min(i + AB[j][0], H)
        b = dp[i] + AB[j][1]
        if dp[a] > b:
            dp[a] = b

print(dp[H])

Submission status: Time Limit Exceeded

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

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

dp[0] = 0

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


INF=10**9
dp=[INF for i in range(H+max(A))]
dp[0]=0

for i in range(H):
    for j in range(N):
        p1=dp[i+A[j]]
        p2=dp[i]+B[j]
        dp[i+A[j]]=min(p1,p2)

print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, N = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    dp = [10**12 for i in range(H+1)]  # dp[i] := 体力をi減らすときの魔力の最小値
    dp[0] = 0
    for h in range(H):
        for damage, cost in AB:
            next_index = min(h + damage, H)
            dp[next_index] = min(dp[next_index], dp[h] + cost)
    print(dp[H])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

dp = [sys.maxsize for i in range(H+1)]
dp[0] = 0

# 各ダメージ段階の最小値を求める
for i in range(H):
    if dp[i] == sys.maxsize:
        continue
    # 各魔法を使って最小値なら更新
    for j in range(N):
        a = min(i + AB[j][0], H)
        b = dp[i] + AB[j][1]
        if dp[a] > b:
            dp[a] = b

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
"""問題文
トキはモンスターと戦っています。
モンスターの体力は  H  です。
トキは  N  種類の魔法が使え、 i  番目の魔法を使うと、モンスターの体力を  A i  減らすことができますが、トキの魔力を  B i  消耗します。
同じ魔法は何度でも使うことができます。魔法以外の方法でモンスターの体力を減らすことはできません。
モンスターの体力を  0  以下にすればトキの勝ちです。  トキがモンスターに勝つまでに消耗する魔力の合計の最小値を求めてください。
"""
from functools import lru_cache
import sys


# @lru_cache(maxsize=1000)
# def attack(h, mp):
#     """
#     毎回Nパターン選択肢がある
#     最小値を返す
#     """
#     min_mp = 999999
#     for a, b in sorted_ab_list:
#         new_h = h - a
#         new_mp = mp + b
#
#         if new_mp >= min_mp:
#             # 既に最小値超えてたら中断
#             continue
#
#         if new_h > 0:
#             min_mp = min(min_mp, attack(new_h, new_mp))
#         else:
#             min_mp = min(min_mp, new_mp)
#
#     # 最小値をとる
#     return min_mp


def use_dp():
    max_a = max([ab[0] for ab in AB])
    dp = [sys.maxsize for i in range(H+1)]
    dp[0] = 0

    # 各ダメージ段階の最小値を求める
    for i in range(H):
        if dp[i] == sys.maxsize:
            continue
        # 各魔法を使って最小値なら更新
        for j in range(N):
            a = min(i + AB[j][0], H)
            b = dp[i] + AB[j][1]
            if dp[a] > b:
                dp[a] = b

    min_mp = dp[H]

    # H < i < H+max(Ai)の範囲で最小値を求める
    # min_mp = sys.maxsize
    # for i in range(H, H+max_a):
    #     min_mp = min(dp[i], min_mp)

    print(min_mp)


if __name__ == '__main__':
    H, N = list(map(int, input().split()))
    AB = []
    for n in range(N):
        AB.append(list(map(int, input().split())))

    use_dp()

    # sorted_ab_list = sorted(AB_list, key=lambda x: x[0]/x[1], reverse=True)
    #
    # print(attack2(H))

Submission status: Time Limit Exceeded

Solution code:
"""問題文
トキはモンスターと戦っています。
モンスターの体力は  H  です。
トキは  N  種類の魔法が使え、 i  番目の魔法を使うと、モンスターの体力を  A i  減らすことができますが、トキの魔力を  B i  消耗します。
同じ魔法は何度でも使うことができます。魔法以外の方法でモンスターの体力を減らすことはできません。
モンスターの体力を  0  以下にすればトキの勝ちです。  トキがモンスターに勝つまでに消耗する魔力の合計の最小値を求めてください。
"""
from functools import lru_cache
import sys


# @lru_cache(maxsize=1000)
# def attack(h, mp):
#     """
#     毎回Nパターン選択肢がある
#     最小値を返す
#     """
#     min_mp = 999999
#     for a, b in sorted_ab_list:
#         new_h = h - a
#         new_mp = mp + b
#
#         if new_mp >= min_mp:
#             # 既に最小値超えてたら中断
#             continue
#
#         if new_h > 0:
#             min_mp = min(min_mp, attack(new_h, new_mp))
#         else:
#             min_mp = min(min_mp, new_mp)
#
#     # 最小値をとる
#     return min_mp
#
#
# def attack2(h):
#     t_mp = 0
#     t_a = sorted_ab_list[0][0]
#     t_b = sorted_ab_list[0][1]
#     while h > 0:
#         h = h-a
#         t_mp += b
#
#     # やり直しが必要
#     if h < 0:
#         over_h = -h
#         mp = t_mp - t_b
#         # filter_list = [i for i in sorted_ab_list if i[0] > 1]
#     return mp


def use_dp():
    max_a = max([ab[0] for ab in AB])
    dp = [sys.maxsize for i in range(H+max_a)]
    dp[0] = 0

    # 各ダメージ段階の最小値を求める
    for i in range(H):
        if dp[i] == sys.maxsize:
            continue
        # 各魔法を使って最小値なら更新
        for j in range(N):
            a = i + AB[j][0]
            b = dp[i] + AB[j][1]
            if dp[a] > b:
                dp[a] = b

    # H < i < H+max(Ai)の範囲で最小値を求める
    min_mp = sys.maxsize
    for i in range(H, H+max_a):
        min_mp = min(dp[i], min_mp)

    print(min_mp)


if __name__ == '__main__':
    H, N = list(map(int, input().split()))
    AB = []
    for n in range(N):
        AB.append(list(map(int, input().split())))

    use_dp()

    # sorted_ab_list = sorted(AB_list, key=lambda x: x[0]/x[1], reverse=True)
    #
    # print(attack2(H))

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
f_inf = float("inf")
dp = [f_inf] * (h + 1)
dp[0] = 0
for i in range(n):
    a, b = map(int, input().split())
    for j in range(h):
        next_j = min(j + a, h)
        dp[next_j] = min(dp[next_j], dp[j] + b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
attack = [None] * n
cost = [None] * n
for i in range(n):
    a, c = map(int, input().split())
    attack[i] = a
    cost[i] = c
dp = [0] + [float('inf')] * h
for i in range(1, h + 1):
    for j in range(n):
        dp[i] = min(dp[i], cost[j] + dp[max(0, i - attack[j])])
print(dp[h])