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:
import numpy as np
H, N =[int(i) for  i in input().split()]

A=[]
B=[]
for i in range(N):
    tmp = [int(i) for  i in input().split()]
    A+=[tmp[0]]
    B+=[tmp[1]]


val=np.zeros((N+100, H+100))
def dpf(i, n):
    if i==0 and n==0:
        return 0
    elif i<0:
        return 1e10
    elif n<0:
        return 0
    elif n==0:
        return 0
    else:
        if val[i, n]>0:
            return val[i, n]
        val[i, n]=min([dpf(i-1, n-A[i-1])+B[i-1], dpf(i-1, n), dpf(i, n-A[i-1]) + B[i-1]])
        return int(val[i,n])
    
A=np.array(A)
B=np.array(B)
res=H
count=0
print(dpf(N,H))


# while res >0:
#     ind=np.argmin(np.ceil(res/A)*B)
#     minval=1e12
#     try:
#         minval=np.min(B[A>=res])
#     except ValueError:
#         minval=1e12
#     #print(minval, res, count)
#     if mt.ceil(res/A[ind])*B[ind] >= minval:
#         res = 0
#         count += minval
#         #print(count, count - minval)
#     else:
#         res = res - A[ind]
#         count += B[ind]
# print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
H, N = map(int, input().split())
X=[]
for _ in range(N):
    a, b = map(int, input().split())
    c=a/b
    X.append([a,b,c])

X.sort(key=lambda x:x[2],reverse=True)

flag=True
power=0
num=X[0][0]
count=0
while flag:

    if H-X[count][0]<0:
        X.pop(count)
        count+=1

    H-=(num)
    power+=X[count][1]
    if H<=0:
        flag=False

print(power)

Submission status: Runtime Error

Solution code:
import numpy as np
H, N = map(int, input().split())
dp = np.zeros((N, H+1))
skills = []
for _ in range(N):
  a, b = map(int, input().split())
  skills.append([a,b])

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

print(int(min(dp.T[-1])))

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np
import math


def main(inp):
    h, n, a, b = io(inp)
    hp = h
    damage_list = list(a)
    cost_list = list(b)

    ret_list = []
    cost = 0
    while len(damage_list) > 0:
        hp, temp_cost, id = moderate_attack(hp, damage_list, cost_list)
        cost += temp_cost
        ret_list.append(cost + cost_list[id])
        del cost_list[id]
        del damage_list[id]
    print(ret_list)
    return min(ret_list)


def moderate_attack(hp, damages, costs):
    cost_performance = [i / j for i, j in zip(damages, costs)]
    best_id = np.argmax(cost_performance)

    num_attack = math.floor(hp / damages[best_id])
    remain_hp = hp - num_attack * damages[best_id]
    consumption_magic = num_attack * costs[best_id]
    return remain_hp, consumption_magic, best_id


def io(inp):
    temp = inp.rstrip().split(sep='\n')
    h, n = [int(i) for i in temp[0].split(sep=' ')]
    ab = np.array([[int(j) for j in i.split(sep=' ')] for i in temp[1:]])

    a = ab[:, 0]
    b = ab[:, 1]
    return h, n, a, b


if __name__ == '__main__':
    input_lines = stdin.readlines()
    ret = main(input_lines)
    print(ret)

Submission status: Runtime Error

Solution code:
import numpy as np

s = input().split()
H = int(s[0])
N = int(s[1])

m = np.array([list(map(int, input().split())) for i in range(N)])
A = m[:, 0]
B = m[:, 1]

def func(H, m):
    A = m[:, 0]
    B = m[:, 1]

    if H < A[i]:
        i = np.argmin((H // A) * B)
        return (H // A[i]) * B[i]
    else:
        i = np.argmax(A / B)
        H_next = H - (H // A[i]) * B[i] 
        return (H // A[i]) * B[i] + func(H_next)
        
        func(H_next, )
        j = np.argmin(B)
        print((H // A[i])*B[i] + B[j])

print(func(H, m))

Submission status: Runtime Error

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

x = []
y = []

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

def knapsack(N,W,weight,value):
  inf=float("inf")
  dp=[[inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=inf

  for i in range(N):
    for w in range(W+1):
        m = dp[i][w]
        p = value[i]
        q = weight[i]
        while w >= q:
            if w - q >= 0:
                m=min(dp[i][w-q]+p,m)
            p += value[i]
            q += weight[i]
        m=min(dp[i][w-q]+p,m,p)
        dp[i+1][w] = m
  return dp[N][W]

k = knapsack(n,h,x,y)
print(k)

Submission status: Runtime Error

Solution code:
resd = {}
def func(h, al, bl, n):
    tmpl = []
    if h <= 0:
        return 0
    for i in range(n):
        if h-al[i] in resd:
            tmpl.append(resd[h-al[i]] + bl[i])
        else:
            tres = func(h-al[i], al, bl, n) + bl[i]
            tmpl.append(tres)
            resd[h] = tres
    return min(tmpl)
    
H, N = map(int, input().strip().split())
Alist = []
Blist = []
for _ in range(N):
    a, b = map(int, input().strip().split())
    Alist.append(a)
    Blist.append(b)
#print(H, N)
#print(Alist, Blist)
print(func(H, Alist,Blist,N))

Submission status: Runtime Error

Solution code:
from sys import stdin
H,N = [int(x) for x in stdin.readline().rstrip().split()]
data = []
ans = 10**9
for i in range(N):
    A,B = [int(x) for x in stdin.readline().rstrip().split()]
    C = A / B
    if A >= H:
        ans = min(ans,B)
    data.append((C,A,B))


dp = [10**9]*(H+1)

c,a,b = sorted(data)[::-1][0]

cnt = 0
for i in range(1,H+1):
    cnt += 1
    if cnt*a >= H:
        dp[-1] = (b*cnt)
        break
    else:
        dp[cnt*a] = (b*cnt)
        

tmp = 10**9
for j in range(H,-1,-1):
    tmp = min(dp[j],tmp)
    dp[j] = tmp

for c,a,b in data:
    dp[-1] = min(dp[-a] + b ,dp[-1])

print(min(ans,dp[-1]))

Submission status: Runtime Error

Solution code:
import numpy as np
H, N =[int(i) for  i in input().split()]

A=[]
B=[]
for i in range(N):
    tmp = [int(i) for  i in input().split()]
    A+=[tmp[0]]
    B+=[tmp[1]]


val=np.zeros((N+100, H+100))
def dpf(i, n):##i種類の技でn以上ダメージ与えるときの最小コスト
    if i==0 and n==0:
        return 0
    elif i<0:
        return 1e10
    elif n<0:
        return 0
    elif n==0:
        return 0
    else:
        if val[i, n]>0:
            return val[i, n]
        val[i, n]=min([dpf(i-1, n-A[i-1])+B[i-1], dpf(i-1, n), dpf(i, n-A[i-1]) + B[i-1]])
        return int(val[i,n])
    
A=np.array(A)
B=np.array(B)
res=H
count=0
print(dpf(N,H))


# while res >0:
#     ind=np.argmin(np.ceil(res/A)*B)
#     minval=1e12
#     try:
#         minval=np.min(B[A>=res])
#     except ValueError:
#         minval=1e12
#     #print(minval, res, count)
#     if mt.ceil(res/A[ind])*B[ind] >= minval:
#         res = 0
#         count += minval
#         #print(count, count - minval)
#     else:
#         res = res - A[ind]
#         count += B[ind]
# print(count)

Submission status: Runtime Error

Solution code:

h,n = list(map(int,input().split()))
toki = []
toki2 = []
count = 0
result = 0

for i in range(n):
    a,b = list(map(int,input().split()))
    toki.append([b-a,a,b])
    toki2.append([b,a])

toki.sort()
toki2.sort()

while True:
    if h >= toki[0][1]:
        h = h - toki[0][1]
        result += toki[0][2]
    elif h == 0:
        break
    else:
        if h <= toki2[0][1]:
           result += toki2[0][0] 
           break
        while count <= n:
            if h >= toki[count][1]:
               h = h - toki[count][1]
               result += toki[count][2]
            else:
                count += 1
        break

print(result)

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
import math
import fractions
from heapq import heappush, heappop


sys.setrecursionlimit(10 ** 7)

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

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

MAGIC = sorted(MAGIC, key=lambda x: x[0]//x[1])
dp = [[10**9] * (H+1) for _ in range(N+1)]

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


for i in range(N):
    for h in range(H+1):
        if h < MAGIC[i][0]:
            dp[i+1][h] = dp[i][h]
        elif h == H:
            m = min(dp[i+1][h-MAGIC[i][0]:])
            #print("num", h-MAGIC[i][0], dp[i][h-MAGIC[i][0]:])
            # print(m)
            dp[i+1][h] = min(dp[i][h], m+MAGIC[i][1])
        else:
            #print("ok", h)
            dp[i+1][h] = min(dp[i][h], dp[i+1][h-MAGIC[i][0]]+MAGIC[i][1])

    # print(dp[i+1])

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

Submission status: Runtime Error

Solution code:
import math
As = []
Ms = []
def culc(k):
  min_m = 100000
  if k == 0:
    return 0
  p = 0
  for i in range(N):
    if Ms[i] >= Ms[t]:
      continue
    elif As[i] < k:
      if min_m > Ms[i]/As[i]:
        min_m = Ms[i]/As[i]
        p = i
        continue
    else:
      t = i
    if Ms[t] > math.floor(k/As[p])*Ms[p]+culc(k%As[p]):
      return math.floor(k/As[p])*Ms[p]+culc(k%As[p])
    else:
      return Ms[t]
line = input().split(' ')
H = int(line[0])
N = int(line[1])
min_m = 100000
t = 0
mins = 100000
for i in range(N):
  line = input().split(' ')
  As.append(int(line[0]))
  Ms.append(int(line[1]))
  if min_m > Ms[i]/As[i]:
    min_m = Ms[i]/As[i]
    t = i
for i in range(t,N):
  if min_m == Ms[i]/As[i] and Ms[i] < Ms[t]:
      t = i
ans = math.floor(H/As[t])*Ms[t]
k = H%As[t]
if k == 0:
  print(ans)
else:
  ans += culc(k)
  print(ans)

Submission status: Runtime Error

Solution code:
h,n=map(int,input().split())
lst=[]
m=100000
for i in range(n):
  a,b=map(int,input().split())
  if m>b:
    m=b
  lst.append([a,b,a/b])
lst=sorted(lst,key=lambda x:(x[2],x[0]))
Dam=lst[0][0]
Mag=lst[0][1]
Cos=lst[0][2]
if h%Dam==0:
  print(Mag*(h//Dam))
else:
  cnt=Mag*(h//Dam)
  H=h-Dam*(h//Dam)
    for i in range(1000000):
      if H==1:
        print(cnt+m)
        break
      if lst[i][1]<H+1:
        mag=lst[i][1]
        dam=lst[i][0]
        cos=lst[i][2]
        if ((H-Dam)/Cos)>mag:
          Dam=dam
          Cos=cos
          Mag=mag
          if H%Dam==0:
            print(cnt+Mag*(H//Dam))
            break
          else:
            cnt+=Mag*(H//Dam)
            H=H-Dam*(H//Dam)
        else:
          if H%Dam==0:
            print(cnt+Mag*(H//Dam)) 
            break
          else:
            print(cnt+Mag*(H//Dam)+Mag)
            break

Submission status: Runtime Error

Solution code:
h,n = list(map(int, input().split()))
damage = []
cost = []
for _ in range(n):
  tw, tv = list(map(int, input().split()))
  damage.append(tw)
  cost.append(tv)

dp=[[h for i in range(h+1)] for j in range(n+1)]
for i in range(n):
    for j in range(h+1):
      if(dp[i][j-damage[i]]-cost[i] >=0):
        dp[i+1][j]=min(dp[i][j-damage[i]]-cost[i],dp[i][j])
print(dp[n-1][h-1])

Submission status: Runtime Error

Solution code:
import numpy as np
def resolve():
    import sys
    input = sys.stdin.readline
    
    H, N = map(int, input().split())
    v = []
    w = []
    for i in range(N):
        a, b = map(int, input().split())
        v.append(a)
        w.append(b)

    min_v_idx = np.array(v).argmin()
    W = (H // v[min_v_idx] + 1) * w[min_v_idx]
    dp = [[0]*W for _ in range(N+1)]

    for i in range(N):
        for j in range(W):
            if j < w[i]:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i])

    INF = 10e10
    res = np.array(dp)
    res = np.where(res < H, INF, res)
    mins = np.where(res == res.min())
    print(min(mins[1]))



if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
import copy

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

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

for i in magic:
    i.append(i[0]/i[1])
  
magic.sort(key=lambda x: -x[2])

copy_magic=copy.deepcopy(magic)
for i in range(len(magic)):
    for j in range(i):
        if (copy_magic[i][0]%copy_magic[j][0]==0 
                and copy_magic[i][2] <= copy_magic[j][2]):
            magic.pop(i)

use_list=[0]*len(magic)
use_list[0]=((h-1)//magic[0][0]+1)
min_magic=use_list[0]*magic[0][1]
tmp_hp=h-use_list[0]*magic[0][0]
tmp_i=0
len_magic=len(magic)
use_magic=min_magic 
while use_list[0]>-1:
    while tmp_i>=0:
        if use_list[tmp_i]>0:
            use_list[tmp_i]-=1
            break
        else:
            tmp_i-=1
    if tmp_i<0:
        break
    tmp_hp+=magic[tmp_i][0]
    use_magic-=magic[tmp_i][1]
    if tmp_i < len_magic-1:
        tmp_i+=1
        use_list[tmp_i]=((tmp_hp-1)//magic[tmp_i][0]+1)
        tmp_hp-=use_list[tmp_i]*magic[tmp_i][0]
        tmp_magic=use_list[tmp_i]*magic[tmp_i][1]
        use_magic+=tmp_magic
        if min_magic > use_magic:
            min_magic=use_magic
    else:
        tmp_hp+=use_list[tmp_i]*magic[tmp_i][0]
        tmp_magic=use_list[tmp_i]*magic[tmp_i][1]
        use_magic-=tmp_magic
        use_list[tmp_i]=0      

print(min_magic)

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools as it
import math as mt
H, N =[int(i) for  i in input().split()]

A=[]
B=[]
for i in range(N):
    tmp = [int(i) for  i in input().split()]
    A+=[tmp[0]]
    B+=[tmp[1]]


val=np.zeros((N+100, H+100))
def dpf(i, n):##i種類の技でn以上ダメージ与えるときの最小コスト
    if i==0 and n==0:
        return 0
    elif i<0:
        return 1e10
    elif n<0:
        return 0
    elif n==0:
        return 0
    else:
        if val[i, n]>0:
            return val[i, n]
        val[i, n]=min([dpf(i-1, n-A[i-1])+B[i-1], dpf(i-1, n), dpf(i, n-A[i-1]) + B[i-1]])
        return int(val[i,n])
    
A=np.array(A)
B=np.array(B)
res=H
count=0
print(dpf(N,H))


# while res >0:
#     ind=np.argmin(np.ceil(res/A)*B)
#     minval=1e12
#     try:
#         minval=np.min(B[A>=res])
#     except ValueError:
#         minval=1e12
#     #print(minval, res, count)
#     if mt.ceil(res/A[ind])*B[ind] >= minval:
#         res = 0
#         count += minval
#         #print(count, count - minval)
#     else:
#         res = res - A[ind]
#         count += B[ind]
# print(count)

Submission status: Runtime Error

Solution code:
from operator import itemgetter as it
from math import ceil, inf

h, n = map(int, input().split(' '))
a_l = [list(map(int, input().split(' '))) for _ in range(n)]
a_s = sorted(a_l,key=it(0))
x = a_s[-1][0] + h
b = [inf] * x
b[0] = 0
for i in range(h):
    for j,k in a_s:
        if b[i+j] > b[i] + k:
            b[i+j] = b[i] + k
print(min(b[h:]))

Submission status: Runtime Error

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]
      
for i in range(H, 20000):
  if nums[i]>0:
    anss.append(nums[i])
    
print(min(nums))

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools as it
import math as mt
H, N =[int(i) for  i in input().split()]

A=[]
B=[]
for i in range(N):
    tmp = [int(i) for  i in input().split()]
    A+=[tmp[0]]
    B+=[tmp[1]]


val=np.zeros((N+1, H+1))
def dpf(i, n):##i種類の技でn以上ダメージ与えるときの最小コスト
    if i==0 and n==0:
        return 0
    elif i<0:
        return 1e10
    elif n<0:
        return 0
    elif n==0:
        return 0
    else:
        if val[i, n]>0:
            return val[i, n]

        val[i, n]=min([dpf(i-1, n-A[i-1])+B[i-1], dpf(i-1, n), dpf(i, n-A[i-1]) + B[i-1]])
        return int(val[i,n])
    
A=np.array(A)
B=np.array(B)
res=H
count=0
print(dpf(N,H))


# while res >0:
#     ind=np.argmin(np.ceil(res/A)*B)
#     minval=1e12
#     try:
#         minval=np.min(B[A>=res])
#     except ValueError:
#         minval=1e12
#     #print(minval, res, count)
#     if mt.ceil(res/A[ind])*B[ind] >= minval:
#         res = 0
#         count += minval
#         #print(count, count - minval)
#     else:
#         res = res - A[ind]
#         count += B[ind]
# print(count)

Submission status: Runtime Error

Solution code:
h,n=map(int,input().split())
lst=[]
m=100000
for i in range(n):
  a,b=map(int,input().split())
  if m>b:
    m=b
  lst.append([a,b,a/b])
lst.sort(key=lambda x:(x[2],x[0]))
Dam=lst[0][0]
Mag=lst[0][1]
Cos=lst[0][2]
if h%Dam==0:
  print(Mag*(h//Dam))
else:
  cnt=Mag*(h//Dam)
  H=h-Dam*(h//Dam)
    for i in range(1000000):
      if H==1:
        print(cnt+m)
        break
      if lst[i][1]<H+1:
        mag=lst[i][1]
        dam=lst[i][0]
        cos=lst[i][2]
        if ((H-Dam)/Cos)>mag:
          Dam=dam
          Cos=cos
          Mag=mag
          if H%Dam==0:
            print(cnt+Mag*(H//Dam))
            break
          else:
            cnt+=Mag*(H//Dam)
            H=H-Dam*(H//Dam)
        else:
          if H%Dam==0:
            print(cnt+Mag*(H//Dam)) 
            break
          else:
            print(cnt+Mag*(H//Dam)+Mag)
            break

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
damage = []
mp = []

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

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

for j in range(1, H + 1):
    if j % damage[0] == 0:
        x = j // damage[0]
    else:
        x = j // damage[0] + 1 
    dp[1][j] = x * mp[0]

for i in range(1, N):
    for j in range(H + 1):
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - damage[i]] + mp[i])

for i, row in enumerate(dp):
    if i == 0:
        continue
    buf = float("inf")
    for j in range(H + 1):
        if row[H - j] > buf:
            row[H - j] = buf
        buf = row[H - j]

print(dp[N][H])

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
A= [(list(map(int, input().split()))) for _ in range(N)]
total_point = 0
 
while H > 0:
	B= [a[0]/a[1] for a in A]
	index = B.index(max(B))
 
	num = H//A[index][0]
	H = H%A[index][0]
 	used_point = A[index][1]
	damage = A[index][0]
	A = [a for a in A if a[0]<damage and a[1]<used_point]
	if len(A) == 0:
		num += 1
		H = 0
	total_point += num*used_point
print(total_point)

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
X = [0] * N
Y = [0] * N
for i in range(N):
    X[i], Y[i] = map(int, input().split())
ZZZ = zip(X,Y)
ZZZ.sort(reverse = False)
X,Y =zip(*ZZZ) 
HP = H
MP = 0
i = 0
while HP => 0;
    HP = HP - X[i]
    MP = MP + Y[i]
print(MP)

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
A= [(list(map(int, input().split()))) for _ in range(N)]
total_point = 0
 
while H > 0:
	# B= [a[0]/a[1] for a in A]
	index = 0 #B.index(max(B))
 
	num = H//A[index][0]
	H = H%A[index][0]
 	used_point = A[index][1]
    damage = A[index][0]
	A = [a for a in A if a[0]<damage and a[1]<used_point]
	if len(A) == 0:
		num += 1
		H = 0
	total_point += num*used_point
print(total_point)

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
A= [(list(map(int, input().split()))) for _ in range(N)]
total_point = 0
 
while H > 0:
	B= [a[0]/a[1] for a in A]
	index = B.index(max(B))
 
	num = 0# H//A[index][0]
	H = 0# H%A[index][0]
 	used_point = A[index][1]
    damage = A[index][0]
	A = [a for a in A if a[0]<damage and a[1]<used_point]
	if len(A) == 0:
		num += 1
		H = 0
	total_point += num*used_point
print(total_point)

Submission status: Runtime Error

Solution code:
import math

hp,n= map(int,input().split())
rc = [list(map(int,input().split())) for i in range(m)]

rchp=[]
rcmp=[]
li=[]

for i in rc:
    rchp.append(i[0])
    rcmp.append(i[1])
    li.append(i[0]/i[1])

idx = li.index(max(li))

ans1 = hp // rchp[idx]
nokori = hp %  rcmp[idx]

li2=[]

if nokori == 0:
    print(ans1 * rcmp[idx])
else:
    for j in rc:
        if nokori <= j[0]:
            li2.append(j[1])
        else:
            li2.append(math.ceil(nokori/j[0]) * j[1])
    print(ans1 * rcmp[idx] + max(li2))

Submission status: Runtime Error

Solution code:
H,N = map(int,input().split(" "))
lis = []
lis_append = lis.append
dp = [2**50 for _ in range(H+1)]
ans = 2**60
for i in range(N):
    a,b = map(int,input().split(" "))
    dp[a] = b
    if a >= H:
        ans = min(ans,b)
    lis_append([a,b])


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

ans = min(ans,dp[H])
print(ans)

Submission status: Runtime Error

Solution code:
h,n=map(int,input().split())
lst=[]
m=100000
for i in range(n):
  a,b=map(int,input().split())
  if m>b:
    m=b
  lst.append([a,b,a//b])
lst.sort(key=lambda x:(x[2],x[0]))
Dam=lst[0][0]
Mag=lst[0][1]
Cos=lst[0][2]
if h%Dam==0:
  print(Mag*(h//Dam))
else:
  cnt=Mag*(h//Dam)
  H=h-Dam*(h//Dam)
    for i in range(n):
      if H==1:
        print(cnt+m)
        break
      if lst[i][1]<H+1:
        mag=lst[i][1]
        dam=lst[i][0]
        cos=lst[i][2]
        if ((H-Dam)/Cos)>mag:
          Dam=dam
          Cos=cos
          Mag=mag
          if H%Dam==0:
            print(cnt+Mag*(H//Dam))
            break
          else:
            cnt+=Mag*(H//Dam)
            H=H-Dam*(H//Dam)
        else:
          if H%Dam==0:
            print(cnt+Mag*(H//Dam)) 
            break
          else:
            print(cnt+Mag*(H//Dam)+Mag)
            break

Submission status: Runtime Error

Solution code:
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int h = sc.nextInt();
    int n = sc.nextInt(); 
    
    List<Integer> a = new ArrayList<>();
    List<Integer> b = new ArrayList<>();
    
    for(int i = 0; i < n; i++){
      int tmp1 = sc.nextInt();
      int tmp2 = sc.nextInt();
      a.add(tmp1);
      b.add(tmp2);
    }
    
    long[][] dp = new long[n+1][h+1];
    
    for(int i = 0; i < dp.length; i++){
      for(int j = 0; j < dp[i].length; j++){
        dp[i][j] = Long.MAX_VALUE;
      }
    }
    
    dp[0][0] = 0;
    
    for(int i = 0; i < n; i++){
      for(int j = 0; j <= h; j++){
        if(dp[i][j] != Long.MAX_VALUE){
          int index = Math.min(h,j+a.get(i));
          
          dp[i][index] = Math.min(dp[i][index], dp[i][j] + b.get(i));
          dp[i+1][j] = Math.min(dp[i][j], dp[i+1][j]);
          dp[i+1][index] = Math.min(dp[i+1][index], dp[i][j] + b.get(i));
        }
        
        if(j < a.get(i)){
          dp[i+1][j] = dp[i][j];
        }else{
          if(dp[i+1][j-a.get(i)] == Long.MAX_VALUE){
            dp[i+1][j] = Math.min(dp[i][j], Long.MAX_VALUE);
          }else{
            dp[i+1][j] = Math.min(dp[i][j], dp[i+1][j-a.get(i)] + b.get(i));
          }
        }
      }
    }
    

    System.out.println(dp[n][h]);
  }
}

Submission status: Runtime Error

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

S = []
for _ in range(n):
    D = list(map(int, input().split()))
    S.append(D)

dic={}

def rec(magic, nokori,sum_mp):
    if nokori<=0:
        return sum_mp
    if magic >= n:
        return 10**9

    if str(magic)+"_"+str(nokori) in dic:
        return dic[str(magic)+"_"+str(nokori)]

    dmg = S[magic][0]
    mp  = S[magic][1]

    min_mp = min(
        rec(magic  ,nokori-dmg,sum_mp+mp), # use this magic, and check this magic again
        rec(magic+1,nokori-dmg,sum_mp+mp), # use this magic, and check next magic
        rec(magic+1,nokori,sum_mp       ), # don't use this magic, and check next magic
    )

    dic[str(magic)+"_"+str(nokori)] = min_mp
    return min_mp

print(rec(0,h,0))

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
A= [(list(map(int, input().split()))) for _ in range(N)]
total_point = 0
 
while H > 0:
	B= [a[0]/a[1] for a in A]
	index = B.index(max(B))
 
	num = H//A[index][0]
	H = H%A[index][0]
 	used_point = A[index][1]
    damage = A[index][0]

print(total_point)

Submission status: Runtime Error

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

S = []
for _ in range(n):
    D = list(map(int, input().split()))
    S.append(D)

dic={}

def rec(magic, nokori,sum_mp):
    if nokori<=0:
        return sum_mp
    if magic >= n:
        return 10**9

    if str(magic)+"_"+str(nokori) in dic:
        return dic[str(magic)+"_"+str(nokori)]

    dmg = S[magic][0]
    mp  = S[magic][1]

    min_mp = min(
        rec(magic  ,nokori-dmg,sum_mp+mp), # use this magic, and check this magic again
        rec(magic+1,nokori-dmg,sum_mp+mp), # use this magic, and check next magic                    rec(magic+1,nokori,sum_mp       ), # don't use this magic, and check next magic
    )

    dic[str(magic)+"_"+str(nokori)] = min_mp
    return min_mp

print(rec(0,h,0))

Submission status: Runtime Error

Solution code:
H,b= map(int, input().split())
m = [list(map(int, input().split())) for _ in range(b)]
def kara(t,i):
    if t<=0:
        return m[i][1]
    else:
        return m[i][1] + min([kara(t-m[i][0],i) for i in range(b)])

print(kara(H,0)-m[0][1])

Submission status: Runtime Error

Solution code:
import numpy as np
H,N=map(int,input().split())
X=np.array([0,0])
Q=np.array([X for i in range(N)])
maxA=0
maxC=0
for i in range(N):
	A,B=map(int,input().split())
	C=A/B
	Q[i]=np.array([A,B])

Qmax=np.argmax(Q,axis=1)
Bmax=min(Qmax[1],H)
	
P=np.array([1+H*N*100001 for i in range(H+1)])
P[0]=0
for k in range(1,Bmax+1):
	for i in range(N):
		P[k]=min(P[max(0,k-Q[i][0])]+Q[i][1],P[k])
for k in range(Bmax+1,H+1):
	for i in range(N):
		P[k]=min(P[k-Q[i][0]]+Q[i][1],P[k])
print(P[H])

Submission status: Runtime Error

Solution code:
import math

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

A = []
B = []

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

def attack(hp, usedCost = 0):
    if hp <= 0:
        return usedCost
    
    mCost = 10000
    mDamage = 0
    for i in range(N):
        attackNum = math.floor(hp / A[i]) if hp >= A[i] else 1
        damage = A[i] * attackNum
        cost = B[i] * attackNum
        if cost < mCost:
            mCost = cost
            mDamage = damage
    
    return attack(hp - mDamage, usedCost + mCost)
    
print(attack(H))

Submission status: Runtime Error

Solution code:
i=list(map(int,input().split()))
h=i[0]
mp=0
mls=[[0,0]]*i[1]
pls=[[0,0]]*i[1]
for k in range(i[1]):
  mls[k]=list(map(int,input().split()))
  pls[k]=[float(mls[k][0])/mls[k][1],k]
pls.sort(reverse=True)
fp=mls[pls[0][1]][0]
fm=mls[pls[0][1]][1]
kaz=h//fp
h=h-(fp*(kaz))
mp=fm*kaz
v=0
for v in i[1]:
  sp=mls[v][0]
  sm=mls[v][1]
  if sp>=h and sm<fm:
    fm=sm
mp+=fm
print(mp)

Submission status: Runtime Error

Solution code:
import sys
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)
d = {}

sys.setrecursionlimit(h * n)

def toki(h, n):
    if (h, n) in d.keys():
        return d[(h, n)]
    else:
        if h <= 0:
            d[(h, n)] = 0
            return d[(h, n)]
        elif n == 1:
            d[(h, n)] = toki(h - A[0], 1) + B[0]
            return d[(h, n)]
        else:
            d[(h, n)] = min(toki(h, n - 1), toki(h - A[n-1], n) + B[n-1])
            return d[(h, n)]

print(toki(h, n))

Submission status: Runtime Error

Solution code:
H, N = map(int, input().split())
A= [(list(map(int, input().split()))) for _ in range(N)]
total_point = 0
 
while H > 0:
	B= [a[0]/a[1] for a in A]
	index = B.index(max(B))
 
	num = H//A[index][0]
	H = H%A[index][0]
 	used_point = A[index][1]
    damage = A[index][0]
	
print(total_point)

Submission status: Runtime Error

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

A=[]
B=[]
DP=[10000000000]*(H+1)
DP[0]=0
for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    DP[a]=b



minA=A[A.index(min(A))]
minB=B[B.index(min(B))]

for i in range(1,A.index(min(A))):
    DP[i]=minB

for i in range(A.index(min(A)),H+1):
    temp=[]
    for j in range(i+1):
        temp.append(DP[j]+DP[i-j])
    DP[i]=min(temp)

print(DP[H])

Submission status: Runtime Error

Solution code:
import numpy as np
H,N=map(int,input().split())
X=np.array([0,0])
Q=np.array([X for i in range(N)])
maxA=0
maxC=0
for i in range(N):
	A,B=map(int,input().split())
	C=A/B
	Q[i]=np.array([A,B])

Qmax=np.argmax(Q,axis=1)
Bmax=min(Qmax[1],H)
	
P=np.array([1+H*N*100001 for i in range(H+1)])
P[0]=0
for k in range(1,Bmax+1):
	for i in range(N):
		P[k]=min(P[max(0,k-Q[i][0])]+Q[i][1],P[k])
for k in range(Bmax+1,H+1):
	for i in range(N):
		P[k]=min(P[max(0,k-Q[i][0])]+Q[i][1],P[k])
print(P[H])
#print(P)

Submission status: Runtime Error

Solution code:
import numpy as np
hp, n = map(int,input().split())

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

dp = np.full((n+1, hp+1), 100000000000000000)

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 = int(j - list[i-1][0])
            dp[i][j] = min((dp[i][target] + list[i-1][1]), dp[i-1][j])

print(dp[n][hp])

Submission status: Runtime Error

Solution code:
H,N = map(int,input().split(" "))
lis = []
lis_append = lis.append
dp = [10**8+1 for _ in range(H+1)]
for i in range(N):
    a,b = map(int,input().split(" "))
    dp[a] = b
    lis_append([a,b])


for i in range(H+1):
    for a,b in lis:
        if i + a <= H:
            dp[i+a] = min(dp[i+a],dp[i]+b)
        else:
            dp[H] = min(dp[H],dp[i]+b)
print(dp[H])