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:
import math

h, n = map(int, input().split())
a = [0] * n
b = [0] * n
#print(a, b)
for i in range(n):
    a[i], b[i] = map(int, input().split())

#print(a)
#print(b)
INF = int(1e10)
dp = [[INF] * (h+1) for i in range(n+1)]
for i in range(h+1):
    dp[0][i] = math.ceil(i / a[0]) * b[0]
for i in range(n):
    dp[i][0] = 0
for i in range(n):
    for j in range(h+1):
        if j <= a[i]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j - a[i]] + b[i])
#print(dp)
print(dp[n][h])

Submission status: Wrong Answer

Solution code:
import math

h, n = map(int, input().split())
a = [0] * n
b = [0] * n
#print(a, b)
for i in range(n):
    a[i], b[i] = map(int, input().split())

#print(a)
#print(b)
INF = int(1e10)
dp = [[INF] * (h+1) for i in range(n+1)]
for i in range(h+1):
    dp[0][i] = math.ceil(i / a[0]) * b[0]
for i in range(n):
    dp[i][0] = 0
for i in range(n):
    for j in range(h+1):
        if j < a[i]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j - a[i]] + b[i])
#print(dp)
print(dp[n][h])

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect

H, N = map(int, input().split())
A, B, P = [], [], []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    P.append(a/b)
A = np.array(A)
B = np.array(B)
P = np.array(P)
sort_key = np.argsort(P)[::-1]

count = 0
damage = 0
for k in sort_key:
    damage += A[k]
    count += B[k]
    if damage >= H:
        break
print(count)

Submission status: Wrong Answer

Solution code:
def solve():
    H, N = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(N)]
    
    INF = 10 ** 6
    dp = [INF for _ in range(10 ** 7)]
    dp[0] = 0

    for a, b in A:
        for i in range(H+1):
            if dp[i] == INF: continue
            dp[i+a] = min(dp[i+a], dp[i] + b)
            
    return min(dp[H:])

print(solve())

Submission status: Wrong Answer

Solution code:
H,N=map(int,input().split())
A=[]
B=[]
E=[]
idx=0
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    e=a/b
    E.append([e,idx])
    idx+=1

E.sort(reverse=True)
#print(E[0][0])
ans=0
tmp=0
while(1):
    tmp+=A[E[0][1]]
    #print("tmp")
    #print(tmp)
    if tmp>H:
        tmp-=A[E[0][1]]
        break
    elif tmp==H:
        ans+=B[E[0][1]]
        #print(ans)
        exit()
    ans+=B[E[0][1]]
d=10**9
for i in range(N):
    kali=tmp
    kali+=A[i]
    if kali>=H:
        d=min(d,B[i])
#print(d)
ans+=d
print(ans)

Submission status: Wrong Answer

Solution code:
inf=1<<31
H,N=map(int,input().split())
dp=[[inf]*(H+1) for i in range(N+1)]
dp[0][0]=0
for i in range(N):
  i_=i+1
  a,b=map(int,input().split())
  if H<a:
    dp[i_][H]=min(dp[i][H],b)
  else:
    for j in range(a):
      dp[i_][j]=dp[i][j]
  for j in range(a,H+1):
    dp[i_][j]=min(dp[i][j-a]+b,dp[i][j])
  for j in range(H-1,0,-1):
    dp[i_][j]=dp[i_][j+1] if dp[i_][j+1]<dp[i_][j] else dp[i_][j]
print(dp[N][H])

Submission status: Wrong Answer

Solution code:
inf=1<<31
H,N=map(int,input().split())
dp=[[inf]*(H+1) for i in range(N+1)]
dp[0][0]=0
for i in range(N):
  i_=i+1
  a,b=map(int,input().split())
  for j in range(H+1):
    if j<a:
      dp[i_][j]=dp[i][j]
    else:
      tmp=dp[i][j-a]+b
      if tmp<dp[i][j]:
        dp[i_][j]=tmp
  for j in range(0,H-1,-1):
    dp[i_][j]=dp[i_][j+1] if dp[i_][j+1]<dp[i_][j] else dp[i_][j]
print(dp[N][H])

Submission status: Wrong Answer

Solution code:
class Magic:
    def __init__(self, damage, cost):
        self.damage = damage
        self.cost = cost
        self.efficiency = self.damage / self.cost

    def attack_count(self, hp):
        count, remainder = divmod(hp, self.damage)
        # count += 0 if remainder == 0 else 1
        return count

    def sum_cost(self, hp):
        count = self.attack_count(hp)
        return self.cost * count


h, n = map(int, input().split())
magic_list = []
for _ in range(n):
    d, c = map(int, input().split())
    magic_list.append(Magic(d, c))

efficient_magic_list = []
for m in magic_list:
    if efficient_magic_list == []:
        efficient_magic_list.append(m)

    # 一番後ろに挿入
    if efficient_magic_list[-1].sum_cost(h) < m.sum_cost(h):
        efficient_magic_list.append(m)
    # そうでなければ探索
    else:
        for i in range(len(efficient_magic_list)):
            if m.sum_cost(h) < efficient_magic_list[i].sum_cost(h):
                # 後ろから1ずつずらす
                efficient_magic_list.append(efficient_magic_list[-1])
                for j in range(len(efficient_magic_list)-1, i, -1):
                    efficient_magic_list[j] = efficient_magic_list[j-1]
                efficient_magic_list[i] = m
                break

cost_count = efficient_magic_list[0].sum_cost(h)
remainder_hp = h % efficient_magic_list[0].damage
if remainder_hp > 0:
    ans = magic_list[0]
    for m in magic_list:
        if m.attack_count(remainder_hp) <= ans.attack_count(remainder_hp) and m.cost < ans.cost:
            ans = m
    cost_count += (ans.attack_count(remainder_hp)+1) * ans.cost

print(cost_count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(1000000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    h,n = map(int,input().split())
    ab = [list(map(int,input().split())) for i in range(n)]
    a = [ab[i][0] for i in range(n)]
    b = [ab[i][0] for i in range(n)]
    inf = float("inf")
    dp = [inf for i in range(h+max(a)+1)] #dp[i]...体力をi削るために必要な最低の魔力
    dp[0] = 0
    for i in range(n):
        for j in range(h+1):
            dp[j+a[i]] = min(dp[j]+b[i],dp[j+a[i]])

    print(min(dp[h:]))    
#endline===============================================

Submission status: Wrong Answer

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

H, N = map(int, input().split())
AB = []
for _ in range(N):
    lis = list(map(int, input().split()))
    cost = lis[0] / lis[1]
    lis.append(cost)
    AB.append(lis)

AB.sort(key=lambda x: x[2])
cop = copy.deepcopy(AB)
x = cop[N-1]
ans = 0
while H > 0:
    try:
        eff = AB.pop()
    except:
        break
    q = H // eff[0]
    ans += q * eff[1]
    H -= q * eff[0]
if H <= 0:
    print(ans)
    exit()
else:
    cop.sort(key=lambda x:x[1])
    print(ans + cop[0][1])

Submission status: Wrong Answer

Solution code:
"""
NTC here
"""
import sys
inp = sys.stdin.readline


def input(): return inp().strip()


flush = sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**25)


def iin(): return int(input())


def lin(): return list(map(int, input().split()))

# range = xrange
# input = raw_input


def main():
    h, n = lin()
    a = [ lin() for i in range(n) ]
    a.sort(key= lambda x: x[0])
    ans = [-1]*(h+1)
    ans[0] = 0
    for i in range(h):
        if ans[i]!=-1:
            for j in range(n):
                df, val = a[j]
                if i+df<=h:
                    ans[i+df] = ( ans[i]+val if ans[i+df]==-1 else min(ans[i+df], ans[i]+val))
                else:
                    break
    print(ans[h]) 


main()
# threading.Thread(target=main).start()

Submission status: Wrong Answer

Solution code:
INF = float('inf')
H, N = [int(i) for i in input().split(' ')]
dp = [INF for _ in range(H+1)]
dp[0] = 0
for i in range(N):
  a, b = [int(x) for x in input().split(' ')]
  for j in range(H):
    dp[min(j+a, H)] = min(dp[min(j+a, H)], dp[j]+b)
print(dp)

Submission status: Wrong Answer

Solution code:
H, N = [int(i) for i in input().split(' ')]
A = [0 for i in range(N)]
B = A.copy()
for i in range(N):
  A[i], B[i] = [int(j) for j in input().split(' ')]
W = H // min(A) * max(B)

dp = [[0 for _ in range(W)] for _ in range(N+1)]
sumb = 1000000000
for i in range(N):
  for j in range(W):
    if j >= B[i]:
      dp[i+1][j] = max(dp[i][j], dp[i+1][j-B[i]] + A[i])
    else:
      dp[i+1][j] = dp[i][j]
    if dp[i+1][j] >= H:
      sumb = min(sumb, j)
      break
print(sumb)

Submission status: Wrong Answer

Solution code:
H, N = [int(i) for i in input().split(' ')]
A = [0 for i in range(N)]
B = A.copy()
for i in range(N):
  A[i], B[i] = [int(j) for j in input().split(' ')]
W = H // min(A) * max(B)

dp = [[0 for _ in range(W)] for _ in range(N+1)]
for i in range(N):
  for j in range(W):
    if j >= B[i]:
      dp[i+1][j] = max(dp[i][j], dp[i+1][j-B[i]] + A[i])
    else:
      dp[i+1][j] = dp[i][j]

minb = 1000000000
for i in range(1, N+1):
  for j in range(len(dp[i])):
    if dp[i][j] >= H:
      minb = min(minb, j)
      break
print(minb)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

magic = np.zeros((N+1, 2), dtype=int)

for i in range(1, N+1):
    magic[i][0], magic[i][1] = map(int, input().split())

# print(magic)

INF = 110000000  # 10^4 * 10^4 + spare

#dp = np.full((N+1, H*2+1), INF)
#dp[0][0] = 0

dp = np.full(H*2+1, INF)
dp[0] = 0

for i in range(1, N+1):
    for j in range(H + 1):
        # 貰うDP
        if j-magic[i][0] >= 0:
            dp[j] = min(dp[j - magic[i][0]] + magic[i][1], dp[j])
        else:
            dp[j] = min(dp[0] + magic[i][1], dp[j])

#        print(dp[i])
#    print(dp)

print(dp[H])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

magic = np.zeros((N+1, 2), dtype=int)

for i in range(1, N+1):
    magic[i][0], magic[i][1] = map(int, input().split())

# print(magic)

INF = 110000000  # 10^4 * 10^4 + spare

dp = np.full((N+1, H*2+1), INF)

dp[0][0] = 0

for i in range(1, N+1):
    for j in range(H + 1):
        # 貰うDP
        dp[i][j] = min(dp[i - 1][j], dp[i][j])
        if j-magic[i][0] >= 0:
            dp[i][j] = min(dp[i][j - magic[i][0]] + magic[i][1], dp[i][j])
        else:
            dp[i][j] = min(dp[i][0] + magic[i][1], dp[i][j])

#        print(dp[i])
#    print(dp)

print(dp[N][H])

Submission status: Wrong Answer

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

_,a,b = lst[-1]

mp  = (h//a) * b
h -= h//a * a
lst.sort(key = lambda x:x[2])
if h == 0:
  print(mp)
  exit(0)
for l in lst:
  if l[1] >=h:
    mp += l[2]
    break
print(mp)

Submission status: Wrong Answer

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

_,a,b = lst[-1]
import math
print(math.ceil(h/a) * b)

Submission status: Wrong Answer

Solution code:
#WA
H, n = map(int, input().split())
List = [[0 for i in range(4)] for j in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    List[i][0] = b / a
    List[i][1] = b / H
    List[i][2] = a
    List[i][3] = b

def recal():
    for i in range(n):
        if H > 0:
            b = List[i][3]
            List[i][1] = b / H

sList = sorted(List)
MP = 0
k = 0
while H > 0:
    if sList[k][2] <= H:
        H -= sList[k][2]
        MP += sList[k][3]
        recal()
    else:
        if sList[k + 1][2] <= H:
            if sList[k][1] <= sList[k + 1][0]:
                H -= sList[k][2]
                MP += sList[k][3]
                recal()
            else:
                H -= sList[k + 1][2]
                MP += sList[k + 1][3]
                recal()
                k += 1
        else:
            if sList[k][1] <= sList[k + 1][1]:
                H -= sList[k][2]
                MP += sList[k][3]
                recal()
            else:
                H -= sList[k + 1][2]
                MP += sList[k + 1][3]
                recal()
                k += 1
print(MP)

Submission status: Wrong Answer

Solution code:
x=input().split()
h=int(x[0])
n=int(x[1])
import numpy as np
a=np.full(n,0,int)
b=np.full(n,0,int)
for k in range(n):
  x=input().split()
  a[k]=int(x[0])
  b[k]=int(x[1])
m=max(a)  
inf=10**8
dp=np.full(h+m+1,inf,int)
dp[0]=0
for k in range(1,h+m):
  dp[k]=min((dp[np.max(k-a,-1)]+b).min(),10**8)
print(dp[h:h+m].min())

Submission status: Wrong Answer

Solution code:
x=input().split()
h=int(x[0])
n=int(x[1])
import numpy as np
a=np.full(n,0,int)
b=np.full(n,0,int)
for k in range(n):
  x=input().split()
  a[k]=int(x[0])
  b[k]=int(x[1])
m=max(a)  
inf=10**8
dp=np.full(h+m,inf,int)
dp[0]=0
for k in range(1,h+m):
  dp[k]=(dp[np.max(k-a,0)]+b).min()
print(dp[h:h+m].min())

Submission status: Wrong Answer

Solution code:
H, n = map(int, input().split())
List = [[0 for i in range(4)] for j in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    List[i][0] = b / a
    List[i][1] = b / H
    List[i][2] = a
    List[i][3] = b

def recal():
    for i in range(n):
        if H > 0:
            b = List[i][3]
            List[i][1] = b / H

sList = sorted(List)
MP = 0
k = 0
while H > 0:
    if sList[k][2] <= H:
        H -= sList[k][2]
        MP += sList[k][3]
        recal()
    else:
        if sList[k] == sList[k + 1]:
            k += 1
        elif sList[k + 1][2] <= H:
            if sList[k][1] < sList[k + 1][0]:
                H -= sList[k][2]
                MP += sList[k][3]
                recal()
                break
            else:
                H -= sList[k + 1][2]
                MP += sList[k + 1][3]
                recal()
                k += 1
        else:
            if sList[k][1] < sList[k + 1][1]:
                H -= sList[k][2]
                MP += sList[k][3]
                recal()
                break
            else:
                H -= sList[k + 1][2]
                MP += sList[k + 1][3]
                recal()
                k += 1
print(MP)

Submission status: Wrong Answer

Solution code:
H, n = map(int, input().split())
List = [[0 for i in range(4)] for j in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    List[i][0] = b / a
    List[i][1] = b / H
    List[i][2] = a
    List[i][3] = b

def recal():
    for i in range(n):
        if H > 0:
            b = List[i][3]
            List[i][1] = b / H

sList = sorted(List)
MP = 0
k = 0
while H > 0:
    if sList[k][2] <= H:
        H -= sList[k][2]
        MP += sList[k][3]
        recal()
    else:
        if sList[k + 1][2] <= H:
            if sList[k][1] <= sList[k + 1][0]:
                H -= sList[k][2]
                MP += sList[k][3]
                recal()
                break
            else:
                H -= sList[k + 1][2]
                MP += sList[k + 1][3]
                recal()
                k += 1
        else:
            if sList[k][1] <= sList[k + 1][1]:
                H -= sList[k][2]
                MP += sList[k][3]
                recal()
                break
            else:
                H -= sList[k + 1][2]
                MP += sList[k + 1][3]
                recal()
                k += 1
print(MP)

Submission status: Wrong Answer

Solution code:
x=input().split()
h=int(x[0])
n=int(x[1])
a=[]
b=[]
for k in range(n):
  x=input().split()
  a.append(int(x[0]))
  b.append(int(x[1]))  
m=max(a)  
dp=[10**7]*(h+m)
dp[0]=0
for k in range(h+m):
  for i in range(n):
    if k-a[i]<0:
      continue
    new=dp[k-a[i]]+b[i]
    if new < dp[k]:
      dp[k]=new
print(min(dp[h:h+m]))

Submission status: Wrong Answer

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

min_d=min([lis[0] for lis in li])
min_=min([lis[1] for lis in li])
ans=[0 for _ in range(min_d*(n)+1)]

result=0
for i in range(min_,len(ans)):
  max_=0
  for j in range(len(li)):
    if li[j][1]>i:
      continue
    if max_<li[j][0]+ans[i-li[j][1]]:
      max_=li[j][0]+ans[i-li[j][1]]
  ans[i]=max_
  if max_>=n:
    result=i
    break
print(result)

Submission status: Wrong Answer

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

a = []
b = []

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

dp = [pow(10, 10)] * (h + 1)
dp[0] = 0

for k in range(n):
    for j in range(h):
        next_index = min(j + a[k], h)
        dp[next_index] = min(dp[next_index], dp[h] + b[k])

print(dp[h])

Submission status: Wrong Answer

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

a = []
b = []

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

dp = [pow(10, 4)] * (h + 1)
dp[0] = 0

for k in range(n):
    for j in range(h):
        if j + a[k] <= h and dp[j + a[k]] > dp[j] + b[k]:
            dp[j + a[k]] = dp[j] + b[k]
        elif j + a[k] > h:
            if dp[h] > dp[j] + b[k]:
                dp[h] = dp[j] + b[k]

print(dp[h])

Submission status: Wrong Answer

Solution code:
h, n = map(int, input().split())
s = []
t = []
u = []
c = 0
m = 0
ans = 10 ** 9
for i in range(n):
    a, b = map(int, input().split())
    s.append(a)
    t.append(b)
    if c < a / b:
        c = a / b
        m = i
    if c == a / b:
        if s[i] >= s[m]:
            c = a / b
            m = i
for i in range(n):
    if i != m:
        l = int((h - s[i]) / s[m]) + 1
        if ans >= t[i] + l * t[m]:
            ans = t[i] + l * t[m]
    else:
        l = int(h - s[m]) + 1
        if ans >= l * t[m]:
            ans = l * t[m]
print(ans)

Submission status: Wrong Answer

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

a = sorted(p, key = lambda x:x[1] , reverse = True)[0][0]
dp = [10**9] * (H+1+a)
dp[0] = 0

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

print(min(dp[H:H+a]))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

INF=float("inf")
    
dp=np.array([[INF]*(1+h) for i in range(1+n)])

dp[0][0]=0

for i in range(n):
    # print(dp)
    for j in range(h+1):
        # print("i+1=",i+1,"j=",j)
        dp[i+1][j]=min(dp[i+1][j],dp[i][j])
        dp[i+1][min(h,j+a[i])]=min(dp[i+1][min(h,j+a[i])],dp[i+1][j]+b[i])
        # print(i+1,j,min(h,j+a[i]),"\n",dp)
    # print("==================")

# print(dp)
print(dp[n][h])

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

h,n=LI()
a=[]
ab=[]
for i in range(n):
    x,y=LI()
    ab.append((x,y))
ab.sort()
a=[i for i,j in ab]
b=[j for i,j in ab]
dp=[[0]+[inf]*(h) for i in range(n+1)]    
dp[0][0]=0

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

ans=dp[n][h]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

H, N = map(int, input().split())
MAGICS = list(list(map(int, input().split())) for _ in range(N))

INF = sys.maxsize
MAX = max(x[0] for x in MAGICS)
DP = [INF for _ in range(H+2)]

DP[0] = 0

for h in range(H-MAX+2):
    for j, magic in enumerate(MAGICS):
        (damage, mp) = magic
        DP[h + damage] = min(DP[h + damage], DP[h] + mp)

for i in reversed(range(len(DP))):
    DP[i-1] = min(DP[i], DP[i-1])

print(DP[H])

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

h,n=LI()
a=[]
b=[]
for i in range(n):
    x,y=LI()
    a+=[x]
    b+=[y]
M=max(a)

dp=[[0]+[inf]*(h+M) for i in range(n+1)]    
dp[0][0]=0

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

ans=min(dp[n][h:])

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True

ans=0

h,n=LI()
a=[]
b=[]
for i in range(n):
    x,y=LI()
    a+=[x]
    b+=[y]
M=max(a)

dp=[[0]+[inf]*(h+M) for i in range(n+1)]    
dp[0][0]=0

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

ans=dp[n][h]

print(ans)