Problem:


Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

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

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

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

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

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

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

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

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

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

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

Sample Input 2

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

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

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

Sample Output 3

139815



Sample input:
9 3
8 3
4 2
2 1


Sample output:
4

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
dp = [0]
for h in range(H):
    dp.append(min([dp[max(0, h + 1 - A)] + B for A, B in AB]))
print(dp[-1])

Submission status: Accepted

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


l = []
for i in range(n):
  a, b = map(int, input().split())
  l.append((a, b))
m = max(l, key=lambda x: x[0])[0]
dp = [9]*(h+m+1)
dp[0] = 0
for i in range(1, h+m+1):
  dp[i] = min([dp[i-l[j][0]]+l[j][1] for j in range(n)])
print(min(dp[h:h+m]))

Submission status: Accepted

Solution code:
H,N = list(map(int,input().split()))
dp = [float("inf")] * (H+1)

for i in range(N):
  a,b = list(map(int,input().split()))

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

print(dp[-1])

Submission status: Accepted

Solution code:
def main():
    import sys
    # readline = sys.stdin.readline
    readlines = sys.stdin.readlines
    H, N = map(int, input().split())
    M = [tuple(map(int, s.split())) for s in readlines()]
    INF = 10 ** 9
    dp = [INF] * (H + 1)
    dp[0] = 0
    
    for i in range(N):
        a, b = M[i]
        for j in range(H):
            nj = min(H, j + a)
            dp[nj] = min(dp[nj], dp[j] + b)

    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, N = map(int, input().split())
    M = [tuple(map(int, readline().split())) for _ in range(N)]
    INF = 10 ** 9
    dp = [INF] * (H + 1)
    dp[0] = 0
    
    for i in range(N):
        a, b = M[i]
        for j in range(H):
            nj = min(H, j + a)
            dp[nj] = min(dp[nj], dp[j] + b)

    print(dp[H])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for CASES in range(int(input())):
'''
inf = 100000000000000000  # 1e17
mod = 998244353

'''
# example input

'''
H, n = map(int, input().split())
bas = 10**4
F = [inf] * (bas*3 + 10)
A = [0] * (bas + 10)
B = [0] * (bas + 10)
for i in range(1, n + 1):
    A[i], B[i] = map(int, input().split())
F[0]=0
for i in range(1, n + 1):
    for j in range(A[i], bas * 3 + 5):
        F[j] = min(F[j], F[j - A[i]] + B[i])
ans = inf
for i in range(H, bas * 3 + 5):
    ans = min(ans, F[i])
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
h, n = map(int,input().split())
a = np.zeros(n,dtype=int)
b = np.zeros(n,dtype=int)
for i in range(n):
    ai ,bi = map(int,input().split())
    a[i] = ai
    b[i] = bi

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

print(mh[h])

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
l=[]
s=0
for i in range(n):
    a,b=map(int,input().split())
    s=max(s,a)
    l.append((a,b))
dp=[10**10]*(h+s)
dp[0]=0
for i in range(h):
    for s,t in l:
        dp[i+s]=min(dp[i]+t,dp[i+s])
print(min(dp[h:]))

Submission status: Accepted

Solution code:
# DP
H, N = map(int, input().split())
A, B = [], []
for _ in range(N):
    ai, bi = map(int, input().split())
    A.append(ai)
    B.append(bi)
maxA = max(A)


"""
DP[i] モンスターの体力をi減らすため消耗する魔力の最小値

答え min_{H <= i < H + maxAi} DP[i]
"""

INF = int(1e18)
DP = [INF for _ in range(H + maxA + 1)]
DP[0] = 0

# print(DP)
for item in range(N):
    ai, bi = A[item], B[item]
    for idp, value in enumerate(DP):
        if value != INF and idp + ai < len(DP):
            DP[idp + ai] = min(DP[idp + ai], DP[idp] + bi)

ans = min(DP[H:H+maxA+1])
print(ans)

Submission status: Accepted

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


def main():
    W, N = map(int, input().split())
    weight = []
    value = []
    for _ in range(N):
        A, B = map(int, input().split())
        weight.append(A)
        value.append(B)

    INF = 1000000007
    MAX_N = 1005
    MAX_W = 10005
    dp = [[INF for _ in range(MAX_W)] for _ in range(MAX_N)]
    for i in range(MAX_N):
        dp[i][0] = 0

    for i in range(N):
        for j in range(W + 1):
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            w = j + weight[i]
            if w >= W:
                w = W
            dp[i + 1][w] = min(dp[i + 1][w], dp[i + 1][j] + value[i])

    print(dp[N][W])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

H, N = map(int, input().split())
l = [tuple(map(int, input().split())) for _ in range(N)]
M = max([A for A, B in l])

dp = [0]*(H+M)
for i in range(1, H+M):
  dp[i] = min([B + dp[i-A] for A, B in l if i>=A]+[10**8])
ans = min([dp[i] for i in range(H, H+M)])
print(ans)

Submission status: Accepted

Solution code:
H ,N = [int(i) for i in input().split()]
AB = []
for _ in range(N):
    AB.append([int(i) for i in input().split(' ')])

M = max(A for A, B in AB)

DP = [0] * (H+M)

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:
H ,N = [int(i) for i in input().split()]
AB = []
for _ in range(N):
    AB.append([int(i) for i in input().split(' ')])
 
MAX = 0
for a, b in AB:
    MAX = a if MAX < a else MAX
 
DP = [10**10] * (H+MAX)
DP[0] = 0
for i in range(H):
    for a,b in AB:
        if DP[i] != 10**10:
            DP[i+a] = min(DP[i+a],DP[i] + b)
 
print(min(DP[H:]))

Submission status: Accepted

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

Submission status: Accepted

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

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

INFTY = 10**8
dp = [INFTY]*(H+1)
dp[0] = 0

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

print(dp[H])

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
h,n=f()
dp=[0]+[10**9]*h
for i in range(n):
  a,b=f()
  for j in range(h+1): dp[j]=min(dp[j],dp[max(j-a,0)]+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)]

dp = [[100000000]*(10000*2)]*(N+1)
dp[0][0] = 0

for i in range(0, N):
    for h in range(0, H+1):
        dp[i+1][h] = min(dp[i+1][h], dp[i][h])
        dp[i+1][min(h+AB[i][0], H)] = min(dp[i+1][min(h+AB[i][0], H)], dp[i+1][h]+AB[i][1])

ans = {}
for i in range(N):
    ans[i] = dp[i][H]
print(min(ans.values()))

Submission status: Accepted

Solution code:
def main():
    H, N = map(int, input().split())
    AB = [[]*2]*N
    for i in range(N):
        AB[i] = list(map(int, input().split()))

    dp = [[100000000]*(10000*2)]*(N+1)
    dp[0][0] = 0

    for i in range(0, N):
        for h in range(0, H+1):
            dp[i+1][h] = min(dp[i+1][h], dp[i][h])
            dp[i+1][min(h+AB[i][0], H)] = min(dp[i+1][min(h+AB[i][0], H)], dp[i+1][h]+AB[i][1])

    ans = {}
    for i in range(N):
        ans[i] = dp[i][H]
    print(min(ans.values()))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, n = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(n)]
m = max(A for A, B in AB)
dp = [0]*(h+m)

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:
from sys import stdin
from sys import  setrecursionlimit
setrecursionlimit(10000)

[H, N] = [int(x) for x in stdin.readline().rstrip().split()]
As = []
Bs=[]
for i in range(N):
    [A, B] = [int(x) for x in stdin.readline().rstrip().split()]
    As.append(A)
    Bs.append(B)

INF = 10**10
dp_map = [[INF]*(H+1) for _ in range(N+1)]

#dp_map[i][j]: i番目の魔法までを使って、最低でもjのダメージを与える時に必要なMP
dp_map[0][0]  = 0
for i in range(N):
    for j in range(H+1):
        if j<As[i]:
            dp_map[i+1][j] = min(dp_map[i+1][0]+Bs[i],dp_map[i][j])
        else:
            dp_map[i+1][j] = min(dp_map[i+1][j-As[i]]+Bs[i] ,dp_map[i][j])

print(dp_map[N][H])

Submission status: Accepted

Solution code:
import numpy as np
H, N = map(int, input().split())
ATK, MP = np.array(tuple(tuple(map(int, input().split())) for _ in range(N)), dtype=np.int32).T
table = np.zeros((H + ATK.max(),), dtype=np.int32)
for damage in range(1, H + 1):
    table[damage] = (table[damage - ATK] + MP).min()
print(table[H])

Submission status: Accepted

Solution code:
import numpy as np
H, N = map(int, input().split())
ATK, MP = np.array(tuple(tuple(map(int, input().split())) for _ in range(N)), dtype=np.int64).T
MAX_ATK = ATK.max()
table = np.zeros((H + MAX_ATK,), dtype=np.int64)
for damage in range(1, H + 1):
    table[damage] = (table[damage - ATK] + MP).min()
print(table[H])

Submission status: Accepted

Solution code:
import numpy as np
H, N = map(int, input().split())
ATK, MP = np.array(tuple(tuple(map(int, input().split())) for _ in range(N)), dtype=np.int64).T
MAX_ATK = ATK.max()
table = np.zeros((H + MAX_ATK,), dtype=np.int64)
for damage in range(1, H + 1):
    table[damage] = (table[damage - ATK[np.arange(N, dtype=np.int16)]] + MP[np.arange(N, dtype=np.int16)]).min()
print(table[H])

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
hpdown = list()
mpdown = list()
for i in range(N):
  a, b = map(int, input().split())
  hpdown.append(a)
  mpdown.append(b)
  
hmax = H+(max(hpdown)+1)
dp = [10 ** 9] * (hmax)
dp[0] = 0
for h in range(1, hmax):
  for n in range(N):
    if(h-hpdown[n]>=0):
      dp[h] = min(dp[h], dp[h-hpdown[n]] + mpdown[n])
      
ans = 10 ** 9
for i in range(H, hmax):
  if(dp[i] < ans):
    ans = dp[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


def main():
    H, N = map(int, input().split())
    A = [0 for _ in range(N)]
    B = [0 for _ in range(N)]
    for i in range(N):
        a, b = map(int, input().split())
        A[i] = a
        B[i] = b

    INF = 1000000005
    dp = [[INF for _ in range(H + 1)] for _ in range(N + 1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(H + 1):
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            dp[i + 1][min(j + A[i], H)] = min(dp[i + 1]
                                              [min(j + A[i], H)], dp[i + 1][j] + B[i])

    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
INF = float('inf')

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


# i個目までを使って与えた魔力の総和がjの時の最小コスト
dp = [[INF for j in range(H + 1)] for i in range(N + 1)]

dp[0][0] = 0

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

print(dp[N][H])

Submission status: Accepted

Solution code:
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    H, N = map(int, input().split())
    magic = [list(map(int, input().split())) for _ in range(N)]
    dp = [float("inf") for _ in range(H + 1)]
    dp[0] = 0

    for h in range(H):
        for m in magic:
            a, b = m[0], m[1]
            tmp = dp[h] + b
            if h + a >= H and tmp < dp[H]:
                dp[H] = tmp
            elif h + a < H and tmp < dp[h + a]:
                dp[h + a] = tmp
    print(dp[-1])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
INF=10**30
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=[[INF]*(H+10) for i in range(N+1)]
dp[0][0]=0
for i in range(N):
    for j in range(H+1):
        if(j<A[i]):
            dp[i+1][j]=min(dp[i][j],dp[i][0]+B[i])
        else:
            dp[i+1][j]=min(dp[i][j],dp[i+1][j-A[i]]+B[i])
print(dp[N][H])

Submission status: Accepted

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

# 体力をn削るために必要なmpの最小値
dp = [0] * (H+1)
for h in range(1, H+1):
    dp[h] = min([dp[max(h-a, 0)] + b for (a, b) in magics])

print(dp[-1])

Submission status: Accepted

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

dp = [10**10]*(h+1)
dp[h] = 0
for i in range(h,0,-1):
    for j in ab:
        tmp = max(0,i-j[0])
        dp[tmp] = min(dp[tmp], dp[i]+j[1])

print(dp[0])

Submission status: Accepted

Solution code:
###### Importing Libraries ######
import math
import operator as op
from functools import reduce
from fractions import Fraction as frac
#################################
##### User defined function #####

############ NCR ###############
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom
    
############ READ ###############
def rd(p):
    if(p==0):
        return input()
    if(p==1):
        return int(input())
    if(p==2):
        return map(int,input().split())
    if(p==3):
        return list(map(int,input().split()))


############ DSU ###############
global par
# par=[-1 for i in range(100001)]
# t=rd(1)
def find(u):
    if(par[u]<0):
        return u
    else:
        return find(par[u])
def uni(x,y):
    u=find(x)
    v=find(y)
    if u==v:
        return
    if(par[u]<par[v]):
        par[u]+=par[v]
        par[v]=u
    else:    
        par[v]+=par[u]
        par[u]=v
################################        
        

t=1
for term in range(1,t+1):
    h,n=rd(2)
    P=[list(map(int,input().split())) for i in range(n)]
    x=max(a for a,b in P)
    DP=[0]*(h+x)
    for i in range(1,h+x):
        DP[i]=min(DP[i-a]+b for a,b in P)
    print(DP[h])

Submission status: Accepted

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

# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    h, n = input_to_int_map()
    ab = [input_to_int_tuple() for i in range(n)]

    x = max(a for a, b in ab)

    dp = [0] * (h + x)
    for i in range(1, h + 1):

        dp[i] = min(dp[i - a] + b for a, b in ab)

    return dp[h]


if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    H, N = map(int, input().split())
    inf = float('inf')
    AB = [tuple(map(int, input().split())) for _ in range(N)]
    dp = [inf] * (H+10**4+1)
    dp[0] = 0
    for h in range(H+1):
        for a, b in AB:
            dp[h+a] = min(dp[h+a], dp[h]+b)
    print(min(dp[H:]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, N = map(int,input().split())
M = [list(map(int,input().split())) for k in range(N)]
dp = [float("inf")]*(H+1)
dp[0] = 0

for e in M:
    a, b = e[0], e[1]
    for k in range(min(a+1,H+1)):
        dp[k] = min(dp[k],b)
    for k in range(a+1,H+1):
        dp[k] = min(dp[k],dp[k-a]+b)
print(dp[H])

Submission status: Accepted

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

INF = float('inf')
dp = [INF] * (H+1)
dp[H] = 0
for a,b in AB:
    for h in range(H,0,-1):
        i = max(0,h-a)
        dp[i] = min(dp[i], dp[h]+b)
print(dp[0])

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(n)]
A = [a for a, b in AB]
B = [b for a, b in AB]
inf = 10**10
# DP[i][j]=i個以内で体力j以上削るための消費魔力の最小値
DP = [[inf for j in range(h+1)] for i in range(n+1)]
DP[0][0] = 0
for i in range(n):
    for j in range(h+1):
        DP[i+1][j] = min(DP[i][j], DP[i+1][max(0, j-A[i])]+B[i])
print(DP[n][h])

Submission status: Accepted

Solution code:
def main():
  H, N = (int(_) for _ in input().split())
  A = [[int(_) for _ in input().split()] for __ in range(N)]
  INF = 10 ** 15
  dp = [[INF] * (H + 1) for _ in range(N+1)]
  dp[0][0] = 0
  for i in range(N):
    for j in range(H+1):
      dp[i+1][j] = min(dp[i+1][j], dp[i][j])
      if A[i][0] <= j < H:
        dp[i+1][j] = min(dp[i+1][j], A[i][1] + dp[i+1][j-A[i][0]])
      elif j == H:
        for k in range(max(j - A[i][0], 0), j):
          dp[i+1][j] = min(dp[i+1][j], A[i][1] + dp[i+1][k])
  print(dp[N][H])
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

INF = 10**18
dp = [INF]*(h+1)
#dp[i]: hpがiの相手に消費する魔力の最小値
dp[0]=0

for i in range(h):#i:累積ダメージi
    for j in range(n):#
        if i+ab[j][0]<=h: #
            dp[i+ab[j][0]]=min(dp[i+ab[j][0]],dp[i]+ab[j][1])
        else:
            dp[-1]=min(dp[-1],dp[i]+ab[j][1])
print(dp[-1])

Submission status: Accepted

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

Max_A = max(a for a, b in AB)
# print(Max_A)

dp = [10 ** 18] * (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())
ab = [list(map(int,input().split())) for _ in range(N)]
dp = [float('inf')]*(H+1)
dp[0] = 0
#dp[i]:モンスターにiダメージ与えるための最小コスト
for h in range(H):
    for i in range(N):
        #dmgはHを超えてもHとして扱う
        dmg = min(h+ab[i][0],H)
        #costを更新
        dp[dmg] = min(dp[dmg],dp[h]+ab[i][1])
print(dp[H])

Submission status: Accepted

Solution code:
H,n=map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(n)]
maxAi= max(a for a,b in AB)
dp=[0]*(H+maxAi)
i=1
while True:
  try:
    dp[i]=min(dp[i-a]+b for a,b in AB)
    i+=1
  except:
    print(min(dp[H:]))
    break

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    H, N = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(N)]
    maxA = max(AB)[0]

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

    for j in range(H + maxA, -1, -1):
        if j == 0:
            dp[0][j] = 0
        else:
            factor = j // AB[0][0] if j % AB[0][0] == 0 else j // AB[0][0] + 1
            dp[0][j] = factor * AB[0][1]

    for i in range(N):
        for j in range(H + maxA + 1):
            if j - AB[i][0] >= 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]

    print(min([dp[N][j] for j in range(H, H + maxA + 1)]))

Submission status: Accepted

Solution code:
H, N = map(int,input().split())
magic = []
for i in range(N):
    a,b = map(int,input().split())
    magic.append((a, b))
magic.sort(reverse=True)
amax, _ = magic[0]

INF = 1<<60

def solve():
    btab = [INF] * (H+1)
    btab[0] = 0
    oneshot = min(amax, H)
    for h in range(1, oneshot+1):
        btab[h] = min([b for a, b in magic if a >= h])
    for h in range(2, H+1):
        for a, b in magic:
            c = b + btab[max(h-a, 0)]
            if c < btab[h]:
                btab[h] = c
    return btab[H]

print(solve())

Submission status: Accepted

Solution code:
H,N=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N)]
x=max(a for a,b in ab)
dp=[0]*(H+x)
for i in range (1,H+x):
    dp[i]=min(dp[i-a]+b for a,b in ab)
print(min(dp[H:]))

Submission status: Accepted

Solution code:
# Problem E - Crested Ibis vs Monster

# input
H, N = map(int, input().split())
magic_list = []
for i in range(N):
    A, B = map(int, input().split())
    magic_list.append([A, B])

# initialization
dp = [float('INF')]*(H+1)
dp[0] = 0
mg_len = len(magic_list)

# dp
for i in range(1, H+1):
    for j in range(mg_len):
        a = magic_list[j][0]
        b = magic_list[j][1]
        last_p = i - a
        if last_p<0:
            last_p = 0
        tmp = dp[last_p] + b
        dp[i] = min(dp[i], tmp)

# output
print(dp[H])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    inf = 10**17
    #mod = 10**9 + 7

    h,n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    dp = [inf] * (h+1)
    dp[0] = 0
    for i in range(h+1):
        for a,b in ab:
            if i+a > h:
                c = h
            else:
                c = i + a
            dp[c] = min(dp[c], dp[i]+b)

    print(dp[-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    inf = 10**17
    #mod = 10**9 + 7

    h,n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]
    dp = [inf] * (3*(10**4))
    dp[0] = 0
    for i in range(3*(10**4)):
        for a,b in ab:
            if i-a >= 0:
                dp[i] = min(dp[i], dp[i-a]+b)
    res = inf
    for i in range(h, 3*(10**4)):
        res = min(res, dp[i])
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    H, N = (int(i) for i in input().split())
    AB = [[int(i) for i in input().split()] for j in range(N)]
    A = [a[0] for a in AB]
    B = [b[1] for b in AB]
    dp = [[10**9+7]*(H+1) for _ in range(N+1)]
    for i in range(N+1):
        dp[i][0] = 0
    for i in range(N):
        for j in range(1, H+1):
            lessHP = max(j - A[i], 0)
            dp[i+1][j] = min(dp[i+1][lessHP] + B[i], dp[i][j])
    print(dp[N][H])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h, n = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
#(a,b):=(ダメージ,消費魔力)

dp = [float('inf')] * (h + 1)
dp[0] = 0

for i in range(h + 1):
    for j in range(n):
        if dp[i] == float('inf'):continue
        temp_a = i + ab[j][0] if i + ab[j][0] < h else h
        temp_b = dp[i] + ab[j][1]

        dp[temp_a] = min(dp[temp_a], temp_b)

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

Submission status: Accepted

Solution code:
h, n = map(int, input().split())
A, B = zip(*[tuple(map(int, input().split()))for _ in range(n)])
INF = 10**18
dp = [INF]*(h+1)
dp[0] = 0
for j in range(h):
    for i in range(n):
        if h < j+A[i]:
            dp[-1] = min(dp[-1], dp[j]+B[i])
        else:
            dp[j+A[i]] = min(dp[j+A[i]], dp[j]+B[i])
print(dp[-1])

Submission status: Accepted

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

data = []

for i in range(N):
    a, b = map(int, input().split())
    data.append({'a': a, 'b': b})

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


for i in range(N):
    dp[i][0] = 0

for j in range(H+1):
    dp[0][j] = -(-j // data[0]['a']) * data[0]['b']

for i in range(1, N):
    power = data[i]['a']
    magic = data[i]['b']

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

# print(dp)
print(dp[N-1][H])

Submission status: Accepted

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

A = [A_B[i][0] for i in range(N)]
B = [A_B[i][1] for i in range(N)]

M = H + max(A)

#dp[i][j] : i番目までの中から魔法を選んで使い体力をj減らすときの魔力の最小値

def crested(H, N, A, B):
    inf = float('inf')
    dp = [inf for j in range(M+1)]
    dp[0] = 0

    for i in range(N):
        for j in range(A[i],M):
            dp[j] = min(dp[j], dp[j-A[i]]+B[i])
    
    ans = inf
    for k in range(H, M):
        ans = min(ans, dp[k])
    print(ans)

crested(H, N, A, B)

Submission status: Accepted

Solution code:
H,N = map(int,input().split())
magicL =[]
for x in range(N):
    magic = list(int(i) for i in input().split())
    magicL.append(magic)
 
ml = magicL
dp = [10**9 for i in range(H +1)]
dp [0] = 0
for i in range(N):
    for w in range(H +1):
        dp[min(w+ml[i][0],H)] = min(dp[min(w+ml[i][0],H)],dp[w]+ml[i][1])
print(dp[H])

Submission status: Accepted

Solution code:
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
maxAi = max(a for a,b in AB)
dp = [0]*(H+maxAi)

for i in range(1,H+maxAi):
  dp[i] = min(dp[i-a] + b for a,b in AB)
print(min(dp[H:H+maxAi]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    h, n = LI()
    ab = LIR(n)
    maxa = max(map(lambda x: x[0], ab))
    rangeh = h + maxa
    dp = [inf] * (rangeh + 1)
    dp[0] = 0
    for a,b in ab:
        for hi in range(rangeh - a):
            dp[hi + a] = min(dp[hi + a], dp[hi] + b)
        for hi in range(rangeh-1, -1, -1):
            dp[hi] = min(dp[hi], dp[hi + 1])
    print(dp[h])
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

h,n = l_in()
ab = []
for _ in range(n):
    ab.append(l_in())
    
## dp[i][x] iまでの魔法をいくつ使うか確定させて，ダメージがxの時の最小魔力
dp = [10**8 for _ in range(2*h)]

dp[0] = 0

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

print(min(dp[h:]))