Problem:


Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815



Sample input:
9 3
8 3
4 2
2 1


Sample output:
4

Submission status: Wrong Answer

Solution code:
[H,N]=[int(i) for i in input().split()]
DP=[100000000 for i in range(H)]
DP[0]=0
for n in range(N):
  [A,B]=[int(i) for i in input().split()]
  for h in range(N):
    if h-A>-1 and DP[h-A]+B<DP[h]:DP[h]=DP[h-A]+B
print(DP[H-1])

Submission status: Wrong Answer

Solution code:
[h,n] = [int(i) for i in input().split()]
ab = []
for i in range(n):
    ab.append([int(j) for j in input().split()])
dp =[0] * 10001 + [10 ** 10] * 10000
for i in range(10001,20000):
    for ls in ab:
        dp[i] = min(dp[i],dp[i-ls[0]]+ls[1])
print(dp[10000+h])

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7

H, N = MAP()
A, B = [0]*N, [0]*N
for i in range(N):
	A[i], B[i] = MAP()

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


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

#for i in range(N+1):
#	print(dp[i])

print(dp[N][H])

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7

H, N = MAP()
A, B = [0]*N, [0]*N
for i in range(N):
	A[i], B[i] = MAP()

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


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

#for i in range(N+1):
#	print(dp[i])

print(dp[N][H])

Submission status: Wrong Answer

Solution code:
def dp(H, N, a, b):
  max_a = max(a)
  d = [INF for _ in range(H + max_a)]
  d[0] = 0
  for h in range(1, H + max_a):
    min_mp = INF
    for i in range(N):
      if h - a[i] >= 0:
        min_mp = min(min_mp, d[h - a[i]] + b[i])
  return min([d[H + h] for h in range(max_a)])
INF = 10 ** 10
H, N = map(int, input().split())
a = [0 for _ in range(N)]
b = [0 for _ in range(N)]
for i in range(N):
  ai, bi = map(int, input().split())
  a[i] = ai
  b[i] = bi
print(dp(H, N, a, b))

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
AB=[]
bestmagic=0
bestdpc=0
for i in range(N):
    A,B=map(int,input().split())
    dpc=A/B
    if bestdpc<dpc:
        bestmagic=i
        bestdpc=dpc
    AB.append([A,B])

bestfire=AB[bestmagic][0]
bestcost=AB[bestmagic][1]

ans=float("inf")
for a,b in AB:
    repeat=-(-(H-a)//bestfire)
    ans=min(ans,bestcost*repeat+b)
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC153 E Crested Ibis vs Monster
from collections import deque

h, n = map(int, input().split())
a_list = [None] * n
b_list = [None] * n
rate_list = [None] * n

for i in range(n):
    a_list[i], b_list[i] = map(int, input().split())
    rate_list[i] = a_list[i] / b_list[i]

_list = [rate_list, a_list, b_list]
_list = sorted([list(x) for x in zip(*_list)],key=lambda x:x[1])
_list = [list(x) for x in zip(*_list)]

ans = 0
while h > 0:
    if h < _list[1][n-1]:
        _ = n-1
        while True:
            if h < _list[1][_]:
                _list[1][_] = h
                _list[0][_] = h / _list[2][_]
                _ -= 1
            else:
                break
    _idx = _list[0].index(max(_list[0]))
    ans += (h // _list[1][_idx]) * _list[2][_idx]
    h %= _list[1][_idx]
        
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri May 15 19:04:29 2020

@author: USER
"""
H,N=map(int,input().split())
L=[]
l=[]
A,B=map(int,input().split())
C=A/B
l.append(C)
l.append(A)
l.append(B)
L.append(l)
for i in range(N-1):
    l=[]
    A,B=map(int,input().split())
    C=A/B
    l.append(C)
    l.append(A)
    l.append(B)
    L.append(l)
L=sorted(L,reverse=True)
X=0
Y=[]
for i in range(N):
    A=L[0][1]
    B=L[0][2]
    t=H//A
    X+=B*t
    H=H%A
    Y.append(B+X)
    L.pop(0)
X=min(sorted(Y))
print(X)

Submission status: Wrong Answer

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

ab=[]
dp=[10**9]*(10**5)

maxA=0
for _ in range(n):
    a,b=map(int,input().split())
    ab.append((a,b))
    dp[a]=b
    maxA=max(maxA,a)
#print(dp)
#dp[a]は、相手にaダメージを与えるときの、消耗する自分の最小HP
result=10**9
for i in range(1,h+maxA):
    #print(i,"##########")
    for k in range(1,i//2+2):
        dp[i]=min(dp[i],dp[k]+dp[i-k])
    if i>=h:
        result=min(result,dp[i])
    #print(dp)
print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np

HN = list(map(int, input().split()))
H = HN[0]; N = HN[1]
AB = [list(map(int, input().split())) for _ in range(N)]

DP = np.zeros((N,H))

for i in range(H):
    DP[0][i] = (1+i//AB[0][0])*AB[0][1]

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

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

Submission status: Wrong Answer

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

H, N = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N)]
mx = max([a for a,b in ab])
dp = [[float('inf')]*(H+1+mx) for _ in range(N+1)]
# i番目までの魔法を使った時にモンスターの体力をj減らすするのに必要な魔力の最少値
dp[0][0] = 0

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

for i in range(H, H+mx):
  if dp[N][i] != float('inf'):
    print(dp[N][i])
    break

Submission status: Wrong Answer

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

M = max(T)[0]
dp = [10**9]*(H+M+1)
dp[0] = 0
for i in range(1,H+M+1):
    for j in range(N):
        if i - T[j][0] >= 0:
            dp[i] = min(dp[i],dp[i-T[j][0]]+T[j][1])

for i in range(H,H+M+1):
    if dp[i] != 10**9:
        print(dp[i])
        break

Submission status: Wrong Answer

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

dp = [10**9]*(H+10**4+1)
dp[0] = 0
for i in range(1,H+10**4+1):
    for j in range(N):
        if i - T[j][0] >= 0:
            dp[i] = min(dp[i],dp[i-T[j][0]]+T[j][1])

for i in range(H,H+10**4+1):
    if dp[i] != 10**9:
        print(dp[i])
        break

Submission status: Wrong Answer

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

dp = [10**9]*(H+10**3+1)
dp[0] = 0
for i in range(1,H+10**3+1):
    for j in range(N):
        if i - T[j][0] >= 0:
            dp[i] = min(dp[i],dp[i-T[j][0]]+T[j][1])

for i in range(H,H+10**3+1):
    if dp[i] != 10**9:
        print(dp[i])
        break

Submission status: Wrong Answer

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

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

ans = INF
for i in range(1,n+1):
    for j in range(h,3*h+1):
        if ans > dp[i][j]:
            ans = dp[i][j]
print(ans)

Submission status: Wrong Answer

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

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

ans = INF
for i in range(1,n+1):
    for j in range(h,2*h+1):
        if ans > dp[i][j]:
            ans = dp[i][j]
print(ans)

Submission status: Wrong Answer

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

b = 2*10**5
dp = [[0 for _ in range(b+1)] for _ in range(n+1)]

for j in range(1,b+1):
    for i in range(1,n+1):
        atk = ab[i-1][0]
        mag = ab[i-1][1]
        if j - mag >= 0:
            dp[i][j] = max(dp[i-1][j],dp[i-1][j-mag]+atk,dp[i][j-mag]+atk)
        else:
            dp[i][j] = dp[i-1][j]

for j in range(b+1):
    if dp[n][j] >= h:
        print(j)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
import numpy as np
def main():
    h,n=map(int,input().split())
    lis=np.array([list(map(int,input().split())) for _ in range(n)])
    a_max=np.max(lis[:,0])
    dp=np.zeros(h+a_max+1,int)
    for i in range(1,h+1):
        dp[i+lis[:,0]]=np.minimum(dp[i+lis[:,0]],dp[i]+lis[:,1])
    print(dp[h])

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
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())
min_hp = h
max_hp = h + max(A)

dp = [sys.maxsize] * (max_hp + 1)
dp[0] = 0

for i in range(0, max_hp + 1):
    for j in range(n): # type of spell
        if A[j] > i:
            continue
        dp[i] = min(dp[i], dp[i - A[j]] + B[j])

# dp = [[sys.maxsize] * (max_hp + 1) for i in range(n + 1)]
# for i in range(n + 1):
#     for j in range(min(A[1:]) + 1):
#         dp[i][j] = min(B[1:])
#
# for i in range(1, n + 1):
#     for j in range(1, max_hp + 1):
#         dp[i][j] = dp[i-1][j] if A[i] > j else min(dp[i-1][j-A[i]] + B[i], dp[i-1][j])
#         # dp[i][j] = dp[i][j-1] + B[i] if A[i] > j else min(dp[i][j-A[i]] + B[i], dp[i][j-1] + B[i])
print(dp[h])

Submission status: Wrong Answer

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 = [99999999 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 99999999:
            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)

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

Submission status: Wrong Answer

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 = [99999999 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):
        # 与えることができないパターンのダメージなら、何もしない。
        if ans[i] is 99999999:
            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:-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)

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

Submission status: Wrong Answer

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 = [99999999 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):
        # 与えることができないパターンのダメージなら、何もしない。
        if ans[i] is 99999999:
            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)

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

Submission status: Wrong Answer

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 = [9999999 for i in range(hp + 10000)]

    # 初期データを格納(ダメージ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]))


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)

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

Submission status: Wrong Answer

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 = [99999999 for i in range(hp + 10000)]

    # 初期データを格納(ダメージ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]))


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)

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

Submission status: Wrong Answer

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 = [99999999 for i in range(hp + 10000)]

    # 初期データを格納(ダメージ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:hp + 10000]))


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)

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

Submission status: Wrong Answer

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

dp = [[10 ** 9 for _ in range(2 * 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(2 * H + 1):
        if j >= mahou[i - 1][0]:
            dp[i][j] = min(dp[i][j - mahou[i - 1][0]] + mahou[i - 1][1], dp[i - 1][j])
        else:
            dp[i][j] = dp[i - 1][j]

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

Submission status: Wrong Answer

Solution code:
h, n =map(int, input().split())
dp = [10000000 for _ in range(h+100)]
dp[0] = 0

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

magics.sort()

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

x = dp[h:]
print(min(x))

Submission status: Wrong Answer

Solution code:
h, n =map(int, input().split())
dp = [10000000 for _ in range(h+1000)]
dp[0] = 0

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

magics.sort()

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

x = dp[h:]
print(min(x))

Submission status: Wrong Answer

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

for i in range(n):
    info[i].append(info[i][0]/info[i][1])

info.sort(key = lambda x: x[2], reverse = True)

mp = 0
if h % info[0][0] == 0:
    min_mp = 0
else:
    min_mp = info[0][1]
for i in range(n):
    tem_attack = h // info[i][0]
    if h % info[i][0] == 0:
        min_mp = 0
    else:
        if info[i][1] < min_mp:
            min_mp = info[i][1]
    h -= tem_attack * info[i][0]
    mp += tem_attack * info[i][1]

print(mp+min_mp)