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:
INF = 10**9

def unboundedKnapsack(H, n, damage, mana): 
    # dp[i] stores minimum mana for damage i
    dp = [INF for i in range(H + 1)] 
    for i, d in enumerate(damage):
        dp[d] = mana[i]

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

            else:

                dp[i] = min(dp[i +1 ], dp[i]) 

    # print(dp)
    return dp[H]

def main():
    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)
        # cost[a] = max(cost[a], b)
    # print(cost)
    print(unboundedKnapsack(H, N, A, B))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

INF = 10**18

dp = [INF] * (H + 1)
dp[0] = 0
for a, b in AB:
    for n in range(H + 1):
        k = max(0, n - a)
        acc = dp[k] + b
        if acc < dp[n]:
            dp[n] = acc

print(dp[H])

Submission status: Runtime Error

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



MAX=10**4+1
def solve(r,idx):
  if idx==N:
    return MAX
  if r%A[idx][1]==0:
    return (r//A[idx][1])*A[idx][2]
  else:
    return min((r//A[idx][1]+1)*A[idx][2],(r//A[idx][1])*A[idx][2]+solve(r%A[idx][1],idx+1))

def main():
  A=[]
  B=[]
  for i in range(N):
    a,b=map(int, input().split())
    A.append([a/b,a,b])
  if N==1:
    if H%A[0][1]==0:
    	print((H//A[0][1])*A[0][2])
    else:
    	print((H//A[0][1]+1)*A[0][2])
    return
  A=sorted(A)[::-1]

  if H%A[0][1]==0:
    print((H//A[0][1])*A[0][2])
  else:
    ans=min((H//A[0][1]+1)*A[0][2],(H//A[0][1])*A[0][2]+solve(H%A[0][1],1))
    print(ans)

main()

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

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

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

Submission status: Runtime Error

Solution code:
import math
H,N=map(int,input().split())
A=[]
B=[]
eff=[]
mp=0

for i in range(N):
    a,b=map(int,input().split())
    A.append(a)
    B.append(b)
    eff.append(math.ceil(H/a)*b)

while H>0:
    eff_min_index=eff.index(min(eff))
    tmp=math.floor(H/A[eff_min_index])
    H-=(tmp-1)A[eff_min_index]
    mp+=(tmp-1)B[eff_min_index]
    for i in range(N):
        eff[i]=math.ceil(H/A[i])*B[i]
print(mp)

Submission status: Runtime Error

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



MAX=10**4+1
def solve(r,idx):
  if idx==N:
    return MAX
  if r%A[idx][1]==0:
    return (r//A[idx][1])*A[idx][2]
  else:
    return min((r//A[idx][1]+1)*A[idx][2],(r//A[idx][1])*A[idx][2]+solve(r%A[idx][1],idx+1))


A=[]
B=[]
for i in range(N):
  a,b=map(int, input().split())
  A.append([a/b,a,b])
if N==1:
  if H%A[0][1]==0:
  	print((H//A[0][1])*A[0][2])
  else:
 	print((H//A[0][1]+1)*A[0][2])
  return
A=sorted(A)[::-1]

if H%A[0][1]==0:
  print((H//A[0][1])*A[0][2])
else:
  ans=min((H//A[0][1]+1)*A[0][2],(H//A[0][1])*A[0][2]+solve(H%A[0][1],1))
  print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np
    H, N = map(int, input().split())
    a = np.zeros((N+1))
    b = np.zeros((N+1))
    a[0] = 10**9
    b[0] = 10**9
    dp = np.zeros((N+1,H+1))
    dp[0][:] = 10**9

    for i in range(N):
        a_i, b_i = map(int, input().split())
        a[i+1] = a_i
        b[i+1] = b_i

        dp[i+1][0] = b_i

    for i in range(1,N+1):
        for h in range(1,H+1):
            if h > a[i]:
                dp[i][h] = min(dp[i][h-a[i]]+b[i],dp[i-1][h])
            else:
                dp[i][h] = min(dp[i][0],dp[i-1][h])
    print(int(dp[N][H]))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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



MAX=10**4+1
def solve(r,idx):
  if idx==N:
    return MAX
  if r%A[idx][1]==0:
    return (r//A[idx][1])*A[idx][2]
  else:
    return min((r//A[idx][1]+1)*A[idx][2],(r//A[idx][1])*A[idx][2]+solve(r%A[idx][1],idx+1))

def main():
  A=[]
  B=[]
  for i in range(N):
    a,b=map(int, input().split())
    A.append([a/b,a,b])
  if N==1:
    if H%A[0][1]==0:
    	print((H//A[0][1])*A[0][2])
  	else:
    	print((H//A[0][1]+1)*A[0][2])
    return
  A=sorted(A)[::-1]

  if H%A[0][1]==0:
    print((H//A[0][1])*A[0][2])
  else:
    ans=min((H//A[0][1]+1)*A[0][2],(H//A[0][1])*A[0][2]+solve(H%A[0][1],1))
    print(ans)

main()

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:
import math

class Item:
    def __init__(self,a=0,b=0):
        self.attack=a
        self.damage=b

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

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

def knapsack(i,hp):
    if hp <= 0:
        return 0
    elif i==n-1:
        return math.ceil(hp/items[i].attack)*items[i].damage
    else:
        max = math.ceil(hp/items[i].attack)
        return min(knapsack(i+1, hp-(items[i].attack*j))+items[i].damage*j for j in range(max))

print(knapsack(0,h))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import numpy as np
import bisect

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

ab = []
for _ in range(n):
    ab.append(list(map(int, list(input().split()))))
ab = np.array(ab)
max_a = max(ab[:,0])

dp = [int(1e9)] * int(1e6)
checked = [False] * int(1e6)
dp[0] = 0

sorted_list = [0]
while len(sorted_list) > 0:
    v = sorted_list.pop(0)
    
    if v > h:
        break
    
    for i in range(n):
        if checked[v + ab[i, 0]] == False:
            bisect.insort_right(sorted_list, v + ab[i, 0])
            checked[v + ab[i, 0]] = True
        dp[v + ab[i,0]] = min(dp[v + ab[i,0]], dp[v] + ab[i,1])
    
print(min(dp[v-1:]))

Submission status: Runtime Error

Solution code:
import numpy as np
H, N = map(int, input().split())
a = []
b = []
a.append(10**9)
b.append(10**9)

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

dp = np.zeros((N+1,H+1))

a = np.array(a)
b = np.array(b)

for i in range(N+1):
    dp[i][0] = b[i]
dp[0][:] = 10**9


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

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

Submission status: Runtime Error

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

dp = [1000000] * (10 ** 5)
dp[0] = 0
for i in range(H):
    if dp[i] == 1000000:
        continue
    for j in range(N):
        a, b = AB[k]
        if dp[i] + b < dp[i + a]:
            dp[i + a] = dp[i] + b
print(min(dp[H:]))

Submission status: Runtime Error

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

A=[]
B=[]
for i in range(N):
  a,b=map(int, input().split())
  A.append([a/b,a,b])
A=sorted(A)
a=A[0][1]
b=A[0][2]
B.append([a/b,a,b])
new_N=1
for i in range(1,N):
  if a/b !=A[i][1]/A[i][2]:
    a=A[i][1]
    b=A[i][2]
    B.append([a/b,a,b])
    new_N+=1

A=B[::-1]
MAX=10**4+1
N=new_N
def solve(r,idx):
  if idx==N:
    return MAX
  if r%A[idx][1]==0:
    return (r//A[idx][1])*A[idx][2]
  else:
    return min((r//A[idx][1]+1)*A[idx][2],(r//A[idx][1])*A[idx][2]+solve(r%A[idx][1],idx+1))

if H%A[0][1]==0:
  print((H//A[0][1])*A[0][2])
else:
  ans=min((H//A[0][1]+1)*A[0][2],(H//A[0][1])*A[0][2]+solve(H%A[0][1],1))
  print(ans)

Submission status: Runtime Error

Solution code:
import sys
N,D,A=[int(i) for i in input().split(" ")]
enemies=[]
for i in range(N):
    X,H=[int(j) for j in input().split(" ")]
    enemies.append([X,H])

enemies=sorted(enemies,key=lambda w:w[0])

if enemies[-1][0]-enemies[0][0]<=2*D:
    hpmax=sorted(enemies,key=lambda  w:w[1])[-1][1]
    if hpmax%A==0:
        print(hpmax//A)
    else:
        print(hpmax//A+1)
    sys.exit()


retval=0
for i in range(N):
    hp=enemies[i][1]
    place=enemies[i][0]
    if hp<=0:
        continue
    if hp%A==0:
        bomb=hp//A
    else:
        bomb=hp//A+1
    retval+=bomb

    for j in range(i,N):
        if enemies[j][0]-place>2*D:
            break
        enemies[j][1]-=bomb*A



print(retval)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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)

dp = [float("inf")] * (2 * h + 1)
dp[0] = 0
for i in range(h):
    for j in range(n):
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])

ans = float('inf')
for j in range(h, 2 * h + 1):
    ans = min(ans, dp[j])
print(ans)

Submission status: Runtime Error

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)

dp = [float("inf")] * (3 * h)
dp[0] = 0
for i in range(h):
    for j in range(n):
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])

ans = float('inf')
for j in range(h, 2 * h + 1):
    ans = min(ans, dp[j])
print(ans)

Submission status: Runtime Error

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

inf=float("inf")
dp=[inf]*(H+1)
dp[0]=0
ans=inf
for i in range(N):
    A,B=AB[i]
    for j in range(H-A+1):
        dp[j+A]=min(dp[j+A],dp[j]+B)
    for j in range(H-A+1,H):
        ans=min(ans,dp[j]+B)
ans=min(ans,dp[H])

print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
def main():
    H, N = map(int, input().split())
    A_B = []
    for i in range(N):
        tmp = list(map(int, input().split()))
        tmp.append(tmp[0] / tmp[1])
        A_B.append(tmp)
    A_B.sort(key=lambda x: x[2], reverse=True)

    ans = int(H / A_B[0][0]) * A_B[0][1]
    H = H - (int(H / A_B[0][0]) * A_B[0][0])
    del A_B[0]

    min_cost = float('inf')
    for i in range(len(A_B)):
        cost = 0
        tmp = H
        while tmp > 0:
            tmp = tmp - A_B[i][0]
            cost += A_B[i][1]
        if cost < min_cost:
            min_cost = cost
    ans = ans + cost
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h, n = map(int, input().split())
cnt = 0
a = []
b = []
c = []
d = []
for i in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append((i, ai/bi))
c.sort(reverse=True, key=lambda x:x[1])
ok = (a[c[0][0]], b[c[0][0]])
for i in range(1, h+1):
    mincost = 10**10
    mincostatk = 0
    for j in range(n):
        if a[j] > i:
            if mincost < b[j]:
                mincost = b[j]
                mincostatk = a[j]
    if mincostatk == 0:
        mincost = ok[1]
        mincostatk = ok[0]
    d.append((mincostatk, mincost))
while h > 0:
    h = h - d[h-1][0]
    cnt += d[h-1][1]
print(cnt)

Submission status: Runtime Error

Solution code:
# coding: utf-8
hp,n = map(int, input().split())
l = []
for i in range(n):
    damage,cost = map(int, input().split())
    eff = cost/damage
    l.append([eff,cost,damage])
    
l.sort()
print(l)
total = 0

i = 0
while True:
    eff,cost,damage = l[i]
    
    if hp>damage:
        hit = hp//damage
        hp = hp%damage
        
        total += hit * cost
        if hp == 0:
            print('1-1')
            break
        else:
            eff2,cost2,damage2 = l[i+1]
            if cost <= cost2 * -(-hp//damage2):
                total += cost
                print('1-2')
                break
            else:
                i += 1
                print('1-3')
                continue
    else:
        if cost <= cost2 * -(-hp//damage2):
            total += cost
            print('2-1')
            break
        else:
            i += 1
            print('2-2')
            continue
        

print(total)

Submission status: Runtime Error

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

h, n = map(int, input().split(' '))
a_l = [list(map(int, input().split(' '))) for _ in range(n)]
x = min(a_l) + h
b = [10**20] * x
b[0] = 0
for i in range(h):
    for j,k in a_l:
        if b[i+j] > b[i] + k:
            b[i+j] = b[i] + k
print(min(b[h:]))

Submission status: Runtime Error

Solution code:
import numpy
H,  N = map(int, input().split())
A = [0] * N
B = [0] * N
P = [0] * N
Ar = []
Ar.append(A)
Br = []
Br.append(B)
for i in range(N):
    A[i], B[i] = map(int, input().split())
    P[i] = A[i] / B[i]
m = numpy.argmax(P)
ans = 0

while H // A [m] > 0:
  ans += B[m] * (H // A[m])
  H = H % A[m]
  del A[m], B[m], P[m]
  m = numpy.argmax(P)


if H > 0:
  mi = -(-H // Ar[0]) * Br[0]
  for i in range(len(Ar)):
    k = -(-H // Ar[i]) * Br[i]
    if mi > k:
      mi = k
  ans += mi

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
h,n = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]

D = [float("inf")] * (h+10**4+3)
D[0] = 0
for j in range(n):
    for i in range(len(D)//A[j][0] + 1):
        D[i] = min(D[i],D[i-A[j][0]] + A[j][1])
    # print(D[:20])
# print(D[:20])
print(min(D[h:]))

# a = float("inf")
# for i in range(n):
#     for j in range(n):
#         for k in range(h//A[i][0]+1):
#             b = A[i][1] * k + A[j][1] * max(0,-((-h+k*A[i][0])//A[j][0]))
#             a = min(a,b)
#             print(A[i][1] , k , A[j][1] , max(0,-((-h+k*A[i][0])//A[j][0])))
#             print(i,j,k,a)
        
# print(a)

# for i in range(n):
#     A[i].append(A[i][0]/ A[i][1])
# A.sort(reverse=True)
# A.sort(key=lambda x:x[2], reverse=True)
# print(A)
# a = 0
# while h > 0:
#     i=0
#     k = h//A[i][0]
#     h -= k * A[i][0]
#     a += k * A[i][1]
#     print(h)
#     if h > 0:
#         for i in range(n):
#             A[i][0] = min(A[i][0], h)
#             A[i][2] = A[i][0] / A[i][1]
#         A.sort(reverse=True)
#         A.sort(key=lambda x:x[2], reverse=True)
#     print(A)
# print(a)

Submission status: Runtime Error

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

ans = 0
dp = []
for _ in range(N):
    dp.append(10000000)
for n in range(N):
    for a in range(H):
        if a > A[n]:
            dp[a] = min(dp[a], dp[a - A[n]])
        else:
            dp[a] = min(dp[a], B[n])

print(dp[H]-1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
hp,n = map(int, input().split())
l = []
for i in range(n):
    damage,cost = map(int, input().split())
    eff = cost/damage
    l.append([eff,cost,damage])
    
l.sort()

total = 0

i = 0
while True:
    eff,cost,damage = l[i]
    
    if hp>damage:
        hit = hp//damage
        hp = hp%damage
        
        total += hit * cost
        if hp == 0:
            print('1-1')
            break
        else:
            eff2,cost2,damage2 = l[i+1]
            if cost <= cost2 * -(-hp//damage2):
                total += cost

                break
            else:
                i += 1

                continue
    else:
        if cost <= cost2 * -(-hp//damage2):
            total += cost

            break
        else:
            i += 1

            continue
        

print(total)

Submission status: Runtime Error

Solution code:
h, n = map(int, input().split())
cnt = 0
a = []
b = []
c = []
d = []
for i in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append((i, ai/bi))
c.sort(reverse=True, key=lambda x:x[1])
ok = (a[c[0][0]], b[c[0][0]])
for i in range(1, h+1):
    mincost = 10**10
    mincostatk = 0
    for j in range(n):
        if a[j] > i:
            if mincost < b[j]:
                mincost = b[j]
                mincostatk = a[j]
    if mincostatk == 0:
        mincost = ok[1]
        mincostatk = ok[0]
    d.append((mincostatk, mincost))
while h > 0:
    print(h, d[h-1][0])
    h = h - d[h-1][0]
    cnt += d[h-1][1]
print(cnt)

Submission status: Runtime Error

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

cost = [10**12]*10**4
cost[0] = 0
que = [0]
dmg = [0]*N
mp = [0]*N

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

while que != [] :
        target = que.pop(0)
        for i in range(N):
            crnt_dmg = target+dmg[i]
            crnt_cost = cost[target] + mp[i]
            if crnt_cost < cost[crnt_dmg] and crnt_cost < cost[H]:
                cost[crnt_dmg] = crnt_cost
                if crnt_dmg < H:
                    que.append(crnt_dmg)

    
print(min(cost[(H):]))

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 = map(int, input().split())
a = []
b = []
for _ in range(n):
    ai, bi = map(int, input().split())
    a.append(ai)
    b.append(bi)

dp = [float("inf")] * (3 * h)
dp[0] = 0
for i in range(h):
    for j in range(n):
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])

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

Submission status: Runtime Error

Solution code:
def attk(HP,Magic,D0):
    T = HP // Magic[0][0]
    MD0 = T * Magic[0][1]
    MD1 = (T+1) * Magic[0][1]
    Z = HP % Magic[0][0]
    ND = Magic[0][1]
    if len(Magic) == 1:
        return (MD1)
    A = attk(Z,Magic.pop(0),ND)
    if D0 > MD0+A:
        if MD1 > MD0+A:
            return MD0+A
        else:
            return MD1
    else:
        return D0

H,N = map(int,input().split())
A =[]
for i in range(N):
    a,b = map(int,input().split())
    A.append((a,b,a/b))
print(A)
B = sorted(A, key=lambda s: s[1]) 
C = sorted(B, key=lambda s: s[2], reverse=True) 
print(C)

for i in range(N-1):
    if C[N-1-i][2] ==C[N-2-i][2]:
        C.pop(N-1-i)
print(C)
TDMAX = (H // C[0][0])*C[0][1]
if (H%C[0][0])!=0:
    TDMAX += c[0][1]
TD = attk(H,C,TDMAX)
print(TD)

Submission status: Runtime Error

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

def ibis(h,a,b,n):
    if h <= 0:
        return 0
    else:
        c = [0]*n
        for i in range(n):
            c[i] = b[i] + ibis(h-a[i],a,b,n)
        return(min(c))
 
print(ibis(h,a,b,n))

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=[[0]*(H+100)]*(N+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:
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])
    
for c,a,b in data:
    dp[-1] = min(dp[-2*a] + (2*b) ,dp[-1])

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

Submission status: Runtime Error

Solution code:
H,N = map(int, input().split())
A_B = [list(map(int, input().split())) for i in range(N)]
A= [0]*N
B= [0]*N
A[i] = A_B[i][0]
B[i] = A_B[i][0]
cp = [0]*N
for i in range(N):
    cp[i] =(A_B[i][0])/(A_B[i][1])
main = A_B[cp.index(max(cp))][0]
print(main)
atk1 =H//main
B2=sorted(B)
for i in range(N):
    if B2[i] >= H%main:
        LA = B2[i]
        break
print(atk1+(A_B[B.index(LA)][1]))

Submission status: Runtime Error

Solution code:
def quick_sort(C):
    stand=C[1][0]/C[0][0]
    over=[[] for i in range(2)]
    same=[[] for i in range(2)]
    under=[[] for i in range(2)]
    for i in range(len(C[0])):
        if(C[1][i]/C[0][i]>stand):
            over[0].append(C[0][i])
            over[1].append(C[1][i])
        elif(C[1][i]/C[0][i]<stand):
            under[0].append(C[0][i])
            under[1].append(C[1][i])
        else:
            same[0].append(C[0][i])
            same[1].append(C[1][i])
    if(len(over[0])>1):
        over=list(quick_sort(over))
    else:
        pass
    if(len(under[0])>1):
        under=list(quick_sort(under))
    else:
        pass

    return [over[0]+same[0]+under[0],over[1]+same[1]+under[1]]

H,N=map(int,input().split())
C=[[0 for i in range(N)] for j in range(2)]
for i in range(N):
    C[0][i],C[1][i]=map(int,input().split())
C=quick_sort(C)
count=0
while(H!=0):
    if(H%C[0][N-1]==0):
        count+=(H//C[0][N-1])*C[1][N-1]
        H=H%C[0][N-1]
    else:
        count+=(H//C[0][N-1])*C[1][N-1]
        H=H%C[0][N-1]
        for i in range(N):
            if(C[0][i]>=H):
                C[0][i]=H
            else:
                C[1][i]=(H//C[0][i])*C[1][i]
        C=quick_sort(C)
        for i in range(N):
            if(C[0][i]<H):
                C[1][i]=C[1][i]/(H//C[0][i])
            else:
                pass
print(count)

Submission status: Runtime Error

Solution code:
import math
H,N=map(int,input().split())
AB=[]
for i in range(N):
    a,b = input().split()
    AB+=[(int(a),int(b),int(a)/int(b))]
AB=sorted(AB,key=lambda x:x[2],reverse=True)

def magic(H):
  iAB=0
  tA,tB=1,1
  mA,mB=AB[iAB][0],AB[iAB][1]
  a=math.floor(H/mA)
  while H>0:
    H-=mA*a
    tB+=mB*a
    iAB+=1
    mA,mB=AB[iAB][0],AB[iAB][1]
  return tB

if __name__=='__main__':
    print(magic(H))

Submission status: Runtime Error

Solution code:
# coding:utf-8

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

H_list = [0 for _ in range(H+1)]

for i in range(len(H_list)):
    if i == 0:
        for a, b in ab_list:
            H_list[a] = b
    elif H_list[i] != 0:
        for a, b in ab_list:
            idx = i + a
            if idx > len(H_list) - 1:
                idx = len(H_list) - 1
            if H_list[idx] == 0:
                H_list[idx] = H_list[i] + b
            else:
                H_list[idx] = min(H_list[i] + b, H_list[idx])

print(H_list[-1])

Submission status: Runtime Error

Solution code:
# import itertools
# import math
import numpy as np


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

AB.sort()

ans = 0
dp = np.zeros(H, dtype=int)
for a, b in AB:
    ans = max(ans, dp.max() + b)
    np.max(dp[:-a] + b, dp[a:], out=dp[a:])

print(ans)

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 = 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]
               count = 0
            else:
                count += 1
        break

print(result)

Submission status: Runtime Error

Solution code:
#include <vector> // vectorを使うためにインクルードする
#include <iostream>
using namespace std;

const int MAX_N = 1000;
const int MAX_M = 20000;

int n, H;
int w[MAX_N], v[MAX_N];
// dp[i+1][j]: i番目までの品物から重さの総和がj以下となるように選んだ時の,
// 価値の総和の最大値
int dp[MAX_N + 1][MAX_M + 1];

int main() {
    cin >> H;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> w[i] >> v[i];
    }

    for (int i = 0; i < n+1; i++) {
        for (int j = 0; j <= MAX_M+1; j++) {
            dp[i][j] = 100000000;
        }
    }
  for (int i = 0; i < n+1; i++) {
            dp[i][0] = 0;
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= MAX_M; j++) {
            dp[i+1][j] = min(dp[i][j],dp[i+1][j-w[i]]+v[i]);
        }
    }
    int ans = 100000000;
  for (int i=H; i<MAX_M+1; i++){
  	ans = min(ans, dp[n][i]);
  }

    cout << ans << '\n';

    return 0;
}

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]

    i = np.argmin((H // A) * B)
    
    if H < A[i]:
        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())
ab = [list(map(int,input().split())) for i in range(n)]
import copy
newab = copy.deepcopy(ab)
for i in newab:
    i.insert(0,i[0] / i[1])
newab.sort()
ans = 0
while h > 0:
    for i in newab:
        if h >=i[1]:
            h -= i[1]
            ans +=i[2]
            continue
        else:
            break
    continue
gyaku = []
for i in ab:
    gyaku.append(i.reverse)
gyaku.sort()
for i in gyaku:
    if i[1]>=h:
        ans += i[0]
print(ans)

Submission status: Runtime Error

Solution code:
h = int(input())

result = 0
i = 1
while h > 0:
    h = h // 2
    result = result + i
    i *= 2

print(result)