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: Runtime Error

Solution code:
H,N =map(int,input().split()) 
dp = [[0] * (H+1) for i in range(N)]
A,B = splitintinput()
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: Runtime Error

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

ATK = []
for i in range(N):
    A, B= map(int, input().split())
    C = A/B
    ATK.append([C,A,B])
    
ATK.sort(reverse=True)
print(ATK)

mp = 0
while(True):
    print(H,ATK[0])
    if H == 0:
        break
    if H >= ATK[0][1]:
        N = H//ATK[0][1]
        H = H%ATK[0][1]
        mp += N*ATK[0][2]
        ATK.pop(0)
    else:
        if len(ATK) > 1:
            ATK.pop(0)
        else:
            N = H//ATK[0][1]
            mp += N*ATK[0][2]+1
            H = 0
print(mp)

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
A = list()
B = list()
eff = list()
for i in range(N):
  a_, b_ = map(int, input().split())
  eff_ = a_ / b_
  A.append(a_)
  B.append(b_)
  eff.append(b_)

best_ind = eff.index(max(eff))
num_of_best_attack = H // A[best_ind]
rest = H % A[best_ind]
if rest == 0:
  num_of_best_attack += 1
else:
  B_sorted = B.sorted()
  i = 0
  while True:  
    if A[B.index(B_sorted[i])] >= rest:
      ret = B_sorted[i]
      break:
    i += 1
    if i == len(B) - 1:
      num_of_best_attack += 1
      ret = 0
      break
ret += num_of_best_attack * B[best_ind]
print(ret)

Submission status: Runtime Error

Solution code:
memo = dict()


def solve(i, A, B, hp):
    key = (i, hp)
    if key in memo:
        return memo[key]

    if hp <= 0:
        return 0
    if i < 0:
        return float("infinity")

    a = A[i]
    b = B[i]
    ans = min(solve(i-1, A, B, hp), solve(i, A, B, hp-a) + b)
    memo[key] = ans
    return ans


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

ans = solve(N-1, A, B, H)
print(ans)

Submission status: Runtime Error

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

def calc(h, b_max, ab):
  if len(ab) == 0:
    return b_max
  a, b = max(ab, key=lambda x: x[0] / x[1])
  if h >= a:
    q, r = divmod(h, a)
    return q * b + calc(r, b_max, ab)
  else:
    dup = [x for x in ab if x[1] < b]
    return min(b, calc(h, b, dup))

print(calc(H, 10**8, AB))

Submission status: Runtime Error

Solution code:

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

H,N = splitintinput()
dp = [[0] * (H+1) for i in range(N)]
A,B = splitintinput()
for j in range(1, H+1):
    dp[0][j] = B * int((j+A-1)/A)

for i in range(1,N):
    A,B = splitintinput()
    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: Runtime Error

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

L=[]
for i in range(N):
    a,b=map(int,input().split())
    L.append([a,b])
L.sort()
import math
X=math.ceil(H/L[i][0]*L[i][1])
dp=[10**20 for i in range(10**4)]
dp[0]=0
for i in range(N):
    for j in range(len(dp)):
        if j%L[i][0]==0:
            Y=j//L[i][0]
        else:
            Y=j//L[i][0]+1
        if j-L[i][0]>=0:
            dp[j]=min(dp[j],Y*L[i][1],dp[j-L[i][0]]+L[i][1])
        else:
            dp[j]=min(dp[j],Y*L[i][1])
print(dp[H])

Submission status: Runtime Error

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, H+1):
            T[i][j] = min(T[i-1][j], T[i][j - AB[i][0]]+AB[i][1])
print(T[-1][-1])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
memo = {}
h, n = map(int, input().split())
ab = [tuple(map(int, input().split())) for _ in range(n)]

def f(n):
    if n <= 0:
        return 0
    elif n in memo:
        return memo[n]
    else:
        fn = min([f(n-a) + b for a, b in ab])
        memo[n] = fn
        return fn

print(f(h))

Submission status: Runtime Error

Solution code:
import math

def intinput():
    return int(input())

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

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

H,N = splitintinput()
dp = [[0] * (H+1) for i in range(N)]
A,B = splitintinput()
for j in range(1, H+1):
    dp[0][j] = B * math.ceil(j/A)

for i in range(1,N):
    A,B = splitintinput()
    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: Runtime Error

Solution code:
import math

def intinput():
    return int(input())

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

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

H,N = splitintinput()
dp = [[0] * (H+1) for i in range(N)]
A,B = splitintinput()
for j in range(1, H+1):
    dp[0][j] = B * math.ceil(j/A)

for i in range(1,N):
    A,B = splitintinput()
    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[ij-A] + B)

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

Submission status: Runtime Error

Solution code:
import numpy as np
import pandas as pd
H, N = input().split(" ")
H, N = int(H), int(N) 
A = np.zeros(N, dtype=np.int64)
B = np.zeros(N, dtype=np.int64)

for i in range(N):
    a, b = input().split(" ")
    A[i], B[i] = int(a), int(b)
sorted_idx = np.argsort(A)[::-1]
efficient_rate = A/B
data = pd.DataFrame({
    "A" : A,
    "B" : B,
    "rate" : efficient_rate  
})
data = data.sort_values(by=["rate", "A"], ascending=False)
efficient_rank = np.argsort(efficient_rate)[::-1]
_idx = 0 
accum = 0
while True:
    a = data.loc[_idx, "A"]
    b = data.loc[_idx, "B"]
    if a < H or _idx == N-1:
        H -= a
        accum += b
    else: 
        _idx += 1 
    if H <= 0:
        print(accum)
        break

Submission status: Runtime Error

Solution code:

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

H,N = splitintinput()
dp = [[0] * (H+1) for i in range(N)]
A,B = splitintinput()
for j in range(1, H+1):
    dp[0][j] = B * math.ceil(j/A)

for i in range(1,N):
    A,B = splitintinput()
    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[ij-A] + B)

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

Submission status: Runtime Error

Solution code:
#E
#入力
H,N=map(int,input().split())
M=[list(map(int,input().split())) for i in range(N)]#[攻撃,魔力]
#処理
As=sorted(M,key=lambda x:x[0])
Bs=sorted(M,key=lambda x:x[1])

def E(h):#体力hの相手に対して最も最小の魔力消費
    if h<=0:
        return 0
    else:
        return min([E(h-M[i][0]) +M[i][1] for i in range(N)])
print(E(H))

Submission status: Runtime Error

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


def main():

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

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

    dp = [0] * 10**4
    for a, b in zip(A, B):
        dp[a] = b
    for i in range(10**4):
        for a, b in zip(A, B):
            # if i + a < 10**4:
            # print(i + a)
            # print(dp[i], dp[i + a], b, i + a)
            if i + a < 10**4 and dp[i] > 0:
                if dp[i + a] > 0:
                    dp[i + a] = min(dp[i + a], dp[i] + b)
                else:
                    dp[i + a] = dp[i] + b
    k = h
    flag = 1
    ans = 0
    # print(dp[:20])
    while (flag):
        # print(k)
        if dp[k] > 0:
            ans = dp[k]
            flag = 0
        k += 1
    print(ans)


main()

Submission status: Runtime Error

Solution code:
''' Hey stalker :) '''
INF = 10**10
def main():
    #print = out.append
    ''' Cook your dish here! '''
    h, n = get_list()
    wepons = dict()
    for _ in range(n):
        a,b = get_list()
        if a in wepons:
            wepons[a] = min(b, wepons[a])
        else: wepons[a] = b

    dp = [-1]*(h+1)
    dp[0] = 0
    #print(wepons)
    def calc(n):
        if n<0: return 0
        if dp[n]!=-1: return dp[n]
        #print(n)
        cst = INF
        for wepon in wepons:
            #print(n-wepon)
            cst = min(cst, calc(n-wepon) + wepons[wepon])
        dp[n] = cst
        #print(n, cst)
        return cst
    print(calc(h))


''' Pythonista fLite 1.1 '''
import sys
#from collections import defaultdict, Counter
#from bisect import bisect_left, bisect_right
#from functools import reduce
#import math
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()
#[main() for _ in range(int(input()))]
print(*out, sep='\n')

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

signed main(){
  ll h,n;cin >> h >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  for(ll i=0;i<n;i++){
    cin >> a[i] >> b[i];
  }
  ll inf=10000000000;
  ll ma=*max_element(a.begin(),a.end());
  vector<ll> dp(h+1+ma,inf);dp[0]=0;
  for(ll i=0;i<n;i++){
    for(ll j=0;j<h+1+ma;j++){
      if(dp[j]!=inf){
        if(j+a[i]<=h+ma){
          dp[j+a[i]]=min(dp[j+a[i]],dp[j]+b[i]);
        }else{
          break;
        }
      }
    }
  }
  cout << *min_element(dp.begin()+h,dp.end()) << endl;
}

Submission status: Runtime Error

Solution code:
from operator import itemgetter, attrgetter

h,n = map(int,input().split())
ls = []

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

ls = sorted(ls,key = lambda x: min(x[0],h)/x[1], reverse = True)
ans = 0

t = ls[0]
ans += h//t
h %= t

while h>0:
  ls = sorted(ls,key = lambda x: min(x[0],h)/x[1], reverse = True)
  t = ls[0]
  ans += h//t
  h %= t
print(ans)

Submission status: Runtime Error

Solution code:
#E
H,N=map(int,input().split())
A,B=[0]*N,[0]*N
for i in range(N):
    A[i],B[i]=map(int,input().split())
    
memo=[0]*(H+10)
def dpinit(H):
    for i in range(H):
        memo[H]=min([dp(H-A[i])+B[i] for i in range(N)])
    
def dp(H):
    if H<=0:
        return 0
    else:
        if memo[H]==0:
            memo[H]=min([dp(H-A[i])+B[i] for i in range(N)])
        return memo[H]
    
dpinit(int(H//100))
print(dp(H))

Submission status: Runtime Error

Solution code:
from functools import lru_cache

H, N = (int(x) for x in input().split())

spells = []
for _ in range(N):
    a, b = (int(x) for x in input().split())

    spells.append((a,b))

@lru_cache(maxsize=None)
def f(h):
    if h <= 0: return 0

    return min(b + f(h-a) for a,b in spells)


print(f(H))

Submission status: Runtime Error

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

att = []

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

min_matrix = [-1] * (H+1)

def min_att(h):
    if h <= 0:
        return 0
    if min_matrix[h] >= 0:
        return min_matrix[h]
    min_cand = -1
    for i in range(N):
        if i == 0:
            min_cand = min_att(h-att[i][0]) + att[i][1]
        else:
            tmp = min_att(h-att[i][0]) + att[i][1]
            if tmp < min_cand:
                min_cand = tmp
    min_matrix[h] = min_cand
    return min_matrix[h]

print(min_att(H))

Submission status: Runtime Error

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

dg = 10**5

ba = []
for _ in [0]*n:
    a,b = map(int,input().split())
    ba.append(b*dg+a)
ba.sort()

d = [0]*(h+1)

a,b = ba[0]%dg,ba[0]//dg
d[0] = 0
for i in range(1,h+1):
    if i%a == 0:
        d[i] = (i//a)*b
    else:
        d[i] = (i//a+1)*b


for i in range(1,n):
    a,b = ba[i]%dg,ba[i]//dg
    if d[a] >= b:
        for j in range(a,h+1):
            if d[j] > d[j-a]+b:
                d[j] = d[j-a]+b

print(d[-1])

Submission status: Runtime Error

Solution code:
from functools import lru_cache

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

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

threshold = sum(a for a, b in AB)
bestA, bestB = max(AB, key=lambda ab: ab[0] / ab[1])

@lru_cache(maxsize=None)
def f(h):
    if h <= 0:
        return 0
    times = (h - threshold) // bestA
    if times > 0:
        return bestB * times + f(h - bestA * times)

    return min(b + f(h - a) for a, b in AB)

print(f(H))

Submission status: Runtime Error

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

inf = 10 ** 10
DP = [[inf] * (H + 1) for _ in range(N)]

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

print(DP[-1][H])

Submission status: Runtime Error

Solution code:
#coding:utf-8

def main():
    h, n = map(int, input().split())
    ab = []
    for _ in range(n):
        ab.append(list(map(int, input().split())))
    
    inf = 1000000
    # indexがモンスターに与えたダメージで、中身がトキのダメージ
    damages = [inf for _ in range(100000)]
    damages[0] = 0
    for i in range(h):
        if damages[i] == inf:
            continue
        for j in range(n):
            new_damage = ab[j][0]
            if damages[i + new_damage] > damages[i] + ab[j][1]:
                damages[i + new_damage] = damages[i] + ab[j][1]
    
    k = h
    while damages[k] == inf:
        k += 1
    print(damages[k])
main()

Submission status: Runtime Error

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

ans = float('inf')
for i in range(N):
    for j in range(H-AB[i][0],H):
        if dp[i][j] != float('inf'):
            ans = min(ans,dp[i][j]+AB[i][1])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
h,n=map(int,input().split())
d=sorted([list(map(int,input().split()))for i in range(n)])
p=np.array([float("INF")]*max(h+1,2*d[-1][0]+1))
p[0]=0
s=max(h+1,2*d[-1][0]+1)
for i in range(n):
    a,b=d[i][0],d[i][1]
    j=1
    while j*a<=s:
        p[j*a:]=np.minimum(p[j*a:],p[:-j*a]+j*b)
        j+=1
print(int(min(p[h:])))

Submission status: Runtime Error

Solution code:
#E
H,N=map(int,input().split())
A,B=[0]*N,[0]*N
for i in range(N):
    A[i],B[i]=map(int,input().split())
    
memo=[0]*(H+10)
def dp(H):
    if H<=0:
        return 0
    else:
        if memo[H]==0:
            memo[H]=min([dp(H-A[i])+B[i] for i in range(N)])
        return memo[H]

print(dp(H))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


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

import math
dp = [math.inf] * (h + 10 ** 4)
dp[0] = 0

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

dprint(dp[:10])
print(min(dp[h:]))

Submission status: Runtime Error

Solution code:

def resolve():
    H,N=(int(i) for i in input().split())
    magics=[[int(i) for i in input().split()] for j in range(N)]
    mx=2**31
    dpt=[mx for i in range(H+1)]
    dpt[-1]=0
    for i in range(H,-1,-1):
        
        current=dpt[i]
        if current> dpt[0]:
            continue
        for m in magics:
            dpt[max(0,i-m[0])]=min(dpt[max(0,i-m[0])],current+m[1])
    print(dpt[0])

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

Submission status: Runtime Error

Solution code:
from functools import lru_cache

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

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

@lru_cache(maxsize=None)
def f(h):
    if h <= 0:
        return 0
    return min(b + f(h - a) for a, b in AB)

print(f(H))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,*w = [int(i) for i in read().split()]
#n,*t = map(int,read().split())

#n = int(input())
#s = input()
h,n = [int(i) for i in readline().split()]

 
def div(m,a,b,dp): #divide by (1+bx^a) up to x^m
    for i in range(m-a+1):
        dp[i+a] = min(dp[i+a], dp[i]+b)
    for i in range(m-a+1,m):
        dp[-1] = min(dp[-1],dp[i]+b)
        #dp[i+c] += dp[i]


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

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    div(h,a,b,dp)
    #print(a,b,dp)
 


print(dp[-1])

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <functional>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, m; cin >> n >> m;
  vector<pii> p(m);
  for(int i=0; i<n; i++) cin >> p[i].first >> p[i].second;

  vector<int> dp(n+1, 1e9);
  dp[0] = 0;
  for(int i=1; i<=n; i++) {
    for(int j=0; j<m; j++) {
      if(i >= p[j].first) dp[i] = min(dp[i], dp[i-p[j].first] + p[j].second);
    }
  }

  cout << dp[n] << endl;

  return 0;
}

Submission status: Runtime Error

Solution code:
import sys

h, n = list(map(int, input().split()))
INF = 10 ** 18
dp = [INF] * (h + 10001)
dp[0] = 0
for line in sys.stdin:
    a, b = map(int, line.split())
    for i in range(10000):
        dp[i + a] = min(dp[i + a], dp[i] + b)
print(min(dp[h:]))