Problem:


Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

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

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

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

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

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

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

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

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

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

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

Sample Input 2

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

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

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

Sample Output 3

139815



Sample input:
9 3
8 3
4 2
2 1


Sample output:
4

Submission status: Accepted

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

a = []
b = []
m = []
for i in range(n):
    at, bt = map(int, input().split()) # a_tmp, b_tmp
    a.append(at)
    b.append(bt)

dp = [[0]*(h+1) for _ in range(n+1)]
for i in range(n):
    for j in range(h):
        if j - a[i] < 0:
            o = b[i]
        else:
            o = dp[i+1][j+1-a[i]] + b[i]
        if i != 0:
            dp[i+1][j+1] = min(dp[i][j+1], o)
        else:
            dp[i+1][j+1] = o

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

Submission status: Accepted

Solution code:
#import numpy as np

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

a = []
b = []
m = []
for i in range(n):
    at, bt = map(int, input().split()) # a_tmp, b_tmp
    a.append(at)
    b.append(bt)

#dp = np.zeros((n+1,h+1))
dp = [[0]*(h+1) for _ in range(n+1)]
for i in range(n):
    for j in range(h):
        # print("Test")
        if j - a[i] < 0:
            o = b[i]
        else:
            o = dp[i+1][j+1-a[i]] + b[i]
        if i != 0:
            dp[i+1][j+1] = min(dp[i][j+1], o)
        else:
            dp[i+1][j+1] = o

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

Submission status: Accepted

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

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

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

print(dp[N][H])

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

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

H, N = lr()
INF = 10 ** 15
limit = H + 100
dp = [INF] * limit
dp[0] = 0
for _ in range(N):
    a, b = lr()
    for i in range(H):
        index = i+a
        if index > limit - 1:
            index = limit - 1
        dp[index] = min(dp[index], dp[i] + b)

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, n = list(map(int, readline().split()))
    ab = [[0] * 2 for _ in range(n)]

    for i in range(n):
        ab[i] = list(map(int, readline().split()))

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

    for i in range(h + 1):
        mpcur = dp[i]
        for a, b in ab:
            hpnext = min(i + a, h)
            dp[hpnext] = min(mpcur + b, dp[hpnext])

    print(dp[h])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

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

H, N = lr()
INF = 10 ** 15
limit = H + 100
dp = [INF] * limit
dp[0] = 0
for _ in range(N):
    a, b = lr()
    for i in range(limit):
        index = i+a
        if index > limit - 1:
            index = limit - 1
        dp[index] = min(dp[index], dp[i] + b)

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

Submission status: Accepted

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

import sys

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

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

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

Submission status: Accepted

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

import sys

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

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

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

Submission status: Accepted

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


inf=float('inf')
component=[0]
for i in range(H):
    component.append(inf)
DP=[]
for j in range(N+1):
    DP.append(copy.copy(component))#DPは(N+1)×(H+1)


for k in range(1,N+1):#k番目の魔法
    for l in range(1,H+1):#体力H
        DP[k][l]=min(DP[k-1][l],DP[k][max(0,l-A[k-1])]+B[k-1])

print(DP[N][H])

Submission status: Accepted

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


inf=float('inf')
component=[0]
for i in range(H):
    component.append(inf)
DP=[]
for j in range(N+1):
    DP.append(component)#DPは(N+1)×(H+1)


for k in range(1,N+1):#k番目の魔法
    for l in range(1,H+1):#体力H
        DP[k][l]=min(DP[k-1][l],DP[k][max(0,l-A[k-1])]+B[k-1])

print(DP[N][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)
#n番目を1回は使う時の魔力最小値
dp=np.zeros((H+A.max()))
for i in range(1,H+1):
  dp[i]=(dp[i-A]+B).min()
  

      
      
print(int(dp[H]))

Submission status: Accepted

Solution code:
H,N=map(int,input().split())
magic=[list(map(int,input().split())) for _ in range(N)]
dp=[float("inf") for _ in range(H+10**4)] #dptableには、魔力の合計値が格納されてるはず
#print(magic)
for i in range(H+10**4):
    for j in range(N):
        if magic[j][0]>i:
            dp[i]=min(dp[i],magic[j][1])
        else:
            dp[i]=min(dp[i],dp[i-magic[j][0]]+magic[j][1])
        #print(dp,dp[i],magic[j][0],i,magic[j][0]<i,magic[j][1])
        #input()
print(dp[H-1])

Submission status: Accepted

Solution code:
h,n = map(int,input().split())
A,B = [0],[0]
for i in range(n):
  a,b = map(int,input().split())
  A.append(a)
  B.append(b)
dp = [0]+[float('inf')]*(h)
for j in range(1,h+1):
  for i in range(1,n+1):
    dp[j] = min(dp[j],dp[max(0,j-A[i])]+B[i])
print(dp[-1])

Submission status: Accepted

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

dp = [float('inf') for _ in range(H + 1)]
dp[H] = 0

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

print(dp[0])

Submission status: Accepted

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

W,n = inpl()
wv = [inpl() for _ in range(n)]
dp = [INF] * (W+1)
dp[0] = 0
for i in range(n):
    w,v = wv[i]
    for j in range(W+1):
        if j < w:
            dp[j] = min(dp[j], v)
            continue
        dp[j] = min(dp[j], dp[j-w] + v)
print(dp[W])

Submission status: Accepted

Solution code:
# ABC153-E
if True:
    H,N=map(int,input().split())
    M=tuple(tuple(map(int,input().split()))for i in range(N))
    MAX=max(a for a,b in M)
    DP=[0 for _ in range(H+MAX)]
    for i in range(1,len(DP)):
        DP[i]=min(DP[i-a]+b for a,b in M)
    print(DP[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_)

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

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

print(dp[-1])

Submission status: Accepted

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

dp = [float('inf') for i in range(h+1)]
dp[0]=0
for i in range(h):
    for j in range(n):
        dp[min(h,i+a[j][0])] = min(dp[min(h,i+a[j][0])],dp[i]+a[j][1])
print(dp[-1])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


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


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = {False: 'No', True: 'Yes'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


# 個数制限付きナップザック問題
def main():
    H, N = MI()
    A = [0] * N
    B = [0] * N
    dp = [IINF] * (H+1)
    dp[0] = 0

    for i in range(N):
        A[i], B[i] = MI()

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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


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


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = {False: 'No', True: 'Yes'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


# 個数制限付きナップザック問題
def main():
    H, N = MI()
    A = [0] * N
    B = [0] * N
    dp = [IINF] * (H+1)
    dp[0] = 0

    for i in range(N):
        A[i], B[i] = MI()

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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#モンスターの体力は H
#トキはN種類の魔法が使え、
#i番目の魔法を使うと、モンスターの体力を Ai減らすことができますが、
#トキの魔力を Bi消耗します。

#dp[i] = モンスターの体力を i 減らすため消耗する魔力の最小値
#dp[i+1] = min(dp[i-Ai]+Bi,dp[i])
#dp[0]=0

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

Submission status: Accepted

Solution code:
H,N=map(int,input().split())
C=[0 for i in range(H+(10**4)+1)]
d=0
for i in [0]*N:
 A,B=map(int,input().split())
 for i in range(H+A+1):
  if A==i :
   C[i]=B
  elif A<i :
   if C[i]==0 and C[i-A]>0:
    C[i]=C[i-A]+B
   elif C[i-A]>0:
    C[i]=min(C[i],C[i-A]+B)
 d=max(d,A)
e=10**9
for i in range(H,H+d+1):
 if C[i]>0:
  e=min(C[i],e)
print(e)

Submission status: Accepted

Solution code:
H,N=map(int,input().split())
magic=[list(map(int,input().split())) for _ in range(N)]
maxA=max(a for a,b in magic)
DP=[0]*(H+maxA)
for i in range(1,H+maxA):
    DP[i]=min(DP[i-a]+b for a,b in magic)
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 = 0 
for a, b in ab:
  max_a = max(max_a, a)
# i番目まで見たときに、モンスターの体力をj減らした時の、トキの消耗した魔力の最小値
dp = [[float('inf')]*(h+max_a+1) for _ in range(n+1)]
for i in range(n+1):
  dp[i][0] = 0
for i in range(n):
  a, b = ab[i]
  for j in range(h+max_a+1):
    if j-a >= 0:
      dp[i+1][j] = min(dp[i][j], dp[i+1][j-a]+b)
    else:
      dp[i+1][j] = dp[i][j]


ans = float('inf')
for i in range(h, h+max_a+1):
  ans = min(ans, dp[n][i])
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    H, N = Scanner.map_int()
    A, B = [0 for _ in range(N)], [0 for _ in range(N)]
    for i in range(N):
        a, b = Scanner.map_int()
        A[i] = a
        B[i] = b
    dp = [0] * (H + 1)
    for i in range(1, H + 1):
        mp = INF
        for j in range(N):
            if i - A[j] < 0:
                mp = min(mp, B[j])
            else:
                mp = min(mp, dp[i - A[j]] + B[j])
            pass
        dp[i] = mp
    print(dp[H])


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,n = map(int,input().split())
magic = []
maxa = 0
for _ in range(n):
  a,b = map(int,input().split())
  maxa = max(maxa,a)
  magic.append((a,b))
#magic.sort()

dp = [float('inf') for _ in range(h + maxa)]
dp[0] = 0

for i in range(h):
  for a,b in magic:
    if i+a > h+maxa:
      continue
    dp[i+a] = min(dp[i] + b,dp[i+a])
print(min(dp[h:]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import heapq


def solve(H: int, N: int, A: "List[int]", B: "List[int]"):
    dp = [10**16]*(H+1)
    dp[0] = 0
    positions = [0]
    for i in range(len(dp)):
        c = dp[i]
        for j in range(N):
            ni = min(H, i+A[j])
            dp[ni] = min(dp[ni], c+B[j])
    print(dp[-1])
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(H, N, A, B)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
magic = list()
for _ in range(N):
    magic.append(list(map(int, input().split())))
 
magic_min = [0 for _ in range(H+1)]
for i in range(H+1):
    if i != 0 and magic_min[i] == 0:
        continue
    for A, B in magic:
        Index = i + A
        value = magic_min[i] + B
        try:
            if magic_min[Index] == 0:
                magic_min[Index] = value
            elif value < magic_min[Index]:
                magic_min[Index] = value
        
        except:
            if magic_min[-1] == 0:
                magic_min[-1] = value
            elif value < magic_min[-1]:
                magic_min[-1] = value
                
print(magic_min[-1])

Submission status: Accepted

Solution code:
import numpy as np

H,N = list(map(int, input().split()))
As = []
Bs = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    As.append(a)
    Bs.append(b)
As = np.array(As)
Bs = np.array(Bs)

dp = np.full((H+1), np.inf)
dp[0] = 0

for c in range(1,H+1):
    dp[c] = np.amin(dp[np.maximum(c - As, 0)] + Bs)

print(int(dp[-1]))

Submission status: Accepted

Solution code:
from collections import deque


def read():
    H, N = list(map(int, input().strip().split()))
    A = list()
    B = list()
    for i in range(N):
        a, b = list(map(int, input().strip().split()))
        A.append(a)
        B.append(b)
    return H, N, A, B


def solve(H, N, A, B, INF=10**9):
    BMAX = max(B)
    dp = [[INF for i in range(N)] for h in range(H + BMAX + 1)]
    for i in range(0, N):
        dp[0][i] = 0
    for h in range(0, H + BMAX + 1):
        for i in range(N):
            if i == 0:
                if h - A[i] >= 0:
                    dp[h][i] = dp[h - A[i]][i] + B[i]
            else:
                if h - A[i] >= 0:
                    dp[h][i] = min(dp[h][i-1], dp[h - A[i]][i] + B[i])
                else:
                    dp[h][i] = dp[h][i-1]
    min_b = INF
    for h in range(H, H + BMAX + 1):
        for i in range(N):
            min_b = min(min_b, dp[h][i])
    return min_b


if __name__ == "__main__":
    inputs = read()
    print("%s" % solve(*inputs))

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

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
dp=[10**10]*(h+1)
dp[0]=0
X=[]
for i in range(n):
    a,b=map(int,input().split())
    X.append([a,b])


for i in range(h):
    for j in range(n):
        k=min(i+X[j][0],h)
        dp[k]=min(dp[k],dp[i]+X[j][1])
print(dp[h])

Submission status: Accepted

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

Submission status: Accepted

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

for start in range(h):
    for a, b in ab:
        end = min(start+a, h)
        dp[end] = min(dp[end], dp[start]+b)
print(dp[h])

Submission status: Accepted

Solution code:
H, N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
inf = float("inf")
dp = [[inf] * (H+10**4+1) for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
    for j in range(H+10**4+1):
        if j-AB[i][0] >= 0:
            dp[i+1][j] = min(dp[i+1][j-AB[i][0]]+AB[i][1], dp[i][j]) 
        else:
            dp[i+1][j] = dp[i][j]
ans = inf
for i in range(H,H+10**4+1):
    ans = min(ans, dp[-1][i])
print(ans)

Submission status: Accepted

Solution code:
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
maxA = max(a for a,b in AB)
dp = [0]*(H+maxA)
for i in range(1,H + maxA):
  dp[i] = min(dp[i-a]+b for a,b in AB)
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 =[0]*(h+max(a for a,b in ab))
for i in range(1, h+1):
    dp[i] = min(dp[i-a]+b for a,b in ab)
print(dp[h])

Submission status: Accepted

Solution code:
def main():
    h, n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    dp =[0]*(h+max(a for a,b in ab))
    for i in range(1, h+1):
        dp[i] = min(dp[i-a]+b for a,b in ab)
    print(dp[h])
      
if __name__ == '__main__':
    main()

Submission status: Accepted

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

#dp[i][h]:i番目の魔法まで見てhのダメージを与えるときの最小魔力
dp = [[10**8+1 for j in range(H+1)] for i in range(N+1)]
dp[0][0] = 0

#配るdpで考える
for i in range(N):
  for h in range(H+1):
    #i番目の魔法を使わないとき
    dp[i+1][h] =  min(dp[i][h], dp[i+1][h])
    #i番目の魔法を一個使うとき
    dp[i+1][ min(h+l[i][0],H) ]  = min(dp[i+1][min(h+l[i][0],H)],dp[i+1][h]+l[i][1] )   
print(dp[N][H])

Submission status: Accepted

Solution code:
h,n = map(int,input().split())
dp = [0]+[1000000000 for _ in range(h)]
for i in range(n):
    ai,bi = map(int,input().split())
    for j in range(h):
        dp[min(j+ai,h)] = min(dp[min(j+ai,h)],dp[j]+bi)
print(dp[h])

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
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 = ZIP(N)
A = np.array(A)
B = np.array(B)

dp = np.zeros(10001, dtype=np.int64)

for i in range(1, H+1):
	dp[i]=(dp[i-A]+B).min()

print(dp[H])

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np
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 = ZIP(N)
A = np.array(A)
B = np.array(B)
max_A = A.max()

dp = np.zeros(10001, dtype=np.int64)

for i in range(1, H+1):
	dp[i]=(dp[i-A]+B).min()

print(dp[H])

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
table = [float("inf")] * (h + 10000)
table[0] = 0

for a, b in ab:
    for i in range(h):
        table[i+a] = min(table[i+a], table[i] + b)

print(min(table[h:]))

Submission status: Accepted

Solution code:
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 = [float('inf')] * (H + 5)
    dp[0] = 0
    for i in range(H+1):
        for j in range(N):
            if i + A[j] >= H:
                dp[H] = min(dp[H], dp[i] + B[j])
            else:    
                dp[i + A[j]] = min(dp[i + A[j]], dp[i] + B[j])
    ans = dp[H]

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h,n = [int(i) for i in input().split()]
ab = []
for i in range(n):
  a,b = [int(i) for i in input().split()]
  ab.append([a,b])
dp = [[10**8]*((h+1)+10**4) for i in range(n+1)]
dp[0][0]=0
for i in range(n):
  for j in range(h+1+10**4):
    if j>=ab[i][0]:
      dp[i+1][j]=min(dp[i][j],dp[i+1][j-ab[i][0]]+ab[i][1])
    else:
      dp[i+1][j]=dp[i][j]
      
ans = 10**8    
for i in range(n+1):
  for j in range(h,h+1+10**4):
    ans = min(ans,dp[i][j])
print(ans)

Submission status: Accepted

Solution code:
import math
import time


t = time.time()
def iip():
    return [int(i) for i in input().split()]

def main():
    H, N = iip()
    mp_for_damage = [10**9 for i in range(H+1)]
    mp_for_damage[0] = 0
    ab = []

    for i in range(N):
        ab.append(iip())

    ab= sorted(ab, key=lambda x:x[0]/x[1], reverse=True)

    for i in range(N):
        a, b = ab[i]

        for k in range(1, min(a, H) + 1):
            mp_for_damage[k] = min(mp_for_damage[k], b)
            # print(k, b)

        if a >= H:
            mp_for_damage[H] = min(mp_for_damage[H], b)
            continue

        if a < H and mp_for_damage[a] < b:
            continue

        for j in range(H-a+1):
            mp_for_damage[j+a] = min(mp_for_damage[j+a], mp_for_damage[j] + b)

    #print(mp_for_damage)
    #print(mp_for_damage)
    print(mp_for_damage[H])

    #print(time.time() - t)

main()

Submission status: Accepted

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

for _ in range(n):
    a, b = map(int, input().split())
    for i in range(h):
        dp[min(i+a,h)] = min(dp[i]+b, dp[min(i+a,h)])
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 = [1e100]*(h+1)
dp[0] = 0

for i in range(n):
    for j in range(h+1):
        if dp[j] != 1e100:
            nj = j + a[i]
            if nj >= h:
                nj = h
            dp[nj] = min(dp[nj],dp[j] + b[i])
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, _ in ab)
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 ab)
print(min(dp[h:]))

Submission status: Accepted

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

h_m = max(d)+h+1
dp = [[float("inf")]*h_m for x in range(n+1)]
dp[0][0] = 0

for i in range(n):
    for j in range(h_m):
        if j<d[i]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i+1][j-d[i]]+mp[i],dp[i][j])

print(min(dp[n][h:h_m]))