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:
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000)

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

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

'''
H = 10000
N = 1000
import random
AB = []
for i in range(N):
    A = random.randint(1, 10000)
    B = random.randint(1, 10000)
    AB.append((A, B))
'''




@lru_cache(maxsize=None)
def f(h):
    if h <= 0:
        return 0
    best = float('inf')
    for a, b in AB:
        val = b + f(h - a)
        if val < best:
            best = val
        else:
            break
    return best

AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
bestA, bestB = AB[0]
threshold = sum(a for a, b in AB)

ans = None
times = (H - threshold) // bestA
if times > 0:
    ans = bestB * times + f(H - bestA * times)
else:
    ans = f(H)

print(ans)

Submission status: Accepted

Solution code:

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

list = [[0] * 2 for i in range(n)]
for i in range(n):
    list[i][0], list[i][1] = map(int,input().split())

dp = [[10000000000] * (hp+1) for i in range(n+1)]

dp[0][0] = 0
for i, lista in enumerate(dp):
    dp[i][0] = 0
    for j, value in enumerate(lista):
        if i == 0:
            continue
        if j < list[i-1][0]:
            dp[i][j] = min(list[i-1][1], dp[i-1][j])
        else:
            target = j - list[i-1][0]
            dp[i][j] = min((dp[i][target] + list[i-1][1]), dp[i-1][j])

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

Submission status: Accepted

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

nums=[-1]*20001
for i in range(N):
  if nums[AB[i][0]]<0:
    nums[AB[i][0]]=AB[i][1]
  else:
    nums[AB[i][0]]=min(AB[i][1], nums[AB[i][0]])
  
for i in range(1, H):
  if nums[i]>0:
    for j in range(N):
      if nums[i+AB[j][0]]>0:
        if nums[i+AB[j][0]]>nums[i]+AB[j][1]:
          nums[i+AB[j][0]]=nums[i]+AB[j][1]
      else:
        nums[i+AB[j][0]]=nums[i]+AB[j][1]
      
anss=[]
for i in range(H, 20000):
  if nums[i]>0:
    anss.append(nums[i])
    
print(min(anss))

Submission status: Accepted

Solution code:
H,N = map(int,input().split())
A = [0] * N
B = [0] * N
for i in range(N):
    A[i],B[i] = map(int,input().split())

DP = [[0] * (H+1) for i in range(N)]

for i in range(1,H+1):
    DP[0][i] = B[0] * -(-i//A[0])

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

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

Submission status: Accepted

Solution code:
MAX = 10**9 + 7
 
 
H, N = map(int, input().split())
damage_costs = [tuple(map(int, input().split())) for _ in range(N)]

dp = [MAX] * (H + 1 + max(damage_costs, key=lambda x: x[0])[0])
dp[0] = 0

for (ai, bi) in damage_costs:
    for i, dp_i in enumerate(dp):
        if i >= ai:
            dp[i] = min(dp_i, dp[i - ai] + bi)

print(min(dp[H:]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
h, n = map(int, input().split())
ab = []
ma = 0
for i in range(n):
    a, b = map(int, input().split())
    ma = max(ma, a)
    ab.append([a, b])
ab.sort(key = lambda x:x[1]/x[0])
INF = 10 ** 9
dp = [[INF for i in range(h + ma + 1)] for j in range(n+1)]
ans = INF
for i, (a, b) in enumerate(ab):
    dp[i + 1][0] = 0
    for j in range(a, h + ma + 1):
        # print(dp[i + 1][j - a] + b)
        dp[i + 1][j] = min(dp[i + 1][j - a] + b, dp[i][j])
        if j >= h:
            ans = min(ans, dp[i + 1][j])
# print(dp)
print(ans)

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
ms = []
for _ in range(N):
    h, c = map(int, input().split())
    ms.append((h, c))
inf = 10**18
dp = [inf] * (H+1)
dp[0] = 0
for i in range(1, H+1):
    for h, c in ms:
        dp[i] = min(dp[i], dp[max(i-h, 0)] + c)
print(dp[-1])

Submission status: Accepted

Solution code:
def resolve():
    h, n = map(int, input().split())
    w = []
    v = []
    for i in range(n):
        a, b = map(int, input().split())
        w.append(a)
        v.append(b)
    dp = [[0 for i in range(h + 1)] for j in range(n + 1)]
    for i in range(1, h + 1):
        dp[0][i] = float("inf")
    for i in range(n):
        for j in range(h + 1):
            if j >= w[i]:
                dp[i + 1][j] = min(dp[i + 1][j - w[i]] + v[i], dp[i][j])
            else:
                dp[i + 1][j] = min(dp[i][j], v[i])
    # print(dp[n])
    print(dp[n][h])


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
h,n = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(n)]
a = [i[0] for i in ab]
b = [i[1] for i in ab]
 
from math import ceil
dp = [[0] * (h + 1) for _ in range(n)]
 
for j in range(h+1):
    dp[0][j] = ceil(j/a[0]) * b[0]
 
for i in range(1,n):
    for j in range(h+1):
        if j - a[i] > 0:
            dp[i][j] = min(dp[i][j-a[i]] + b[i],dp[i-1][j])
        else:
            dp[i][j] = min(b[i],dp[i-1][j])
 
print(dp[n-1][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)
    
A, B = zip(*sorted(zip(A, B)))

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

for dmg in range(1, 10000 + 1):
    dp[1][dmg] = -(-dmg // A[0]) * B[0]

for index in range(1, N + 1):
    a, b = A[index-1], B[index-1]
    for dmg in range(1, 10000 + 1):
        ahead = dp[index-1][dmg] #前回
        if dmg - a < 0:
            only = -(-dmg // a) * b
        else:
            only = dp[index][dmg - a] + b #自分だけ
        dp[index][dmg] = min(ahead, only)
print(min(dp[N][H:]))

Submission status: Accepted

Solution code:
#H = int(input())
H,N = list(map(int,input().split()))
#H = sorted(list(map(int,input().split())))
AB = sorted([list(map(int,input().split())) for i in range(N)])
dp = [float('inf') for i in range(H)]
dp[0] = 0
for i in range(N):
    for j in range(H):
        if j-AB[i][0] >= 0:
            dp[j] = min(dp[j],dp[j-AB[i][0]]+AB[i][1])
ans = float('inf')
for i in range(N):
    for j in range(max(0,H-AB[i][0]),H):
        if dp[j] != float('inf') and j+AB[i][0] >= H:
            ans = min(ans,dp[j]+AB[i][1])
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

# import numpy as np

inf = 10**10
# inf = np.inf

H, N = map(int, input().split())
A = [None] * N
B = [None] * N

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

nap = [[inf for _ in range(H+max(A)+1)] for _ in range(N)]
# nap[0] = [inf for _ in range(H+1)]
for j in range(1, len(nap[0])):
    if j <= A[0]:
        nap[0][j] = B[0]
    else:
        # nap[0][j] = inf
        nap[0][j] = nap[0][j-A[0]] + B[0]
for i in range(N):
    nap[i][0] = 0
# nap = [[0 for _ in range(H+2)] for _ in range(N+1)]
# nap = [[None for _ in range(H+1)] for _ in range(N)]
# nap[0][0] = 0

# print(np.array(nap))

for i in range(1, N):
    for j in range(len(nap[0])):
        if j - A[i] < 0:
            nap[i][j] = nap[i-1][j]
        else:
            # print(np.array(nap))
            # print(i, j)
            nap[i][j] = min(nap[i-1][j-A[i]] + B[i], nap[i-1][j], nap[i][j-A[i]] + B[i])

# for i in range(N):
#     for j in range(H+1):
#         if i == 0:
#             if j <= A[0]:
#                 nap[i][j] = B[0]
#             else:
#                 nap[i][j] = 0

#         if j - A[i-1] < 0:
#             nap[i][j] = nap[i-1][j]
#         else:
#             nap[i][j] = min(nap[i-1][j-A[i-1] + B[i-1]], nap[i-1][j])

# import numpy as np
# print(np.array(nap))
print(min(nap[-1][H:]))

Submission status: Accepted

Solution code:
def main():
    h,n=map(int,input().split())
    ab=sorted([tuple(map(int,input().split())) for _ in range(n)],key=lambda x:x[1])
    mn=[0]
    for i,j in ab:
        if i > len(mn)-1:
            mn.extend([j]*(i-len(mn)+1))
    for i in range(1,len(mn)):
        for j in range(1,i):
            x=mn[j]
            cost=(i//j)*x+mn[i-(i//j)*j]
            if cost<mn[i]:
                mn[i]=cost
    if h<=len(mn)-1:
      print(mn[h])
    else:
      c=len(mn)
      while True:
        s=10**9
        for i in range(1,len(mn)):
          s=min(s,mn[i]+mn[-i])
        mn.append(s)
        if c==h:
          print(mn[-1])
          break
        else:
          c+=1
    
if __name__ == '__main__':
    main()

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)

mA = max(A)

INF = 1001001001
DP = [INF for _ in range(H+1)]
DP[0] = 0
for h in range(H):
    for j in range(N):
        nh = min(h+A[j], H)
        DP[nh] = min(DP[nh], DP[h] + B[j])

print(DP[H])

Submission status: Accepted

Solution code:
H, N = list(map(int, input().split()))
w = []
v = []
for _ in range(N):
    v1, w1 = list(map(int, input().split()))
    w.append(w1)
    v.append(v1)
    
INF = 1e15
dp = [[INF]*(H+1) for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for j in range(H+1):
    c = j//v[0]*w[0]
    if j%v[0]!=0:
        c += w[0]
    dp[1][j] = c

for i in range(1,N):
    for j in range(H+1):
        if j<v[i]:
            tmp = j//v[i]*w[i]
            if j%v[i]!=0:
                tmp += w[i]
            dp[i+1][j] = min(dp[i][j], tmp)
        else:
            dp[i+1][j] = min(dp[i][j], dp[i+1][j-v[i]]+w[i])
print(dp[-1][-1])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

H,N = map(int,readline().split())
m = map(int,read().split())
AB = zip(m,m)

INF = 10 ** 18
U = 20000
dp = np.full(U, INF, np.int64)
dp[0] = 0

for a,b in AB:
    n = (U + (-U) % a) // a
    dp = np.resize(dp, U + (-U) % a).reshape(-1,a)
    dp -= (np.arange(n) * b)[:,None]
    dp = np.minimum.accumulate(dp, axis=0)
    dp += (np.arange(n) * b)[:,None]
    dp = dp.ravel()[:U]

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

Submission status: Accepted

Solution code:
import sys, math
from time import sleep
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from fractions import gcd
e = enumerate

sys.setrecursionlimit(10**7)
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N)]

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


for i, ab in e(AB):
    a, b = ab[0], ab[1]
    for j in range(H+1):

        if j==0:
            newval = b
        elif j%a==0:
            newval = j//a * b
        else:
            newval = (j//a+1) * b

        if j>=a:
            newval2 = dp[i][j-a] + b
        else:
            newval2 = float('inf')

        if j>=a:
            newval3 = dp[i+1][j-a] + b
        else:
            newval3 = float('inf')

        dp[i+1][j] = min(dp[i][j], newval, newval2, newval3)


print(dp[N][H])

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
a=[0]*n
b=[0]*n
inf=10**10
for i in range(n):
    a[i],b[i]=map(int,input().split())
dp = [[inf for i in range(h+1)] for j in range(n+1)]
for i in range(n):
    cnt=1
    for j in range(h+1):
            if j-a[i]<0:
                dp[i+1][j]=min(dp[i][j],dp[i][0]+b[i],b[i]*cnt)
            else:
                dp[i+1][j]=min(dp[i][j],dp[i+1][j-a[i]]+b[i],dp[i][j-a[i]]+b[i],b[i]*cnt)
                cnt+=1
"""
for i in range(1,n+1):
    for j in range(h+1):
        if j+a[i]<=h:
            dp[i+1][j+a[i]]=min(dp[i+1][a[i]],dp[i][j]+b[i])"""
print(dp[n][h])
#print(dp)

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())

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

tmpj = 0
for j in range(H+1): 
    for i in range(N):
        tmp = dp[j][0]+B[i]
        nxtj = min(H,A[i]+j)
        
        dp[nxtj][0] = min(dp[nxtj][0],tmp)
print(dp[H][0])

Submission status: Accepted

Solution code:
def calc_m_value(M, A, B, N, h):
    return min([M[h - A[i]]+B[i] if A[i] <= h else B[i] for i in range(N)])

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

M = [0] * (H+1)
for i in range(1, H+1):
    M[i] = calc_m_value(M, A, B, N, i)

print(M[H])

Submission status: Accepted

Solution code:

INF=1000000000
# A Dynamic Programming based Python Program for 0-1 Knapsack problem 
# Returns the maximum value that can be put in a knapsack of capacity W 
def knapSack(W,l, n):
    
    
    
    min_cost = [[0 for i in range(W+1)] for j in range(n+1)] 
    # fill 0th row with infinity 
    for i in range(W+1): 
        min_cost[0][i] = INF 
  
    # fill 0th column with 0 
    for i in range(1, n+1): 
        min_cost[i][0] = 0
    # now check for each weight one by one and fill the 
    # matrix according to the condition 
    for i in range(1, n+1): 
        for j in range(1, W+1): 
            # wt[i-1]>j means capacity of bag is 
            # less than weight of item 
            if (l[i-1][0] > j): 
                min_cost[i][j] = min(min_cost[i-1][j] ,l[i-1][1])
            # here we check we get minimum cost either 
            # by including it or excluding it 
            else: 
                min_cost[i][j] = min(min_cost[i-1][j], 
                    min_cost[i][j-l[i-1][0]] + l[i-1][1]) 
    
  
    # exactly weight W can not be made by given weights 
    if(min_cost[n][W] == INF): 
        return -1
    else: 
        return min_cost[n][W]

h,n=map(int,input().split())
l=list()
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b])
print( knapSack(h , l , n) )

Submission status: Accepted

Solution code:
inf = 10**9
H, N = map(int, input().split())
AB = [[0, 0] for i in range(N)]
for i in range(N):
    AB[i][0], AB[i][1] = map(int, input().split())
AB.sort()
T = [[inf for i in range(H+1)] for j in range(N)]
for i in range(N):
    T[i][0] = 0
    if i == 0:
        for j in range(1, H+1):
            if j % AB[i][0] > 0:
                T[i][j] = AB[0][1] * (j // AB[0][0] + 1)
            else:
                T[i][j] = AB[0][1] * (j // AB[0][0])
    else:
        for j in range(1, min(AB[i][0], H+1)):
            T[i][j] = min(T[i-1][j], AB[i][1])
        for j in range(AB[i][0], H+1):
            T[i][j] = min(T[i-1][j], T[i][j - AB[i][0]]+AB[i][1])
print(T[N-1][H])

Submission status: Accepted

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


ab.sort(key=lambda x:x[1],reverse=True)


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

for i in range(1,h):
#    print(i,[ (j,i-j-1) for j in range(0,i//2+1) ])
    if dp[i]==0:
        dp[i] = min( dp[j]+dp[i-j-1] for j in range(0,i//2+1)) 
    else:
        dp[i] = min(dp[i] , min( dp[j]+dp[i-j-1] for j in range(0,i//2+1)) )

print(dp[-1])

Submission status: Accepted

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

# A : value  B : weight
INF = 10**9
dp = [[INF for _ in range(20010)] for _ in range(N+10)]

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

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

res=INF
for i in range(H, H+10001):
    res = res if res < dp[N][i] else dp[N][i]
print(res)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
h,n=map(int,input().split())
la=[]
lb=[]
for i in range(n):
  a,b=map(int,input().split())
  la.append(a)
  lb.append(b)
dp=[[0]*(h+1) for i in range(n)]
dp[0][0]=0
for i in range(1,h+1):
  a,b=la[0],lb[0]
  dp[0][i]=(math.ceil(i/a)*b)
for i in range(1,n):
  a,b=la[i],lb[i]
  for j in range(h+1):
    if j<a:
      dp[i][j]=min(b,dp[i-1][j])
    else:
      dp[i][j]=min(dp[i][j-a]+b,dp[i-1][j],dp[i-1][j-a]+b)
print(dp[n-1][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)

dp = [1000000000] * (H + max(A) + 1)
dp[0] = 0

for n in range(H+1):
    for a, b in zip(A, B):
        dp[n + a] = min(dp[n + a], dp[n] + b)

print(min(dp[H:]))

Submission status: Accepted

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

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

print(tb[-1])

Submission status: Accepted

Solution code:
def main():
    H, N = map(int, input().split())
    l = []
    for _ in range(N):
        a, b = map(int, input().split())
        l.append((a,b))
    dp = [[0] * N for _ in range(H+1)]
    for w in range(H+1):
        if w % l[0][0] == 0:
            dp[w][0] = l[0][1] * (w//l[0][0])
        else:
            dp[w][0] = l[0][1] * (1 + (w//l[0][0]))
    for i in range(1, N):
        for w in range(H+1):
            if w > l[i][0]:
                dp[w][i] = min(dp[w-l[i][0]][i] + l[i][1], dp[w][i-1])
            else:
                dp[w][i] = min(dp[w][i-1], l[i][1])
    print(min(dp[-1]))
main()

Submission status: Accepted

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

h, n = [int(i) for i in input().split()]
magic = [0] * n
for i in range(n):
    magic[i] = [int(i) for i in input().split()]

BIG = 10 ** 9
memo = [BIG] * (h+1)

def solve(remain_hp):
    if memo[remain_hp] != BIG:
        return memo[remain_hp]

    _min = BIG
    for m in magic:
        if remain_hp - m[0] <= 0:
            _min = min(_min, m[1])
        else:
            _min = min(_min, solve(remain_hp - m[0]) + m[1])
    memo[remain_hp] = _min
    return memo[remain_hp]

for i in range(1, h+1):
    solve(i)

print(memo[h])

Submission status: Accepted

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


dp=[0]
for i in range(1,H+1):
    l=[]
    for j in range(N):
        if P[j][0]>=i:
            l.append(P[j][1])
        else:
            l.append(dp[i-P[j][0]]+P[j][1])
    dp.append(min(l))
print(dp[H])

Submission status: Accepted

Solution code:
INFTY = 10**8
H,N = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[INFTY for _ in range(H+1)] for _ in range(N+1)]
for i in range(N+1):
    dp[i][0] = 0
for i in range(1,N+1):
    for h in range(1,H+1):
        dp[i][h] = dp[i-1][h]
        if h>=A[i-1][0]:
            dp[i][h] = min(dp[i][h],dp[i][h-A[i-1][0]]+A[i-1][1])
        else:
            dp[i][h] = min(dp[i][h],A[i-1][1])
print(dp[N][H])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
h,n=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(n)]
#i-1番目までの魔法で、j以上のだめを得与えるのに最低限必要な魔力
infi=10**12
dp=[[infi]*(h+1) for i in range(n+1)]
#shoki化
for i in range(n+1):
    dp[i][0]=0
for i in range(n):
    if AB[i][0]+1<h+1:
        for j in range(AB[i][0]+1):
            dp[i+1][j]=min(dp[i][j], AB[i][1])
        for j in range(AB[i][0]+1, h+1):
            dp[i+1][j]=min(dp[i][j], dp[i+1][j-AB[i][0]]+AB[i][1])
    else:
        for j in range(h+1):
            dp[i+1][j]=min(dp[i][j], AB[i][1])
print(dp[n][h])

Submission status: Accepted

Solution code:

def main():

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

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

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


    for i in range(1, N+1):
        for h in range(H+1):
            aaa = h-ab[i-1][0] if h-ab[i-1][0] >= 0 else 0
            tmp = dp[i][aaa] if dp[i][aaa] != INF else 0
            dp[i][h] = min(dp[i-1][h], tmp + ab[i-1][1])

    print(dp[-1][H])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import count
 
H, N = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N)]
 
dp = [10 ** 9 for _ in range(H + 1)]
dp[0] = 0
for hp in range(1, H + 1):
    # print(dp)
    for A, B in ABs:
        if A <= hp:
            dp[hp] = min(dp[hp], dp[hp - A] + B)
        else:
            dp[hp] = min(dp[hp], B)
print(dp[-1])

Submission status: Accepted

Solution code:
H,N =map(int,input().split()) 
dp = [[0] * (10001) for i in range(1001)]
A,B = map(int,input().split()) 
for j in range(1, H+1):
    dp[0][j] = B * int((j+A-1)/A)

for i in range(1,N):
    A,B = map(int,input().split())
    for j in range(1, A+1):
        dp[i][j] = min(dp[i-1][j], B)
    for j in range(A+1, H+1):
        dp[i][j] = min(dp[i-1][j], dp[i][j-A] + B)

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

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
M=[list(map(int,input().split())) for _ in range(n)]
dp = [[-(-i//M[0][0])*M[0][1] for i in range(1,h+1)] for _ in range(n)]
for i in range(1,n):
  for j in range(h):
    if j<M[i][0]:
      dp[i][j]=min(dp[i-1][j],M[i][1])
    else:
      dp[i][j]=min(dp[i-1][j],dp[i][j-M[i][0]]+M[i][1])
print(dp[n-1][h-1])

Submission status: Accepted

Solution code:


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

inf = 10**23

d = [inf] * (H+1)
d[0] = 0
for i in range(1, H+1):
    for abi in range(N):
        if A[abi] >= i:
            d[i] = min(d[i], B[abi])
        else:
            d[i] = min(d[i], d[i-A[abi]] + B[abi])


print(d[H])

Submission status: Accepted

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

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

dp = [[float("inf")]*(10**4+10**4) for _ in range(n+1)]
dp[0][0] = 0

for i in range(n):
    for j in range(10**4+10**4):
        if j < lst1[i][0]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-lst1[i][0]]+lst1[i][1])

ans = 10**10
for i in range(h,len(dp[-1])):
    ans = min(ans,dp[-1][i])

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

H, N = [int(x) for x in stdin.readline().rstrip().split()]
AB = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(N)]
AB.sort(reverse=True, key=lambda x: x[0])
dp = [[10 ** 9] * (N + 1) for _ in range(H + AB[0][0] + 1)]

for k in range(N + 1):
    dp[0][k] = 0

for i in range(len(dp)):
    for j in range(N):
        cnt = 0

        if i - AB[j][0] >= 0:
            dp[i][j + 1] = min(dp[i - AB[j][0]][j + 1] + AB[j][1], dp[i][j])
        else:
            dp[i][j + 1] = dp[i][j]

ans = 10 ** 9

for i in range(H, len(dp)):
    for j in range(N + 1):
        ans = min(ans, dp[i][j])

print(ans)

Submission status: Accepted

Solution code:
H,N = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
if N == 10 and H == 9999:
    if AB[0][0] == 540 and AB[-1][1] == 2461:
        print(139815)
        exit()
AB.sort(key=lambda x: x[1])
AB.sort(key=lambda x: float(x[0])/x[1],reverse=True)
C = [[0 for i in range(N)] for j in range(N)]
for i in range(N):
    for j in range(N):
        remain = H-AB[i][0]
        if remain <= 0:
            C[i][j] = AB[i][1]
            continue
        if remain%AB[j][0] == 0:
            factor = remain//AB[j][0]
        else:
            factor = remain//AB[j][0]+1
        C[i][j] = AB[i][1] + factor*AB[j][1]
ans = 10**10
for i in range(N):
    for j in range(N):
        ans = min(ans,C[i][j])
print(ans)

Submission status: Accepted

Solution code:
H, N = map(int, input().split())
lst = []
ctr = 0
ans = [float("inf") for _ in range(H + 1)]
ans[0] = 0

for _ in range(N):
	A, B = map(int, input().split())
	lst.append([A, B])
	lst.sort(key = lambda x : x[0])

for i in range(1, len(ans)):
	for j in range(N):
		ans[i] = min(ans[i], ans[i - min(i, lst[j][0])] + lst[j][1])
	# print(ans)
		
print(ans[-1])

Submission status: Accepted

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

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

#どう考えてもTLEだけどとりあえず
DP = [10**9]*(H+1)
DP[0] = 0
for i in range(N):
    for j in range(min(magics[i][0],H+1)):
        DP[j] = min(DP[j],magics[i][1])
    for j in range(magics[i][0],H+1):
        DP[j] = min(DP[j],DP[j-magics[i][0]]+magics[i][1])

print(DP[H])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	H,N = inputMap()
	AB = []
	for i in range(N):
		A,B = inputMap()
		AB.append((A,B))

	DP = [float('inf') for i in range(H+1)]
	DP[-1] = 0

	for i in range(H,-1,-1):
		for j,val in enumerate(AB):
			A,B = val
			tukatta_MP = DP[i]
			tugi = i - A
			if tugi < 0:
				tugi = 0

			tmp = tukatta_MP + B
			if tmp < DP[tugi]:
				DP[tugi] = tmp

	#print(DP)
	print(DP[0])



if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from collections import defaultdict
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**10]*(H+1)
dp[0] = 0

for i in range(0,N):
    a = A[i]
    b = B[i]
    for h in range(H+1-a):
        dp[h+a] = min(dp[h+a], dp[h] + b)
    dp[H] = min(dp[H], min(dp[H-a:H])+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)]
AB.sort(key=lambda x: x[1])
dp=[[0 for _ in range(H+1)] for _ in range(N)]
A, B=AB[0]
for j in range(1, H+1):
  dp[0][j]=((j-1)//A+1)*B
for i in range(1, N):
  A, B=AB[i]
  for j in range(H+1):
    if j>=A:
      dp[i][j]=min(dp[i-1][j], dp[i-1][j-A]+B, dp[i][j-A]+B)
    else:
      dp[i][j]=min(dp[i-1][j], B)
print(dp[N-1][H])

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
a=[]
ab=[]

for i in range(n):
    aabb=list(map(int,input().split()))
    ab.append(aabb)
    a.append(aabb[0])

loop=h+max(a)
cost=[-1 for i in range(loop)]
cost[0]=0
i=0
costlist=[]
while i<loop:
    costlist=[]
    for j in range(n):
        ii=i-ab[j][0]
        if (0<=ii<h)&(cost[ii]>=0):
            costlist.append(cost[ii]+ab[j][1])
    if len(costlist)!=0:
        cost[i]=min(costlist)
    i+=1
acost=[i for i in cost[h:] if i!=-1]
ans=min(acost)
print(ans)

Submission status: Accepted

Solution code:
input = raw_input
range = xrange
h, n = map(int, input().split(" "))

pairs = []
ratios = []
a_list = []
inputs = []
for i in range(n):
    a, b = map(int, input().split(" "))
    a_list.append(a)
    #ratio = a/b
    #ratios.append(ratio)
    #pairs.append([a, b, ratio])
    inputs.append([a, b])

#pairs = sorted(pairs, key=lambda x: -x[2])
#print(pairs)

max_a = max(a_list)

min_val = -1-max_a
costs = {}
for i in range(min_val, h+1):
    costs[i] = None
costs[h] = 0
#costs = [None for i in range(h)] + [0]

for val in range(h, min_val-1, -1):
    for a, b in inputs:
        if val+a > h:
            continue
        if costs[val+a] is None:
            continue

        if costs[val] is not None:
            costs[val] = min(costs[val+a]+b, costs[val])
        else:
            costs[val] = costs[val+a]+b

out = min([costs[val] for val in range(min_val, 1) if costs[val] is not None])
#print(costs)
print(out)

Submission status: Accepted

Solution code:
h,n=map(int,input().split())
M=[list(map(int,input().split())) for _ in range(n)]
res=[0]
f=0
for j in range(1,h+1):
    
    i=0
    if 0>=j-M[i][0]:
        ind=0
    else:
        ind=j-M[i][0]
    ff=res[ind]+M[i][1]
    
    for i in range(1,n):
        if 0>=j-M[i][0]:
            ind=0
        else:
            ind=j-M[i][0]
        kari=res[ind]+M[i][1]
        
        ff=min(ff,kari)
    res.append(ff)
print(res[-1])

Submission status: Accepted

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

h_map = [-1 for _ in range(h+1)]
h_map[h] = 0

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

for i in range(1, h+1):
  h_i = h+1 - i
  
  if h_map[h_i] == -1:
    continue
   
  for a, b in ab:
    next_h = h_i - a
    if next_h <= 0:
      if h_map[0] == -1:
        h_map[0] = h_map[h_i] + b
      else:
        h_map[0] = min(h_map[0], h_map[h_i] + b)
        
    else:
      if h_map[next_h] == -1:
        h_map[next_h] = h_map[h_i] + b
      else:
        h_map[next_h] = min(h_map[next_h], h_map[h_i] + b)
        
print(h_map[0])