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 = [list(map(int, input().split())) for _ in range(N)]

d = [0] + [10 ** 18] * H

for hp in range(1, H + 1):
    for ab in AB:
        # print(ab)
        hp_remain = hp - ab[0]
        if hp_remain < 0: hp_remain = 0
        # print(hp_remain)
        c = d[hp_remain] + ab[1]
        if c < d[hp]: d[hp] = c

print(d[H])

Submission status: Time Limit Exceeded

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

INF = float("inf")
    
dp = [[INF] * (1+10**4) for i in range(1+10**3)]


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(h, j+a[i])] = min(dp[i+1][min(h, j+a[i])], dp[i+1][j] + b[i])

print(dp[n][h])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    H,N = map(int, input().split())
    A = [None]*N
    B = [None]*N
    for i in range(N):
        A[i], B[i] = map(int, input().split())
    
    dp = [float("inf")] * (H+1)
    dp[0] = 0

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

    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

inf = 2**30
dp = [inf] * (H + 1)
dp[0] = 0
for i in range(N):
    A, B = list(map(int, input().split()))
    for j in range(H + 1):
        if dp[j] != inf:
            next = min(H, j + A)
            dp[next] = min(dp[next], dp[j] + B)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H, N = map(int, input().split())
inf =1000100001001000
dp = [inf for _ in range(H+1)]

# min_cost = np.inf
dp[0]=0
for n in range(N):
    a,b =  map(int, input().split())
    for h in range(H):
        nj = min(h+a,H)
        dp[nj] = min([dp[nj],dp[h]+b])
        
print(int(dp[H]))

Submission status: Time Limit Exceeded

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

    INF = 10 ** 8

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

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

    for _ in range(N):
        A, B = map(int, readline().split())
        for h in range(H):
            dp[min(H, h + A)] = min(dp[min(H, h + A)], dp[h] + B)

    print(dp[H])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
INF = 10 ** 8

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

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

for _ in range(N):
    A, B = map(int, input().split())
    for h in range(H):
        dp[min(H, h + A)] = min(dp[min(H, h + A)], dp[h] + B)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
INF = 1145141919810893

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[i]=体力がちょうどiのモンスターを倒すのに必要な魔力の最小値
#もしそのような魔力が存在しない場合、INF
#INFは10^9くらいあれば足りると思う(適当)
dp = [INF]*20000
dp[0]=0
#体力が0のモンスターを倒すのに必要な魔力は0
#iは小さい順に決定することができる
#なぜなら、i<jのとき決定したdp[j]の値はdp[i]に影響しないから
for i in range(H):
    #dp[H]から配る遷移は、必ずdp[H]より大きい値を配ることになるので、不要
    #よってH-1までやればOK
    #dp[i]の値はこの時点で確定する
    if dp[i]==INF:
        continue
    for j in range(N):
        #いわゆる配るDP
        dp[i+A[j]]=min(dp[i+A[j]],dp[i]+B[j])

#これで答えはdp[H]であるかというと、そうとは限らない
#なぜなら、dp[H+1]に行くコストがdp[H]に行くコストより安いかもしれないから
#今回は「与えるダメージがH以上」で条件を達成することができる
#なので、H以上のdpについて精査して、最もコストが低いものが答えである
Ans=INF
for d in dp[H:]:
    Ans=min(Ans,d)
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

# インデックスはライフ 値はそれを以下にする最小MP
dp = [np.inf for i in range(H+1)]
dp[0] = 0

for i in range(1, H+1):
    quietus_MP = np.inf
    for j in AB:
        if i-j[0] >= 0:
            dp[i] = min(dp[i-j[0]]+j[1], dp[i])
        # 一回で倒せて一番MP使わない魔法
        if j[0] > i:
            quietus_MP = min(j[1], quietus_MP)
    # 一回で倒す場合
    dp[i] = min(quietus_MP, dp[i])

print(dp[-1])

Submission status: Time Limit Exceeded

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

ABs = []
best = (0, 0)
dp = [2**32] * (H + 1)
dp[0] = 0
for i in range(N):
    A, B = list(map(int, input().split()))
    ABs.append((A, B))


for j in range(H + 1):
    for A, B in ABs:
        next = min(H, j + A)
        dp[next] = min(dp[next], dp[j] + B)

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

INF = 10**12

H, N = map(int, input().split())
magics = [tuple(map(int, input().split())) for _ in range(N)]
dp = [[INF]*(H+1) for _ in range(N+1)]
dp[0][0] = 0
for i, (a, b) in enumerate(magics):
    for j in range(H+1):
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        dp[i+1][min(H, j+a)] = min(dp[i+1][min(H, j+a)], dp[i+1][j] + b)
print(dp[N][H])

Submission status: Time Limit Exceeded

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

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

d = [0] + [10**18] * H

for hp in range(1, H + 1):
    for ab in AB:
        # print(ab)
        hp_remain = hp - ab[0]
        if hp_remain < 0: hp_remain = 0
        # print(hp_remain)
        c = d[hp_remain] + ab[1]
        if c < d[hp]: d[hp] = c

print(d[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
H, N = map(int, input().split())

dp = [np.inf for _ in range(H+1)]

# min_cost = np.inf
dp[0]=0
for n in range(N):
    a,b =  map(int, input().split())
    for h in range(H):
        if dp[h]!=np.inf:
            nj = min(h+a,H)
            dp[nj] = min([dp[nj],dp[h]+b])
            # if h+a<H:
            #     dp[h+a] = np.min([dp[h+a], dp[h]+b])
            # elif dp[h]+b<min_cost:
            #     min_cost = dp[h]+b
        
print(int(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)]
INF = float('inf')
dp = [INF]*(H+1)
dp[0] = 0
for i in range(1,H+1):
  for k in AB:
    a = k[0]
    b = k[1]
    tmp = max(0,i-a)
    dp[i] = min(dp[i],dp[tmp]+b)
    
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
dp = [[0] * (h+1) for i in range(n)]
a, b = map(int, input().split())
for c in range(1, h+1):
  dp[0][c] = b * ((c-1) // a + 1)
for i in range(1, n):
  a, b = map(int, input().split())
  for c in range(1, h+1):
    opt1 = dp[i-1][c]
    opt2 = dp[i-1][max(0, c-a)] + b
    opt3 = dp[i][max(0, c-a)] + b
    dp[i][c] = min(opt1, opt2, opt3)
print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

def resolve():
    import sys
    input = sys.stdin.readline
    
    H, N = map(int, input().split())
    v = []
    w = []
    for i in range(N):
        a, b = map(int, input().split())
        v.append(a)
        w.append(b)

    dp = [[0]*H for _ in range(N)]

    # initiate
    for i in range(H):
        dp[0][i] = w[0] * (math.ceil((i + 1) / v[0])) # 魔法1だけでHまで削るのに必要なコスト(MP)

    for i in range(1, N):
        for j in range(H):
            if j+1 < v[i]: 
                # 敵のHPが魔法iで与えるダメージより小さい場合、そのままの組み合わせか新しい魔法1回とで、コストの少ない方を選ぶ
                dp[i][j] = min(dp[i-1][j], w[i])
            else:
                # 大きい場合、そのままの組み合わせと、魔法iを取り込んだ組み合わせで、コストの少ない方を選ぶ
                dp[i][j] = min(dp[i-1][j], dp[i][j - v[i]] + w[i])

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


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
H, N = map(int, input().split())

dp = [np.inf for _ in range(H+1)]

min_cost = np.inf
dp[0]=0
for n in range(N):
    a,b =  map(int, input().split())
    for h in range(H):
        if dp[h]!=np.inf:
            if h+a<H:
                dp[h+a] = np.min([dp[h+a], dp[h]+b])
            elif dp[h]+b<min_cost:
                min_cost = dp[h]+b
print(int(min_cost))

Submission status: Time Limit Exceeded

Solution code:
def run():
  H, N = map(int, input().split())
  ab_li = [tuple(map(int, input().split())) for n in range(N)]
  dp = [0]*(H+1)
  for i in range(1, H+1):
    new_li = []
    for n in range(N):
      a, b = ab_li[n]
      if a <= i:
        new_li.append(dp[i-a]+b)
      else:
        new_li.append(b)
    dp[i] = min(new_li)
  print(dp[-1])
  
if __name__ == '__main__':
  run()

Submission status: Time Limit Exceeded

Solution code:


H,N=map(int,input().split())
weight=[]
value=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    weight.append(Ai)
    value.append(Bi)
INF=100000001
#print(weight)
#print(value)


dp=[INF for i in range(H+1)]
dp[0]=0
for i in range(N):
    a=weight[i]
    b=value[i]
    for j in range(H+1):
        nj=min(j+a,H)
        dp[nj]=min(dp[nj],dp[j]+b)
print(dp[H])

Submission status: Time Limit Exceeded

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

def dp(H,N,ls):
    table=[[float('inf')]*(H+1)] + [[0]*(H+1) for i in range(N)]
    for i in range(1,N+1):
        atk,cost=ls[i-1][0],ls[i-1][1]
        for h in range(H+1):
            if atk>=h:
                table[i][h]=min(table[i-1][h], cost)
            else:
                table[i][h]=min(table[i-1][h], table[i][h-atk]+cost)
    return(table[-1][-1])

print(dp(H,N,ls))

Submission status: Time Limit Exceeded

Solution code:
h,n=map(int,input().split())
ans=[10**10 for i in range(h+1)]+[0 for i in range(10**4)]
ans[0]=0
for i in range(n):
    a,b=map(int,input().split())
    tmp=0
    while tmp<=h:
        ans[tmp]=min(ans[tmp-a]+b,ans[tmp])
        tmp+=1
print(ans[h])

Submission status: Time Limit Exceeded

Solution code:
import sys
inputs = sys.stdin.readlines()

h, n = map(int, inputs[0].split())
AB = []
for s in inputs[1:]:
    a, b = map(int, s.split())
    AB.append((a, b))
AB.sort(key=lambda ab:ab[1])

dp = [10 ** 9] * (h+1)
dp[0] = 0
 
for i in range(h):
    for a, b in AB:
        k = min(i + a, h)
        dp[k] = min(dp[k], dp[i] + b)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

h,n=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n)]
dp=[[float('inf')]*(h+1) for i in range(n+1)]
dp[0][0]=0
# print(dp)
for i in range(n):
    for j in range(h+1):
        dp[i+1][j]=min(dp[i][j],dp[i+1][j])
        tmp=j+ab[i][0]
        if j+ab[i][0]>h:
            tmp=h
        # print(min(dp[i][j]+ab[i][1],dp[i+1][tmp]))
        dp[i+1][tmp]=min(dp[i+1][j]+ab[i][1],dp[i+1][tmp])
        # print(dp[i+1][tmp])
# print(dp)
print(dp[-1][-1])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
MAXH = H+1
DP = [float("inf")]*MAXH
DP[0] = 0
a = []
b = []
for i in range(N):
  A,B = map(int,input().split())
  a.append(A)
  b.append(B) 
for i in range(MAXH):
  if DP[i] == float("inf"):
    continue    
  for j in range(N):
        DP[min(i+a[j],H)] = min(DP[min(i+a[j],H)],DP[i]+b[j])
print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

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

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
H, N = map(int, input().split())
a_list = []
b_list = []

best_at_a = None
best_at_b = None
best_at_c = 0

for _ in range(N):
    a,b =  map(int, input().split())
    a_list.append(a)
    b_list.append(b)
    
    c = a/b # コスパ
    if c>best_at_c:
        best_at_c=c
        best_at_a=a
        best_at_b=b
a_list = np.array(a_list)
b_list = np.array(b_list)


dp = [np.inf for _ in range(H)]

min_cost = np.inf
dp[0]=0
for h in range(H): # h はこれまで削った体力
    if dp[h]!=np.inf:
        for n in range(N):
            a= a_list[n]
            b= b_list[n]
            if h+a_list[n]<H:
                dp[h+a] = np.min([dp[h+a], dp[h]+b])
            elif dp[h]+b<min_cost:
                min_cost = dp[h]+b
print(int(min_cost))

Submission status: Time Limit Exceeded

Solution code:
# E
# -*- coding: utf-8 -*-
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)]
dp[0] = 0
 
for i in range(1, H + 1):
    dp[i] = min([(dp[i - A_list[j]] if i >= A_list[j] else 0) + B_list[j] for j in range(N)])

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
# E TLE
# -*- coding: utf-8 -*-
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 ** 9 for _ in range(H + 1)]
dp[0] = 0
 
for i in range(1, H + 1):
    dp[i] = min(dp[i], min([(dp[i - A_list[j]] if i >= A_list[j] else 0) + B_list[j] for j in range(N)]))

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
inputs = sys.stdin.readlines()

h, n = map(int, inputs[0].split())
AB = []
for s in inputs[1:]:
    a, b = map(int, s.split())
    AB.append((a, b))
AB.sort(key=lambda ab:ab[1])

# AB_ = [AB[0]]
# x = AB[0][0] / AB[0][1]
# for a, b in AB[1:]:
#     if a/b > x:
#         x = a/b
#         AB_.append((a, b))
# AB, n = AB_, len(AB_)

def calc_min_magic(h):
    val = float('inf')
    for i in range(n):
        a, b = AB[i]
        val = min(val, dp[h-a]+b if a <= h else b)
    return val

dp = [0] * (h+1)
for i in range(1, h+1):
    dp[i] = calc_min_magic(i)
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
h,n = map(int,readline().split())
a = [0]*n
b = [0]*n
for i in range(n):
    a[i],b[i] = map(int,readline().split())
#  dp[i] i 以上の攻撃力を与えるための最小魔力
#b,a = zip(*sorted(zip(b,a))) #魔力でソート
dp = [0]*(h+1)
dp[0] = 0
for i in range(1,h+1):
    dp[i] = 10**12
    for j in range(n):#すべての種類の魔法に対してループ
        if i -a[j] >= 0:#魔法が使えるなら
            dp[i] = min(dp[i],dp[i-a[j]]+b[j])
        else: dp[i] = min(dp[i],b[j])
"""print(dp)
print(a,b)"""
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 10 ** 18
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
dp = [INF for i in range(H + 1)]
dp = np.array(dp)
ab = np.array(ab)
dp[0] = 0

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

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
A = [0]*N
B = [0]*N
inf = float('inf')
for i in range(N):
    A[i], B[i] = map(int,input().split())
dp = [inf]*(H+1)
dp[0] = 0
for i in range(N):
    for j in range(H+1):
        if j >= A[i]:
            dp[j] = min(dp[j],dp[j-A[i]] + B[i])
        else: # ワンパンで倒せる場合
            dp[j] = min(dp[j],B[i])
print(dp[H])
# print(dp)

Submission status: Time Limit Exceeded

Solution code:
H,N,*AB = map(int, open(0).read().split())
dp = [0]+[10**8]*H
for n in range(N):
  A,B = AB[2*n:2*(n+1)]
  for h in range(1,H+1): dp[h] = min(dp[h],dp[max(0,h-A)]+B)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
MAXH = H+10000
DP = [float("inf")]*MAXH
DP[0] = 0
for i in range(N):
    a,b = map(int,input().split())
    for j in range(MAXH-a):
        if j>H+a:break
        DP[j+a] = min(DP[j+a],DP[j]+b)
print(min(DP[H:]))

Submission status: Time Limit Exceeded

Solution code:
hp, n = map(int, input().split())
aList = [0] * n
mpList = [0] * n
for i in range(n):
  aList[i], mpList[i] = map(int, input().split())

dp = [float('inf')] * (hp+1)
dp[0] = 0
for i in range(n):
  for h in range(hp + 1):
    if h < aList[i]:
      dp[h] = min(mpList[i], dp[h])
    else:
      dp[h] = min(dp[h - aList[i]] + mpList[i], dp[h])

print(dp[hp])

Submission status: Time Limit Exceeded

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

skills=[tuple(map(int,input().split())) for _ in range(n)]

dp=[10**30]*(10**4+1)

dp[hp]=0

for a,b in skills:
    for h in range(hp,0,-1):
        if h-a>=0:
            dp[h-a]=min(dp[h-a],dp[h]+b)
        else:
            dp[0]=min(dp[0],dp[h]+b)
            
print(dp[0])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
h,n = map(int,input().split())
a = [0]*n
b = [0]*n
for i in range(n):
    a[i],b[i] = map(int,input().split())
#  dp[i] i 以上の攻撃力を与えるための最小魔力
b,a = zip(*sorted(zip(b,a))) #魔力でソート
dp = [0]*(h+1)
dp[0] = 0
for i in range(1,h+1):
    dp[i] = 10**12
    for j in range(n):#すべての種類の魔法に対してループ
        if i -a[j] >= 0:#魔法が使えるなら
            dp[i] = min(dp[i],dp[i-a[j]]+b[j])
        else: dp[i] = min(dp[i],b[j])
"""print(dp)
print(a,b)"""
print(dp[h])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
MAXH = H+1
DP = [float("inf")]*MAXH
DP[0] = 0
a = [0]*N
b = [0]*N
for i in range(N):
  a[i],b[i] = map(int,input().split())
for i in range(MAXH):
  if DP[i] == float("inf"):
    continue    
  for j in range(N):
        DP[min(i+a[j],H)] = min(DP[min(i+a[j],H)],DP[i]+b[j])
print(DP[H])

Submission status: Time Limit Exceeded

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

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

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

print(dp[H])

Submission status: Time Limit Exceeded

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

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

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

print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
MAXH = H+10000
DP = [float("inf")]*MAXH
DP[0] = 0
a = [0]*N
b = [0]*N
for i in range(N):
  a[i],b[i] = map(int,input().split())
for i in range(MAXH):
  if DP[i] == float("inf"):
    continue    
  for j in range(N):
        DP[min(i+a[j],H)] = min(DP[min(i+a[j],H)],DP[i]+b[j])
print(DP[H])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
INF = 10 ** 18
H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
dp = [INF for i in range(H + 1)]

dp[0] = 0

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

print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int, input().split())
dp = [0]+[float("inf")]*H
for n in range(N):
  A,B = map(int, input().split())
  for h in range(1,H+1):
    dp[h] = min(dp[h],dp[max(0,h-A)]+B)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

H, N = map(int, input().split())
a = np.zeros((N+1),dtype=np.int16)
b = np.zeros((N+1),dtype=np.int16)
a[0] = 10**9
b[0] = 10**9
dp = np.zeros((N+1,H+1))
dp[0][:] = 10**9

for i in range(N):
    a_i, b_i = map(int, input().split())
    a[i+1] = a_i
    b[i+1] = b_i

    dp[i+1][0] = b_i

for i in range(1,N+1):
    for h in range(1,H+1):
        if h > a[i]:
            dp[i][h] = min(dp[i][h-a[i]]+b[i],dp[i-1][h])
        else:
            dp[i][h] = min(dp[i][0],dp[i-1][h])
print(int(dp[N][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 = [100000000] * (10 ** 5)
dp[0] = 0
for i in range(H):
    if dp[i] == 100000000:
        continue
    for j in range(N):
        a, b = AB[j]
        if dp[i] + b < dp[i + a]:
            dp[i + a] = dp[i] + b
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    H,N=(int(i) for i in input().split())
    magics=[[int(i) for i in input().split()] for j in range(N)]
    mx=2**31
    dpt=[mx for i in range(H+1)]
    dpt[-1]=0
    for i in range(H,0,-1):
        
        current=dpt[i]
        if current> dpt[0]:
            continue
        for m in magics:
            dpt[max(0,i-m[0])]=min(dpt[max(0,i-m[0])],current+m[1])
    print(dpt[0])

if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter as it
from math import ceil

h, n = map(int, input().split(' '))
A = []
B = []
for _ in range(n):
    a,b = map(int, input().split(' '))
    A.append(a)
    B.append(b)
x = max(A) + h
y = [10**20] * x
y[0] = 0
for i in range(h):
    for j,k in zip(A,B):
        if y[i+j] > y[i] + k:
            y[i+j] = y[i] + k
print(min(y[h:]))

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy

hp, n = map(int, input().split())
aList = [0] * n
mpList = [0] * n
eList = [0] * n
for i in range(n):
  aList[i], mpList[i] = map(int, input().split())
  eList = aList[i] / mpList[i]

# E means Efficient
maxEIndex = numpy.argmax(eList)
kariMinMp = math.ceil(hp / aList[maxEIndex]) * mpList[maxEIndex]
dp = [0 for j in range(kariMinMp + 1)]

for i in range(n):
  for m in range(kariMinMp + 1):
    if m < mpList[i]:
      continue
    else:
      dp[m] = max(dp[m - mpList[i]] + aList[i], dp[m])

for (mp, a) in enumerate(dp):
  if hp <= a:
    print(mp)
    break

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    H, N = map(int, input().split())
    a = np.zeros((N+1),dtype=np.int16)
    b = np.zeros((N+1),dtype=np.int16)
    a[0] = 10**9
    b[0] = 10**9
    dp = np.zeros((N+1,H+1))
    dp[0][:] = 10**9

    for i in range(N):
        a_i, b_i = map(int, input().split())
        a[i+1] = a_i
        b[i+1] = b_i

        dp[i+1][0] = b_i

    for i in range(1,N+1):
        for h in range(1,H+1):
            if h > a[i]:
                dp[i][h] = min(dp[i][h-a[i]]+b[i],dp[i-1][h])
            else:
                dp[i][h] = min(dp[i][0],dp[i-1][h])
    print(int(dp[N][H]))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
H,N=map(int,input().split())
A=[]
B=[]
eff=[]
mp=0

for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    eff.append(math.ceil(H/a)*b)

while H>0:
    eff_min_index=eff.index(min(eff))
    tmp=math.floor(H/A[eff_min_index])
    H-=tmp*A[eff_min_index]
    mp+=tmp*B[eff_min_index]
    for i in range(N):
        eff[i]=math.ceil(H/A[i])*B[i]
print(mp)

Submission status: Time Limit Exceeded

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

AB = []
for i in range(N):
    tmp = list(map(int, input().split()))
    AB.append(tmp)
    
dp = [10**9] * (H+1)
dp[0] = 0
    
for i in range(N):
    for j in range(H+1):
        a, b = AB[i]
        if j < a:
            dp[j] = min(dp[0]+b, dp[j])
        else:
            dp[j] = min(dp[j-a]+b, dp[j])
    
print(dp[H])

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    H, N = map(int, input().split())
    a = np.zeros((N+1),dtype=np.int16)
    b = np.zeros((N+1),dtype=np.int16)
    a[0] = 10**9
    b[0] = 10**9
    dp = np.zeros((N+1,H+1))
    dp[0][:] = 10**9

    for i in range(N):
        a_i, b_i = map(int, input().split())
        a[i+1] = a_i
        b[i+1] = b_i

        dp[i+1][0] = b_i

    for i in range(1,N+1):
        for h in range(1,H+1):
            if h > a[i]:
                dp[i][h] = min(dp[i][h-a[i]]+b[i],dp[i-1][h])
            else:
                dp[i][h] = min(dp[i][0],dp[i-1][h])
    print(int(dp[N][H]))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
MAXH = H+10000
DP = [float("inf")]*MAXH
DP[0] = 0
for i in range(N):
    a,b = map(int,input().split())
    for j in range(MAXH):
        if j+a>MAXH-1:break
        DP[j+a] = min(DP[j+a],DP[j]+b)
print(min(DP[H:]))

Submission status: Time Limit Exceeded

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

cost = {}
cost[0] = 0
que = [0]
dmg = [0]*N
mp = [0]*N

for i in range(N):
    dmg[i], mp[i] = list(map(int,input().split()))

while que != [] :
        que.sort()
        target = que.pop(0)
        for i in range(N):
            crnt_dmg = target+dmg[i]
            crnt_cost = cost[target] + mp[i]
            if crnt_dmg <= H: 
                if crnt_dmg in cost:
                    if cost[crnt_dmg] > crnt_cost:
                        cost[crnt_dmg] = crnt_cost
                else :
                    cost[crnt_dmg] = crnt_cost
                    que.append(crnt_dmg)
            else :
                if H in  cost:
                    if cost[H] > crnt_cost:
                        cost[H] = crnt_cost
                else :
                    cost[H] = crnt_cost
    
print(cost[H])

Submission status: Time Limit Exceeded

Solution code:
S, m = map(int, input().split())
dp = [0] + [-1] * (11005)
w = [0] * m
c = [0] * m
for i in range(m):
    a1, a2 = map(int, input().split())
    w[i] = a1
    c[i] = a2
for i in range(m):
    for j in range(0, 11000):
        if w[i] <= j:
            if dp[j - w[i]] != -1:
                if dp[j] == -1:
                    dp[j] = dp[j - w[i]] + c[i]
                else:
                    dp[j] = min(dp[j - w[i]] + c[i], dp[j])
ans = 99756745678876578
for i in range(S, 11000):
    if dp[i] != -1:
        ans = min(ans, dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, n = map(int, input().split())
a = []
b = []
for _ in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
 
dp = [float("inf")] * (2 * 10000 + 1)
dp[0] = 0
for i in range(h):
    for j in range(n):
        if (i + a[j]) > 2 * 10000:
            dp[2 * 10000] = min(dp[2 * 10000], dp[i] + b[j])
            continue
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])
 
ans = float('inf')
for j in range(h, 2 * 10000 + 1):
    ans = min(ans, dp[j])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#E
h, n = list(map(int,input().split()))
lst = []
for i in range(n):
    a,b = list(map(int,input().split()))
    lst.append([a,b,a/b])

lst.sort(key=lambda x: x[2])
asn = 0

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

#なぜかループに入らない、完全にbreakの位置をミスっていた(いらなかった、、)
for i in range(n-1,-1,-1):
    a = lst[i][0]
    b = lst[i][1]
    for j in range(h-1,-1,-1):
        if dp[j] != float("Inf"):
            for k in range(j,h):
                if dp[k] != float("Inf"):
                    if dp[k] + b < dp[min(k+a,h)]:
                        dp[min(k+a,h)] = dp[k] + b
print(dp[h])
#print(dp)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

INF = 10**18

dp = [INF] * (H + 1)
dp[0] = 0
for a, b in AB:
    for n in range(H + 1):
        k = max(0, n - a)
        acc = dp[k] + b
        if acc < dp[n]:
            dp[n] = acc

print(dp[H])

Submission status: Time Limit Exceeded

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

cost = {}
cost[0] = 0
que = [0]
dmg = [0]*N
mp = [0]*N

for i in range(N):
    dmg[i], mp[i] = list(map(int,input().split()))

while que != [] :
        que.sort()
        target = que.pop(0)
        for i in range(N):
            crnt_dmg = target+dmg[i]
            crnt_cost = cost[target] + mp[i]
            if crnt_dmg <= H: 
                if crnt_dmg in cost:
                    if cost[crnt_dmg] > crnt_cost:
                        cost[crnt_dmg] = crnt_cost
                        if crnt_dmg in que:
                            que.remove(crnt_dmg)
                        que.append(crnt_dmg)
                else :
                    cost[crnt_dmg] = crnt_cost
                    que.append(crnt_dmg)
            else :
                if H in  cost:
                    if cost[H] > crnt_cost:
                        cost[H] = crnt_cost
                else :
                    cost[H] = crnt_cost
    
print(cost[H])

Submission status: Time Limit Exceeded

Solution code:
import math

#Input
H, N = map(int, input().split())
lstV = list()
lstC = list()
for i in range(N):
    value, cost = map(int, input().split())
    lstV.append(value)
    lstC.append(cost)

#DPMap
dp = list([2**31-1]*(H + max(lstV)))
dp[0] = 0
for i in range(0,len(dp)):
    for j in range(0,len(lstV)):
        value = lstV[j]
        cost = lstC[j]
        if i + value <= len(dp) - 1:
            dp[i + value] = min(dp[i + value], dp[i] + cost)
print(min(dp[H:]))

Submission status: Time Limit Exceeded

Solution code:
H,N = map(int,input().split())
A = []
for i in range(N):
    a,b = map(int,input().split())
    A.append((a,b))
MAXH = H+10000
DP = [float("inf")]*(MAXH)
DP[0] = 0
for i in range(0,MAXH):
    for a in A:
        if i-a[0]<0:continue
        DP[i] = min(DP[i],DP[i-a[0]]+a[1])
print(min(DP[H:]))