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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

H,N = inputIL()
M = inputILL(N)


cospa = max(a/b for a,b in M)

dp = [[0 for j in range(H+1)] for i in range(N)]
a0, b0 = M[0]
dp[0] = [((j+(a0-1))//a0)*b0 for j in range(H+1)]

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000) # 再帰上限を増やす

def main():
    input = sys.stdin.readline  # 文字列に対してinputした場合は、rstripするのを忘れずに！
    H, N = map(int, input().rstrip().split())
    magic_list = []
    for _ in range(N):
        A, B = map(int, input().rstrip().split())
        magic_list.append((A, B))

    # dp[hp] = hpのダメージを与えるための最小魔力
    dp = [100000000] * (H+1)
    # 0のダメージを与えるのに必要な最小魔力は0
    dp[0] = 0
    # 各魔法に関して探索
    for i, magic in enumerate(magic_list):
        damage, mp = magic
        # 各ダメージを与えるのに必要な最小魔力を更新する
        # (与えられないダメージ数の場合は最大値で初期化してあるので、最大値のまま更新されない)
        for hp in range(H+1):
            # 魔法を使用する場合のダメージについて、必要魔力が現在計算されている中での最小値であれば更新
            dp[min(H, hp+damage)] = min(dp[min(H, hp+damage)], dp[hp] + mp)
    print(dp[H])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000) # 再帰上限を増やす

def main():
    input = sys.stdin.readline  # 文字列に対してinputした場合は、rstripするのを忘れずに！
    H, N = map(int, input().rstrip().split())
    magic_list = []
    for _ in range(N):
        A, B = map(int, input().rstrip().split())
        magic_list.append((A, B))

    dp = [[100000000] * (H+1) for _ in range(N+1)]
    # dp[i][hp] = i番目までの魔法を使って、hpのダメージを与えるための最小魔力
    dp[0][0] = 0
    for i, magic in enumerate(magic_list):
        damage, mp = magic
        for hp in range(H+1):
            # i番目までの魔法を使って、与えられるダメージの最小魔力の結果をコピー
            dp[i+1][hp] = min(dp[i+1][hp], dp[i][hp])
            # 魔法を使用する場合、i+1番目までの魔法を使って、HP+damageのダメージの魔力最小値であれば更新
            dp[i+1][min(H, hp+damage)] = min(dp[i+1][min(H, hp+damage)], dp[i+1][hp] + mp)
    print(dp[N][H])

if __name__ == '__main__':
    main()

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, b in ab)
dp = [0]*(H+max_a)
for i in range(1, H+max_a):
    dp[i] = min(dp[i-a]+b for a, b in ab)
print(dp[H])

Submission status: Accepted

Solution code:
import numpy as np

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

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

ab = np.array(ab)
a_list = ab[:, 0]
b_list = ab[:, 1]
max_a = ab.max()

inf = float('inf')
dp = np.array([inf for _ in range(H + max_a)])
dp[0] = 0

for i in range(1, len(dp)):
    dp[i] = np.amin(dp[np.maximum(i - a_list, 0)] + b_list)

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

Submission status: Accepted

Solution code:
m=10**9+7

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=[m]*(h+max(a)+1)
dp[0]=0
for i in range(1,h+max(a)+1):
    k=0
    for j in range(n):
        if i< a[j]:
            k=b[j]
        else:
            k=b[j]+dp[i-a[j]]
        dp[i]=min(k,dp[i])

ans=min(dp[h:h+max(a)+1])

print(ans)

Submission status: Accepted

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

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

dp=np.zeros(10**4+1,int) 
dp[0]=0

for a,b in ab:
      dp[1:a+1]=np.minimum(dp[1:a+1],b)

for i in range(1,h+1):
  dp[i]=np.min(dp[i-ab[:,0]]+ab[:,1])
print(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, b in ab)
dp = [0]*(H+max_a)
for i in range(1, H+max_a):
    dp[i] = min(dp[i-a]+b for a, b in ab)
print(dp[H])

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import math
from collections import deque


def resolve():
    hp, n = map(int, input().split())
    magics = [list(map(int, input().split())) for i in range(n)]

    max_magic_power = 0
    # min_magic_power = 99999

    for magic in magics:
        max_magic_power = max(max_magic_power, magic[0])
        # min_magic_power = min(min_magic_power, magic[0])

    # dp作成[与えることができる最大ダメージ]×2(配列再利用と呼ばれる DP 時のメモリ消費を抑えるテクニックを使うので、2つで十分)
    # dp = [[0 for i in range(hp + max_magic_power)] for j in range(2)]

    # 想定できる最大魔法実施回数(HP / 一番弱い魔法)
    # max_cnt = math.ceil(hp / min_magic_power)

    # for cnt, i in enumerate(range(max_cnt)):
    #     now = cnt % 2
    #     bef = (cnt + 1) % 2
    #
    #     for magic in magics:
    #         dp[now][]

    # ダメージ量ごとに処理を行うため、キューを作成
    que = deque()

    ans = [999999999 for i in range(hp + max_magic_power + 1)]
    ans[0] = 0

    # # 初期データを格納(ダメージ0,消費MP0)
    # que.append([0, 0])
    #
    # while len(que) is not 0:
    #     damage, use_mp = que.pop()
    #
    #     # HP以上のダメージを与え終えた場合、何もせず終了
    #     if damage >= hp:
    #         continue
    #
    #     for magic in magics:
    #         # 各魔法を使用した結果をシュミレーション
    #         work_damage = damage + magic[0]
    #         work_use_mp = use_mp + magic[1]
    #
    #         # 消費MPが少ないパターンを発見できた場合
    #         if ans[work_damage] > work_use_mp:
    #             # 結果を更新
    #             ans[work_damage] = work_use_mp
    #             # キューに追加(継続して検証する)
    #             que.append([work_damage, work_use_mp])
    #
    # print(min(ans[hp-1:hp + 10000]))

    # HPの数だけループすればOK(残りHP1で魔法の威力10・・とかも、これで検証できるので。
    for i in range(hp + 1):
        # 与えることができないパターンのダメージなら、何もしない。
        if ans[i] is 999999999:
            continue

        # 与えることができるパターンのダメージなら、さらに、全魔法を打ち込んだ場合を検証。
        for magic in magics:
            # ans[合計ダメージ量] = min(ans[合計ダメージ量] , ans[蓄積ダメージ量] + 今回の魔法の消費HP
            # ※ans[]は、各ダメージ量の場合の、最小消費MPを保持する。
            ans[i + magic[0]] = min(ans[i + magic[0]], ans[i] + magic[1])

    print(min(ans[hp:-1]))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """9 3
8 3
4 2
2 1"""
        output = """4"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """100 6
1 1
2 3
3 9
4 27
5 81
6 243"""
        output = """100"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461"""
        output = """139815"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """10000 1
1 10000
"""
        output = """100000000"""
        self.assertIO(input, output)



if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

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

for i in range(h+1):
    a = dp[i]
    for attack, magic in magics:
        next_index = min(i+attack, h)
        dp[next_index] = min(dp[next_index], a + magic)

print(dp[h])

Submission status: Accepted

Solution code:
h,n = map(int, input().split())
max_h = 10**4
inf = 10**30
info = []
for i in range(n):
    info.append(list(map(int, input().split())))

import math

mp_sort = sorted(info, key = lambda x : x[1])
damage_sort = sorted(info, key = lambda x : x[0], reverse = True)
max_damage = damage_sort[0][0]
'''
efficiency = [0]*n
for i in range(n):
    efficiency[i] = mp_sort[i][0]/mp_sort[i][1]

max_efficiency = max(efficiency)
max_eff_index = efficiency.index(max_efficiency)

max_eff_attack = mp_sort[max_eff_index][0]
max_eff_mp = mp_sort[max_eff_index][1]
'''
dp = [inf]*(max_h + max_damage + 1)
dp[0] = 0
for i in range(n):
    min_mp = info[i][1]
    tem_damage = info[i][0]
    for j in range(tem_damage+1):
        dp[j] = min(dp[j], min_mp)

#eff_fre = math.ceil(h / max_eff_attack)
for i in range(h):
    for j in range(n):
        tem = i + info[j][0]
        if tem <= h:
            dp[tem]= min(dp[tem], dp[i] + info[j][1])

print(dp[h])

Submission status: Accepted

Solution code:
def solve(h, n, A, B):
    dp = [float("inf")] * h + [0]
    for i in range(h, -1, -1):
        if dp[i] == float("inf"):
            continue
        for j in range(n):
            if 0 <= i - A[j]:
                dp[i - A[j]] = min(dp[i - A[j]], dp[i] + B[j])
            else:
                dp[0] = min(dp[0], dp[i] + B[j])
    return dp[0]

if __name__ == '__main__':
    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())

    print(solve(h, n, a, b))

Submission status: Accepted

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

    INF = 1 << 30

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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
magics = [list(map(int, input().split())) for _ in range(n)]
max_damage = max(a for a, b in magics)
dp = [0] * (h + max_damage)
for i in range(1, h + max_damage):
    dp[i] = min(dp[i - a] + b for a, b in magics)
print(dp[h])

Submission status: Accepted

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

ab_input = [list(map(int,input().split())) for i in range(n)]
max_d = max(a for a,b in ab_input)
#配るdp
dp =[0]*(h+max_d)
for i in range(1,h+max_d):
    dp[i]=min(dp[i-a]+b for a,b in ab_input)
print(dp[h])

Submission status: Accepted

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

A=np.array(A)
B=np.array(B)
dp=np.ones((H+1+10**4))*np.inf
dp[H+1:]=0
dp[0]=0
for i in range(1,H+1):
    
    dp[i]=min(dp[i-A]+B)

print(int(dp[H]))
#print(dp)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc153/tasks/abc153_e
# 個数無制限ナップサック問題

H, N = read_ints()
A, B = read_col(N)


'''
dp[i][j] ... 与えられるダメージの最大。[0,i)の魔法で、魔力j以下使うとき。
更新則
dp[i+1][j] = max(dp[i][j],dp[i+1,j-B[i]] + A[i])
∵i番目を使わないとき、i番目の魔法をもう一回使うとき でダメージの最大の方を選択する
初期条件
dp[0][0] = 0 #なくても大丈夫そう


これだとjが大きすぎてTLEする
'''

'''
dp[i][j] ... 魔力の最小値。[0,i)の魔法で、ちょうどダメージjにできるとき。ちょうどjにできなければINF
更新則

dp[i+1][j] = min(dp[i][j],dp[i+1,j-A[i]] + B[i]) で良さそうじゃない？
∵i番目を使わないとき、i番目の魔法をもう一回使うとき でダメージの最大の方を選択する
初期条件
dp[i][0] = 0
'''


J = max(2 * H + 1, 2 * max(B))
dp = [[INF] * (J) for _ in range(N + 1)]  # Hの二倍も取れば十分かな？
for i in range(N + 1):
    dp[i][0] = 0

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

ans = INF
for x in dp[N][H:]:
    ans = min(ans, x)

print(ans)

Submission status: Accepted

Solution code:
H,N = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
A.insert(0,[0,0])
INFTY = 10**9
dp = [[INFTY for _ in range(H+1)] for _ 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] = dp[i-1][j]
        dp[i][j] = min(dp[i][j],dp[i][max(0,j-A[i][0])]+A[i][1])
print(dp[N][H])

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)
M=[]
M.append(0)
for i in range(h):
  M.append(10000000000000)
for i in range(0,h+1):
  for j in range(n):
    c=min(h,a[j]+i)
    if M[c]>M[i]+b[j]:
      M[c]=M[i]+b[j]
print(M[h])

Submission status: Accepted

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

DP=[float('inf')]*(H+1)
DP[0]=0
for i in range(H):
    for a,b in M:
        DP[min(i+a,H)] = min(DP[min(i+a,H)],DP[i]+b)
print(DP[-1])

Submission status: Accepted

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

DP=[float('inf')]*(H+1)
DP[0]=0
for i in range(H):
    for a,b in M:
        DP[min(i+a,H)] = min(DP[min(i+a,H)],DP[i]+b)
print(DP[-1])

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    h, n = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(n)]

    dp = [f_inf] * (h + 1)
    dp[0] = 0
    for a, b in AB:
        for i in range(1, h + 1):
            dp[i] = min(dp[i], dp[max(0, i - a)] + b)

    print(dp[h])


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
magics = [tuple(map(int, input().split())) for _ in range(N)]
maxA = max([x for x, y in magics])

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

for i in range(1, H+maxA+1):
    for j in range(N):
        a, b = magics[j]
        if a<=i:
            dp[i] = min(dp[i-a]+b, dp[i])

print(min(dp[H:]))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    H, N = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    dp = [[10**9]*(H+1) for _ in range(N+1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(H+1):
            nH = max(0, j-AB[i][0])
            dp[i+1][j] = min(dp[i][j], dp[i+1][j],
                             dp[i+1][nH] + AB[i][1])
    ans = 10**9
    for i in range(N+1):
        ans = min(ans, dp[i][H])
    print(ans)
    # print(*dp, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, n = map(int, readline().split())
ab = [list(map(int, readline().split())) for _ in range(n)]
inf = float('inf')
dp = [inf] * (h + 1)
dp[0] = 0
for a, b in ab:
    for j in range(h):
        if dp[j] != inf:
            if a + j > h:
                dp[-1] = min(dp[-1], dp[j] + b)
            else:
                dp[a + j] = min(dp[a + j], dp[j] + b)
print(dp[-1])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
H, N = map(int, input().split())
dp = [float("inf")] * (H + 1)
dp[H] = 0
for _ in range(N):
  a, b = map(int, input().split())
  for x in range(H, -1, -1):
    dp[max(0, x - a)] = min(dp[max(0, x - a)], dp[x] + b)
print(dp[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    H,N = map(int,input().split())
    attack = []
    mp = []
    for i in range(N):
        a,b = map(int,input().split())
        attack.append(a)
        mp.append(b)
    
    max_attack = max(attack)
    dp = [[float('inf')] * (H+max_attack) for _ in range(N+1)]

    for i in range(N+1):
        dp[i][0] = 0
    
    for i in range(N):
        for j in range(H+max_attack):
            if j - attack[i] >= 0:
                dp[i+1][j] = min(dp[i+1][j], dp[i][j-attack[i]]+mp[i], dp[i+1][j-attack[i]]+mp[i])
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
    
    ans = float('inf')
    for i in range(N+1):
        for j in range(H,H+max_attack):
            ans = min(ans, dp[i][j])
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def solve():
    H,N = map(int,input().split())
    attack = []
    mp = []
    for i in range(N):
        a,b = map(int,input().split())
        attack.append(a)
        mp.append(b)
    
    max_attack = max(attack)
    dp = [[float('inf')] * (H+max_attack) for _ in range(N+1)]

    for i in range(N+1):
        dp[i][0] = 0
    
    for i in range(N):
        for j in range(H+max_attack):
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
            if j - attack[i] >= 0:
                dp[i+1][j] = min(dp[i+1][j], dp[i][j-attack[i]]+mp[i], dp[i+1][j-attack[i]]+mp[i])
    
    ans = float('inf')
    for i in range(N+1):
        for j in range(H,H+max_attack):
            ans = min(ans, dp[i][j])
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    h, n = map(int, stdin.readline().split())
    ab = []
    max_a = 0
    for _ in range(n):
        a, b = map(int, stdin.readline().split())
        if a > max_a:
            max_a = a
        ab.append([a / b, a, b])

    ab.sort(reverse=True)

    dp = [0] * (h + max_a)

    for i in range(h + max_a):
        dp[i] = (i // ab[0][1] + 1) * ab[0][2]

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

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


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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 = [float("inf")] * (H + max(A))
dp[0] = 0
for i in range(H):
    for a, b in zip(A, B):
        if dp[i] + b < dp[i+a]:
            dp[i+a] = dp[i] + b
print(min(dp[H:]))

Submission status: Accepted

Solution code:
import numpy as np
h, n = map(int, input().split())
attack = [0] * n
magic = [0] * n
#ab = np.array([list(map(int, input().split())) for _ in range(n)])
for i in range(n):
    a, b = map(int, input().split())
    attack[i] = a
    magic[i] = b
attack = np.array(attack)
mgic = np.array(magic)

lim = 0
dp = [lim] * (h + 1)
dp = np.array(dp)
dp[0] = 0
# dp; 体力iに対するCost Accumulation
#
# np.zeros > 0埋め
# np.ones >　１埋め
# np.full > 　任意の埋め

for i in range(1, h + 1):
    temp = dp[np.maximum(i - attack, 0)] + magic
    # 1ステップ前のAccumulationに、Magicを足す。
    dp[i] = np.min(temp)

print(dp[h])

Submission status: Accepted

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

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

for j in range(0,H+1,1):#初期化
    dp[N-1][j]=B[N-1]*math.ceil(j/A[N-1])

for i in range(0,N,1):#初期化２
    dp[i][0]=0

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

print(dp[0][H])

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 = [[float("inf")]*(h+1) for i in range(n+1)]
for i in range(n+1):
    dp[i][0] = 0
for i in range(n):
    for j in range(h):
        if j+1-a[i] >= 0:
            dp[i+1][j+1] = min(dp[i][j+1], dp[i][j]+b[i], dp[i+1][j+1-a[i]]+b[i])
        else:
            dp[i+1][j+1] = min(dp[i][j+1], dp[i][j]+b[i], b[i])

print(dp[n][h])

Submission status: Accepted

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

DP = [float('inf')] * (H + 1)
DP[0] = 0
for i in range(H):
    for j in range(N):
        d = min(H, i + Attack[j])
        DP[d] = min(DP[d], DP[i] + Magic[j])

print(DP[H])

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

H,N = myinput()
AB = mylistinput(N)

# dp[i]: 敵の体力をiだけ減らすとしたときの，消耗魔力累計値の最小値
dp = [float("inf")]*(H+1)
dp[0] = 0

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

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

H,N = myinput()
AB = mylistinput(N)

AB_sorted = mysort(AB,0,True)
# pprint(AB_sorted)
maxA = AB[0][0]
# print(maxA)
HA = H+maxA

# dp[i]: 敵の体力をiだけ減らすとしたときの，消耗魔力累計値の最小値
dp = [float("inf")]*(H+1)
dp[0] = 0

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

Submission status: Accepted

Solution code:
import numpy as np
h,n=map(int,input().split())
ab=np.array([list(map(int,input().split())) for _ in range(n)])
dp=np.zeros(10**4+1,np.int64) #dp[i]:残り体力がiのとき、倒すまでの最小魔力
dp[0]=0
for a,b in ab:
      dp[1:a+1]=np.minimum(dp[1:a+1],b)
# a:減らせる体力
# b:魔力
for i in range(1,h+1):
  dp[i]=np.min(dp[i-ab[:,0]]+ab[:,1])
print(dp[h])

Submission status: Accepted

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

DP=[float("inf")]*(h+max(AB)[0]+1)
DP[0]=0
for i in range(h+1):
    for a,b in AB:
        DP[i+a]=min(DP[i+a],DP[i]+b)

print(min(DP[h:]))

Submission status: Accepted

Solution code:
inf = 10**15
mod = 10**9+7
h,n = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
dp = [[inf for j in range(h+1)] for i in range(n+1)]
for i in range(n):
    dp[i][0] = 0
    a,b = ab[i]
    for j in range(h):
        if j+1-a>=0:
            dp[i+1][j+1] = min(dp[i+1][j+1-a]+b, dp[i][j+1], dp[i][j+1-a]+b)
        else:
            dp[i+1][j+1] = min(dp[i][j+1], b)
print(dp[n][h])

Submission status: Accepted

Solution code:
h, n = (int(x) for x in input().split())
magic_list = []
for i in range(n):
    a, b = (int(x) for x in input().split())
    taple = (a,b)
    magic_list.append(taple)
ans = 0
mp=[]
for i in range(1,h+1):
    min_mp = 10000000000
    for j in range(n):
        if i<=magic_list[j][0]:
            use_mp = magic_list[j][1]    
        else:
            use_mp = mp[i-magic_list[j][0]-1]+magic_list[j][1]
        if use_mp<min_mp:
            min_mp = use_mp
    mp.append(min_mp)
    if i==h:
        ans=min_mp
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

INF = float("inf")


def main():
    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 = [INF] * (H + max(A))
    dp[0] = 0
    for h in range(H):
        for a, b in zip(A, B):
            if dp[h]+b < dp[h+a]:
                dp[h+a] = dp[h]+b
    ans = min(dp[H:])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
 
magic = [tuple(map(int, input().split())) for _ in range(N)]
 
DP = [0] * (H+10**4)
 
for h in range(1, H+1):
  DP[h] = min(DP[h - damage] + mp for damage, mp in magic)
print(DP[H])

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(1,h+1):
    s = 100000000000
    for j in range(n):
        if i - a[j] < 0:
            s = min(s,b[j])
        else:
            s = min(s,dp[i-a[j]] + b[j])
    dp[i] = s
print(dp[h])

Submission status: Accepted

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

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

DP = [0] * (H+10**4)

for h in range(1, H+1):
  DP[h] = min(DP[h - damage] + mp for damage, mp in magic)
print(DP[H])