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: Accepted

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: Accepted

Solution code:

h,n=map(int,input().split())
a=[[int(i) for i in input().split()]for i in range(n)]
inf=10**9
dp=[inf]*(h+1) #0-9
dp[0]=0
mini=inf
for i in range(h+1):
    if dp[i]==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)

Submission status: Accepted

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

max_A=max(AB)[0]
DP=[0]+[float("inf")]*(h+max_A)
for i in range(h+1):
    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: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

H, N = MAP()
AB = [()]
for i in range(N):
    a, b = MAP()
    AB.append((a, b))

dp = list2d(N+1, 20007, INF)
dp[0][0] = 0
for i in range(1, N+1):
    a, b = AB[i]
    for j in range(20007):
        dp[i][j] = min(dp[i][j], dp[i-1][j])
        if j-a >= 0:
            dp[i][j] = min(dp[i][j], dp[i][j-a] + b, dp[i-1][j-a] + b)
print(min(dp[N][H:]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import numpy as np
import sys
input = sys.stdin.readline


def main():
    h, n = map(int, input().split())
    # A = []
    # B = []
    # for _ in range(n):
    #     A.append(a)
    #     B.append(b)
        
    m = 2 * 10**4 + 10
    dp = [float("inf")] * (h+1)
    dp[0] = 0
    # for a, b in zip(A, B):
    #     dp[a] = b
    for _ in range(n):
        a, b = map(int, input().split())
        for i in range(h):
            # for a, b in zip(A, B):
            if i + a < m :
                j = min(i+a, h)
                if dp[j] > 0:
                    dp[j] = min(dp[j], dp[i] + b)
                else:
                    dp[j] = dp[i] + b
    k = h
    # print(dp[:20])
    # while (flag):
    #     if dp[k] > 0:
    #         ans = dp[k]
    #         flag = 0
    #     k += 1
    # d = np.array(dp[k:])
    # print(np.min(d[d.nonzero()]))
    print(dp[h])
    
    
main()

Submission status: Accepted

Solution code:
import sys

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

dp = [0 for _ in range(H + max(AB, key=lambda x: x[0])[0])]
for h in range(1, len(dp)):
    dp[h] = min(dp[h-a] + b for a, b in AB)

print(dp[H])

Submission status: Accepted

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

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

dp = [[10**10]*(h+10**4+1) for _ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0

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

print(min(dp[-1][h:]))

Submission status: Accepted

Solution code:
a = list(map(int,input().split()))
HP = a[0]

x = []
for i in range(a[1]):
    x1,y1=[int(i) for i in input().split()]
    x.append((x1,y1))

max_tup = max(x, key = lambda x:x[0])
max_a = max_tup[0]
dp = [0] * (HP + max_a)

for i in range(1,len(dp)):
  dp[i] = min(dp[i-a] + b for a,b in x)

print(dp[HP])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
INF = float('INF')


def main():
    H, N = map(int, input().split())
    skills = [None] * N
    max_a = 0
    for i in range(N):
        a, b = map(int, input().split())
        if a > max_a:
            max_a = a
        skills[i] = (a, b)

    # dpの配列準備
    # インデックス→体力、中身→その体力を削るための最小MP
    dp = [INF] * (H + max_a + 1)
    dp[0] = 0

    # 各体力を削る最小MPをDPで更新していく
    for i in range(H):
        # そのHPをちょうど削る攻撃がない場合はそれ以降の行動を計算できないので飛ばす1
        if dp[i] == INF:
            continue

        # そうでない場合は、次の攻撃をシミュレーション
        for a, b in skills:
            # 攻撃した場合の累計消費MPを算出
            mp = dp[i] + b
            # その攻撃をした場合に削れる体力に割り当てられている消費MPと比較
            # より少ない消費MPで削れることがわかった場合は更新
            if mp < dp[i + a]:
                dp[i + a] = mp

    # 更新が終わったので、倒せるダメージのうち、累計消費MPが一番少ないものを出力
    print(min(dp[H:]))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
magics = [list(map(int, input().split())) for _ in range(N)]
val = 1e+9
mps = [val] * H
mps[0] = 0

for i in range(H):
  for d, c in magics:
    if i + d < H:
      mps[i + d] = min(mps[i] + c, mps[i + d])
    else:
      val = min(mps[i] + c, val)
    
print(val)

Submission status: Accepted

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-1][max(j-A[i], 0)] + B[i],
                       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: Accepted

Solution code:
def e_crested_ibis_vs_monster():
    H, N = [int(i) for i in input().split()]
    Damage, Cost = [], []
    for _ in range(N):
        a, b = [int(i) for i in input().split()]
        Damage.append(a)
        Cost.append(b)

    # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値
    dp = [[float('inf')] * (H + 1) for _ in range(N + 1)]
    dp[0][0] = 0
    for i in range(1, N + 1):
        for j in range(H + 1):
            # i 番目の魔法を使わない場合
            dp[i][j] = min(dp[i][j], dp[i - 1][j])
            # i 番目の魔法を使う場合
            dp[i][min(j + Damage[i - 1], H)] = min(dp[i][min(j + Damage[i - 1], H)],
                                                   dp[i][j] + Cost[i - 1])
    return dp[N][H]

print(e_crested_ibis_vs_monster())

Submission status: Accepted

Solution code:
def e_crested_ibis_vs_monster():
    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())

Submission status: Accepted

Solution code:
from sys import stdin
readline=stdin.readline
h,n=map(int,readline().split())
AB=[list(map(int,readline().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: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, N = lr()

U = 20000 #max(H)+max(A)
# dp[i] はモンスターへi攻撃する時、使用する魔力の最小値
INF = 10 ** 10
dp = np.full(U, INF, np.int64)
dp[0] = 0

for _ in range(N):
    a, b = lr()
    n = (U + (-U)%a) // a
    dp = np.resize(dp, U + (-U)%a).reshape(-1, a)
    # 列のサイズはa
    dp -= (np.arange(n) * b)[:, None]
    dp = np.minimum.accumulate(dp, axis=0)
    # ここでdpを現時点の魔力の最小値に更新
    dp += (np.arange(n) * b)[:, None]
    dp = np.ravel(dp)[:U]

answer = min(dp[H:])
print(answer)

Submission status: Accepted

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

DP = [0] * (H + 1)

for i in range(len(DP)):
    if i == 0 or DP[i] != 0:
        for j in range(N):

            if i + A[j] <= H:
                if DP[i + A[j]] != 0:
                    DP[i + A[j]] = min(DP[i + A[j]], DP[i] + B[j])
                else:
                    DP[i + A[j]] = DP[i] + B[j]

            else:
                if DP[H] == 0:
                    DP[H] = DP[i] + B[j]
                else:
                    DP[H] = min(DP[H], DP[i] + B[j])

print(DP[H])

Submission status: Accepted

Solution code:
#!python3
import sys
sys.setrecursionlimit(1000000)

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)]
    inf = float("inf")
    AB.sort(key=lambda a: (a[0]/a[1], -a[0]), reverse=True)

    def memo(f):
        cache = {}
        def wrapper(*a):
            if a in cache:
                return cache[a]
            b = f(*a)
            cache[a] = b

            return b

        return wrapper

    @memo
    def calc(h):
        if h <= 0: return 0

        bi = inf
        for a, b in AB:
            bj = b + calc(h - a)
            if bi > bj:
                bi = bj
            else:
                break
        return bi

    print(calc(H))



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
magic = [list(map(int, input().split())) for _ in range(N)]
# i番目までの魔法を使った時にjを削るために必要な最小のコスト
dp = [[10**10]*(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+magic[i][0], H)] = min(dp[i+1][min(j+magic[i][0], H)], dp[i+1][j] + magic[i][1])
print(dp[N][H])

Submission status: Accepted

Solution code:
def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    max_A = max(a for a, _ in AB)
    inf = 10 ** 9
    dp = [inf] * (H + max_A+ 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == inf:
            continue
        for a, b in AB:
            t = dp[i] + b
            if t < dp[i + a]:
                dp[i + a] = t
    print(min(dp[H:]))


main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,n = inpl()
att = []
cost = []
for i in range(n):
    a,b = inpl()
    att.append(a); cost.append(b)
dp = [[INF for i in range(h+1)] for j in range(n+1)]
dp[0][0] = 0

for i in range(n):
    cnt = h//att[i]
    for j in range(h+1):
        if att[i] >= j:
            dp[i+1][j] = min(dp[i][j], cost[i])
            continue
        dp[i+1][j] = min(dp[i][j], cost[i]+dp[i+1][j-att[i]])
# pprint.pprint(dp)
print(dp[n][h])

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
l = []
for i in range(n):
    a, b = map(int, input().split())
    l.append([a, b])
INF = 10 ** 9 + 11
dp = [INF] * (h + 1)
dp[0] = 0
nextj = 0
for i in range(n):
    for j in range(h):
        nextj = j + l[i][0]
        if nextj <= h:
            dp[nextj] = min(dp[nextj], dp[j] + l[i][1])
        else:
            dp[h] = min(dp[h], dp[j] + l[i][1])
print(dp[-1])

Submission status: Accepted

Solution code:
def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    max_A = max(a for a, _ in AB)
    dp = [100000000] * (H + max_A+ 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for a, b in AB:
            t = dp[i] + b
            if t < dp[i + a]:
                dp[i + a] = t
    print(min(dp[H:]))


main()

Submission status: Accepted

Solution code:
import numpy as np
h, N = map(int,input().split())
VW = [[int(x) for x in input().split()] for _ in range(N)]
VW = np.array(sorted(VW, key=lambda x:(x[0]/x[1],x[0])))
a = VW[:, 0]
b = VW[:, 1]

dp = np.zeros(10**4+1, dtype=np.int)
for i in range(1, h+1):
    dp[i] = (dp[i-a]+b).min()
print(dp[h])

Submission status: Accepted

Solution code:
import numpy as np
 
h, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab = np.array(ab)
 
a = ab[:, 0]
b = ab[:, 1]
 
dp = np.zeros(100001, dtype=np.int)
for i in range(1, h+1):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
magi=[list(map(int,input().split())) for i in range(n)]
magi.sort(reverse=True)
infi=10**9
dp=[infi]*(2*10**4)
dp[0]=0
for a,b in magi:
  for i in range(h+1):
    if dp[i]!=infi:
      dp[i+a]=min(dp[i]+b,dp[i+a])
print(min(dp[h:]))

Submission status: Accepted

Solution code:
import math
INF = 1001001001
h, n = map(int, input().split())
ab = list(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):
        nj = min(j+ab[i][0], h)
        dp[nj] = min(dp[nj], dp[j]+ab[i][1])

print(dp[h])

Submission status: Accepted

Solution code:
# 解き直し
H, N = map(int, input().split())
A_to_minB = {}
max_A = -1
for i in range(N):
    a, b = map(int, input().split())
    max_A = max(max_A, a)
    if a not in set(A_to_minB.keys()):
        A_to_minB[a] = b
    else:
        A_to_minB[a] = min(A_to_minB[a], b)
dp = [float("inf")] * (H + max_A)
dp[0] = 0
for i in range(1, H+max_A):
    tmp = [dp[i-a]+b for a, b in A_to_minB.items() if a<=i]
    if tmp:
        dp[i] = min(tmp)
print(min(dp[H:H+max_A]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab = np.array(ab)

a = ab[:, 0]
b = ab[:, 1]

dp = np.zeros(100001, dtype=np.int)
for i in range(1, h+1):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Submission status: Accepted

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

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

mp = [float("inf") for i in range(h+1)]
mp[0] = 0

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

print(mp[-1])

Submission status: Accepted

Solution code:
import sys
from collections import deque, defaultdict
from itertools import combinations, permutations
from heapq import heapify, heappop, heappush

def solve():
    input = sys.stdin.readline
    H, N = map(int, input().split())
    M = [tuple(int(a) for a in input().split()) for _ in range(N)]
    DP = [1000000000] * (H + 2) #ダメージiを与えるのに必要な最小MP, DP[H+1]はH以上のダメージを与えた場合
    DP[0] = 0
    for i, m in enumerate(M):
        a, b = m
        for j in range(max(H-a+1, 0)): DP[j + a] = min(DP[j + a], DP[j] + b)
        for j in range(max(H-a+1, 0), H): DP[H + 1] = min(DP[H + 1], DP[j] + b)
    print(min(DP[H], DP[H + 1]))

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

md = {}
max_d = 0
for _ in range(N):
    d, m = [int(i) for i in input().split()]
    if m not in md:
        md[m] = 0
    md[m] = max(md[m], d)
    max_d = max(max_d, d)

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

for i in range(1, H + max_d + 1):
    for m, d in md.items():
        if 0 <= i - d <= H + max_d:
            dp[i] = min(dp[i], dp[i-d] + m)
ans = min(dp[H:])
print(ans)

Submission status: Accepted

Solution code:
import collections
import time


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

INF = 10 ** 20

dp = collections.defaultdict(lambda : INF)
dp[0] = 0

magic = []

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


t = time.time()

que = [0]
cc = [0]
m = 0
c = 0
while que:
    next_que = set()
    for now_dam in que:
        for a, b in magic:
            dam = now_dam + a
            if dp[dam] > dp[now_dam] + b:
                dp[dam] = dp[now_dam] + b
                if h > dam:
                    next_que.add(dam)
    que = list(next_que)
ans = INF
# print(time.time() - t)

for k, v in dp.items():
    if k >= h:
        ans = min([v, ans])

print(ans)

# print(time.time() - t)

Submission status: Accepted

Solution code:
import collections
import time

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

    INF = 10 ** 20

    dp = collections.defaultdict(lambda : INF)
    dp[0] = 0

    magic = []

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


    t = time.time()

    que = [0]
    cc = [0]
    m = 0
    c = 0
    while que:
        next_que = set()
        for now_dam in que:
            for a, b in magic:
                dam = now_dam + a
                if dp[dam] > dp[now_dam] + b:
                    dp[dam] = dp[now_dam] + b
                    if h > dam:
                        next_que.add(dam)
        que = list(next_que)
    ans = INF
    # print(time.time() - t)

    for k, v in dp.items():
        if k >= h:
            ans = min([v, ans])

    print(ans)

    # print(time.time() - t)

main()

Submission status: Accepted

Solution code:
import time

def main():
    h, n = map(int, input().split())
    INF = 10 ** 9
    magic = []

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

    # t = time.time()

    magic.sort(reverse=True, key=lambda x: x[0])

    dp = [INF] * (h + 10 ** 4)
    dp[0] = 0

    for a, b in magic:
        for i in range(h):
            if dp[i] != INF:
                tmp = dp[i] + b
                if tmp < dp[i + a]:
                    dp[i + a] = tmp

    ans = min(dp[h:])

    print(ans)
    # print(time.time() - t)

main()

Submission status: Accepted

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: Accepted

Solution code:
from sys import stdin

h, n = map(int, stdin.readline().split())
attack = [None] * n
cost = [None] * n
for i in range(n):
    a, c = map(int, stdin.readline().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])

Submission status: Accepted

Solution code:
def main():
    inf = 10**9
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))
 
 
main()

Submission status: Accepted

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

f_inf = float("inf")
dp = [f_inf] * (h + max(a))
dp[0] = 0
for i in range(n):
    for j in range(h):
        if dp[j] != f_inf:
            if dp[j + a[i]] > dp[j] + b[i]:
                dp[j + a[i]] = dp[j] + b[i]

print(min(dp[h:]))

Submission status: Accepted

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

Submission status: Accepted

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

max_a = max(a for a, _ in ab)
dp = [0] * (h+max_a)

for i in range(0, h+max_a):
    if i <= 0:
        dp[i] = 0
    else:
        dp[i] = min(dp[i-a] + b for a,b in ab) 

print(min(dp[h:]))

Submission status: Accepted

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

max_a = max(a for a, _ in ab)
dp = [0] * (h + max_a)

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

Submission status: Accepted

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

max_a = max(a for a, _ in ab)
dp = [0] * (h + max_a)

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

Submission status: Accepted

Solution code:
import sys

def input() -> str:
    return sys.stdin.readline().rstrip()

h, n = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]
m = max(a for a, _ in ab)
dp = [0] * (h + m * 2 + 1)
for i in range(-m, h + m + 1):
    if i <= 0:
        dp[i] = 0
    else:
        dp[i] = min(dp[i - a] + b for a, b in ab)
print(min(dp[h:h + m + 1]))

Submission status: Accepted

Solution code:
# E - Crested Ibis vs Monster
def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  # dp[i] := min magic points to decrease monster's health by i
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H):
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# E - Crested Ibis vs Monster
def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  # dp[i] := min magic points to decrease monster's health by i
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H):
            dp[i + a] = min(dp[i + a], dp[i] + b)
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# E - Crested Ibis vs Monster
def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  # dp[i] := min magic points to decrease monster's health by i
    dp[0] = 0
    for i in range(H):
        for a, b in zip(*[iter(AB)] * 2):
            dp[i + a] = min(dp[i + a], dp[i] + b)
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# E - Crested Ibis vs Monster
def main():
    INF = 1 << 30

    H, N, *AB = map(int, open(0).read().split())
    dp = [INF] * (H + 10001)  # dp[i] := min magic points to decrease monster's health by i
    dp[0] = 0
    for i in range(H):
        if dp[i] == INF:
            continue
        for a, b in zip(*[iter(AB)] * 2):
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# E - Crested Ibis vs Monster
from collections import defaultdict


def main():
    H, N, *AB = map(int, open(0).read().split())
    dp = defaultdict(lambda: 1 << 30)  # dp[i] := min magic points to decrease monster's health by i
    dp[0] = 0
    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H):
            if i not in dp:
                continue
            dp[i + a] = min(dp[i + a], dp[i] + b)
    ans = min(magic for health, magic in dp.items() if health >= H)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
ab = tuple(tuple(map(int, input().split())) for _ in range(n))

dp = [10 ** 20] * 10 ** 4 * 2
dp[0] = 0
for i in range(h):
    for a, b in ab:
        dp[i + a] = min(dp[i + a], dp[i] + b)
print(min(dp[h:]))

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
ab = tuple(tuple(map(int, input().split())) for _ in range(n))

dp = [10 ** 20] * 10 ** 4 * 2
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: Accepted

Solution code:
import sys

stdin = sys.stdin

mod = 1000000007
inf = 1 << 60

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

h, n = na()
ab = [na() for i in range(n)]
max_a = max([ab[i][0] for i in range(n)])
dp = [inf] * (h + max_a)
dp[0] = 0

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

print(dp[h])

Submission status: Accepted

Solution code:
from sys import stdin

h, n = (int(v) for v in stdin.readline().split())
spells = [tuple(int(v) for v in stdin.readline().split()) for i in range(n)]

# ~ print(spells)

costs = [0]
for i in range(1, h + 1):
    lowest_cost = None
    for damage, cost in spells:
        if damage < i:
            total_cost = cost + costs[i - damage]
        else:
            total_cost = cost
        if lowest_cost is None or total_cost < lowest_cost:
            lowest_cost = total_cost
    costs.append(lowest_cost)

print(costs[h])

Submission status: Accepted

Solution code:
import numpy as np

h, n = map(int, input().split())
a = []
b = []
for _ in range(n):
  ai, bi = map(int, input().split())
  a.append(ai)
  b.append(bi)
a = np.array(a)
b = np.array(b)
dp = np.zeros(h+1, dtype=np.int)
for c in range(1, h+1):
  dp[c] = np.min(dp[np.maximum(0, c-a)] + b)
print(dp[-1])

Submission status: Accepted

Solution code:
def main():
    inf = 10**9
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))


main()

Submission status: Accepted

Solution code:

     

import math



H,N = map(int,input().split())
Damage = []
Cost = []


DP = [0] + [pow(10,9)] * (H+1)



for i in range(N):
    a,b = map(int,input().split())
    Damage.append(a)
    Cost.append(b)
    
#print(DP)


# DP[i] = min magic points to do i damage

for i in range(1,len(DP)):
    for spell in range(N):
        DP[i] = min(DP[i],DP[max(0,i-Damage[spell])]+Cost[spell])




print (DP[H])

Submission status: Accepted

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

#dp[i] -> HPをi減らすのに必要な魔力の最小値
dp = [10 ** 9 + 7 for i in range(h + max_a)]
dp[0] = 0
for i in range(h + max_a):
    for j in magics:
        dp[i] = min(dp[i], dp[max(i - j[0], 0)] + j[1])

print(dp[h])

Submission status: Accepted

Solution code:
inf = 10 ** 9


def main():
    H, N = map(int, input().split())
    AB = []
    for n in range(N):
        a, b = map(int, input().split())
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            # if dp[h] == inf:
            #     continue
            # else:
            #     dp[h+a] = min(dp[h]+b, dp[h+a])
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print(min(dp[H:]))


main()