qyliang/CodeNanoFix huggingface dataset:
Buggy code:
# coding=utf-8
# Date: 2020-01-08
# Author: lee215


get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]


H, N = gets()
AB = getss(N)
AB.sort(key=lambda a_b: -a_b[0] * 1.0 / a_b[1])

a0, b0 = AB[0]
dp = [0] + [float('inf')] * (H + a0)

cur = 0
while cur <= H + a0:
    dp[cur] = cur / a0 * b0
    cur += a0


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


Fixed code:
# coding=utf-8
# Date: 2020-01-08
# Author: lee215
get = lambda: int(input().strip())
getl = lambda: input().strip()
gets = lambda: list(map(int, input().strip().split()))
getss = lambda n: [gets() for _ in range(n)]

H, N = gets()
AB = getss(N)
AB.sort(key=lambda a_b: -a_b[0] * 1.0 / a_b[1])

a0, b0 = AB[0]
dp = [0] + [float('inf')] * (H + a0)

cur = 0
while cur <= H + a0:
    dp[cur] = cur // a0 * b0
    cur += a0


for a, b in AB[1:]:
    for i in range(a, H + a0 + 1):
        dp[i] = min(dp[i], dp[i - a] + b)
print(min(dp[H:]))
Buggy code:
import sys

h, n = map(int, input().split())
magic = []
a_max = 0
for i in range(n):
    a, b = map(int, input().split())
    a_max = max(a, a_max)
    magic.append((a, b))

dp = [float('inf')] * (h + a_max)
dp[0] = 0
for i in range(h):
    for a, b in magic:
        if i + a > h + a_max:
            continue
        dp[i + a] = min(dp[i] + b, dp[i + a])
        print(dp[i + a])

print(min(dp[h:]))

Fixed code:
h, n = map(int, input().split())
magic = []
a_max = 0
for i in range(n):
    a, b = map(int, input().split())
    a_max = max(a, a_max)
    magic.append((a, b))

dp = [float('inf')] * (h + a_max)
dp[0] = 0
for i in range(h):
    for a, b in magic:
        if i + a > h + a_max:
            continue
        dp[i + a] = min(dp[i] + b, dp[i + a])

print(min(dp[h:]))
Buggy 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])

Fixed 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 range(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])
Buggy code:
H,N = map(int,input().split())
magic = [list(map(int,input().split())) for i in range(N)]

INF = 10**10
#print(magic)

dp = [INF for i in range(H+AM+1)]
dp[0] = 0

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


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

INF = 10**10
#print(magic)

AM = 0
for i in range(N):
    AM = max(AM,magic[i][0])
#print(AM)


dp = [INF for i in range(H+AM+1)]
dp[0] = 0

for i in range(1,H+AM+1):
    for j in range(N):
        if magic[j][0] > i:
            dp[i] = min(dp[i],magic[j][1])
        else:
            dp[i] = min(dp[i-magic[j][0]]+magic[j][1],dp[i])
ans = INF
for i in range(H,H+AM+1):
    ans = min(ans,dp[i])
print(ans)
Buggy code:
h, n = map(int, input().split())
inf = 10**12
data = []
for i in range(n):
    data.append(list(map(int, input().split())))
dp = [[inf for i in range(h+1 + 10**4)] for j in range(n+1)]
dp[0][0] = 0
for i in range(n):
    a, b = data[i]
    for j in range(h+1+10**4):
        if j - a >= 0:
            dp[i+1][j] = min(dp[i+1][j-a] + b, dp[i][j])
        else:
            dp[i+1][j] = min(dp[i][j], b)
print(dp[-1][h-1])

Fixed code:
h, n = map(int, input().split())
inf = 10**12
data = []
for i in range(n):
    data.append(list(map(int, input().split())))
dp = [[inf for i in range(h+1 + 10**4)] for j in range(n+1)]
for i in range(n):
    dp[i][0] = 0
for i in range(n):
    a, b = data[i]
    for j in range(h+1+10**4):
        if j - a >= 0:
            dp[i+1][j] = min(dp[i+1][j-a] + b, dp[i][j])
        else:
            dp[i+1][j] = min(b, dp[i][j])
print(dp[-1][h])
Buggy code:
h, n = [int(i) for i in input().split()]

magics = [None]


class Magic:
    def __init__(self, attack, cost):
        self.attack = attack
        self.cost = cost


maxA = 0
for i in range(n):
    attack, cost = [int(i) for i in input().split()]
    maxA = max(maxA, attack)
    magics.append(Magic(attack, cost))

dp = [[10 ** 9 for i in range(h + 1)] for j in range(n + 1)]

for i in range(n + 1):
    for j in range(h + 1):
        if j == 0:
            dp[i][j] = 0
        elif i == 0:
            continue
        elif j - magics[i].attack >= 0:
            dp[i][j] = min(dp[i - 1][j], dp[i][j - magics[i].attack] + magics[i].cost)

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

Fixed code:
h, n = [int(i) for i in input().split()]

magics = [None]


class Magic:
    def __init__(self, attack, cost):
        self.attack = attack
        self.cost = cost


maxA = 0
for i in range(n):
    attack, cost = [int(i) for i in input().split()]
    maxA = max(maxA, attack)
    magics.append(Magic(attack, cost))

dp = [[10 ** 9 for i in range(h + maxA)] for j in range(n + 1)]

for i in range(n + 1):
    for j in range(h + maxA):
        if j == 0:
            dp[i][j] = 0
        elif i == 0:
            continue
        elif j - magics[i].attack >= 0:
            dp[i][j] = min(dp[i - 1][j], dp[i][j - magics[i].attack] + magics[i].cost)
        else:
            dp[i][j] = dp[i - 1][j]

print(min(dp[-1][h:]))
Buggy code:
import math
import time

t = time.time()
def iip():
    return [int(i) for i in input().split()]

H, N = iip()
mp_for_damage = [10**9 for i in range(H+1)]
mp_for_damage[0] = 0
ab = []

for i in range(N):
    ab.append(iip())

ab= sorted(ab, key=lambda x:x[1])

for i in range(N):
    a, b = ab[i]
    for j in range(H+1):
        if a < H and mp_for_damage[a] < b:
            continue

        for k in range(1, a):
            if k > H:
                break
            mp_for_damage[k] = min(mp_for_damage[k], b)

        if j + a > H:
            break
        mp_for_damage[j+a] = min(mp_for_damage[j+a], mp_for_damage[j] + b)

#print(mp_for_damage)
print(mp_for_damage[H])

#print(time.time() - t)

Fixed code:
import math
import time


t = time.time()
def iip():
    return [int(i) for i in input().split()]

def main():
    H, N = iip()
    mp_for_damage = [10**9 for i in range(H+1)]
    mp_for_damage[0] = 0
    ab = []

    for i in range(N):
        ab.append(iip())

    ab= sorted(ab, key=lambda x:x[0]/x[1], reverse=True)

    for i in range(N):
        a, b = ab[i]

        for k in range(1, min(a, H) + 1):
            mp_for_damage[k] = min(mp_for_damage[k], b)
            # print(k, b)

        if a >= H:
            mp_for_damage[H] = min(mp_for_damage[H], b)
            continue

        if a < H and mp_for_damage[a] < b:
            continue

        for j in range(H-a+1):
            mp_for_damage[j+a] = min(mp_for_damage[j+a], mp_for_damage[j] + b)

    #print(mp_for_damage)
    #print(mp_for_damage)
    print(mp_for_damage[H])

    #print(time.time() - t)

main()
Buggy code:
H, N = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
print(H, N)

INF = int(1e18)
dp = [INF] * (H + 1)
dp[H] = 0;
for i in range(N):
    for j in range(H, -1, -1):
        dp[max(0, j - A[i][0])] = min(dp[max(0, j - A[i][0])], dp[j] + A[i][1])

ans = dp[0]
print(ans)


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

INF = int(1e18)
dp = [INF] * (H + 1)
dp[H] = 0;
for i in range(N):
    for j in range(H, -1, -1):
        dp[max(0, j - A[i][0])] = min(dp[max(0, j - A[i][0])], dp[j] + A[i][1])

ans = dp[0]
print(ans)
Buggy code:

H, N = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]
max_A = max([a for a, _b in AB])

dp = [10**18] * (N+max_A)
dp[0] = 0

for i in range(N+max_A):
  for a, b in AB:
    dp[i+a] = min(dp[i]+b, dp[i+a])
    
print(int(min(dp[H:])))

Fixed code:
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]
max_A = max([a for a, _b in AB])

dp = [10**18] * (H+max_A)
dp[0] = 0
#print(dp)

for i in range(H):
  for a, b in AB:
    dp[i+a] = min(dp[i]+b, dp[i+a])
    
print(int(min(dp[H:])))
Buggy code:
def solve(H, N, AB):
    INF = 10e10
    dp = [[INF] * (H+1) for i in range(N+1)]
    dp[0][0] = 0
    A, B = AB[0]
    #print(" ", *range(H+1), sep="\t")
    for j in range(H+1):
        dp[0][j] = min(dp[0][j], dp[0][j-A]+B)
    #print(0, *dp[0], sep="\t")
    for i in range(1,N):
        A, B = AB[i]
        for h in range(H+1):
            dp[i][h] = min(dp[i-1][h], dp[i][max(0, h-A)]+B)
        #print(i, *dp[i], sep="\t")
    
    return(dp[N-1][H])


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


Fixed code:
def solve(H, N, AB):
    INF = 10e10
    dp = [[INF] * (H+1) for i in range(N+1)]
    dp[0][0] = 0
    A, B = AB[0]
    for j in range(H+1):
        dp[0][j] = min(dp[0][j], dp[0][max(0, j-A)]+B)
    for i in range(1,N):
        A, B = AB[i]
        for h in range(H+1):
            dp[i][h] = min(dp[i-1][h], dp[i][max(0, h-A)]+B)
        #print(i, *dp[i], sep="\t")
    
    return(dp[N-1][H])


def main():
    H, N = map(int, input().split())
    AB = []
    for i in range(N):
        A, B = map(int, input().split())
        AB.append((A, B))
    print(solve(H, N, AB))
main()
Buggy code:
INF=10**10
def main():
  H,N=map(int,input().split())
  AB=[]
  for i in range(N):
    AB.append(list(map(int,input().split())))
  dp=[INF]*(H+1)
  dp[0]=0
  for a,b in AB:
    for k in range(H+1):
      tmp=k-a
      if tmp<0:
        tmp=0
      need_magic=dp[tmp]+b
      if need_magic<dp[k]:
        dp[k]=need_magic
  print(dp[H+1])
main()
      

Fixed code:
INF=10**10
def main():
  H,N=map(int,input().split())
  AB=[]
  for i in range(N):
    AB.append(list(map(int,input().split())))
  dp=[INF]*(H+1)
  dp[0]=0
  for a,b in AB:
    for k in range(H+1):
      tmp=k-a
      if tmp<0:
        tmp=0
      need_magic=dp[tmp]+b
      if need_magic<dp[k]:
        dp[k]=need_magic
  print(dp[H])
main()
Buggy code:
import math
import copy

def main():
    H,N=map(int,input().split())

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

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

                if j > H-A:
                    #dp[-1]=min(dp[-1],dp[j]+B)
                    if dp[j]+B < dp[-1]:
                        dp[-1]=dp[j]+B

    print(dp[-1])

main()

Fixed code:
import math

def main():
    H,N=map(int,input().split())

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

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

                if j > H-A:
                    #dp[-1]=min(dp[-1],dp[j]+B)
                    if dp[j]+B < dp[-1]:
                        dp[-1]=dp[j]+B

    print(dp[-1])

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

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

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

dp = [0]*(H+1)
for i in range(1, H+1):
  for j in range(N):
    if j == 0:
      if ls[j][0] <= i:
        dp[i] = dp[i-ls[j][0]] + ls[j][1]
      else:
        dp[i] = ls[j][1]    
    else:
      if ls[j][0] <= i:
        dp[i] = min([dp[i], dp[i-ls[j][0]] + ls[j][1]])
      else:
        dp[i] = min([dp[i], ls[j][1]])
        
print(dp[H])
Buggy code:
import sys
input = sys.stdin.readline
H, N = map(int, input().split())
dp = [float("inf")] * (H + 1)
dp[H] = 0
for _ in range(N):
  a, b = map(int, input().split())
  for x in range(H + 1):
    dp[max(0, x - a)] = min(dp[max(0, x - a)], dp[x] + b)

print(dp[0])

Fixed code:
import sys
input = sys.stdin.readline
H, N = map(int, input().split())
dp = [float("inf")] * (H + 1)
dp[H] = 0
for _ in range(N):
  a, b = map(int, input().split())
  for x in range(H, -1, -1):
    dp[max(0, x - a)] = min(dp[max(0, x - a)], dp[x] + b)
print(dp[0])
Buggy code:
import pprint
H, N = list(map(int, input().split()))

A, B = [0]*N, [0]*N

maxDamage = H + max(A)

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


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

for i in range(N):
    for j in range(maxDamage+1):

        if j < A[i]:
            dp[i+1][j] = dp[i][j]

        else:
            res1 = dp[i][j]
            res2 = dp[i+1][j - A[i]] + B[i]

            dp[i+1][j] = min(res1, res2)
            # pprint.pprint(dp)
# pprint.pprint(dp)
for i in range(H, maxDamage+1):

    if dp[N][i] < float("inf"):
        print(dp[N][i])
        exit()


Fixed code:
import pprint
H, N = list(map(int, input().split()))

A, B = [0]*N, [0]*N


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

maxDamage = H + max(A)

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

for i in range(N):
    for j in range(maxDamage+1):

        if j < A[i]:
            dp[i+1][j] = dp[i][j]

        else:
            res1 = dp[i][j]
            res2 = dp[i+1][j - A[i]] + B[i]

            dp[i+1][j] = min(res1, res2)

# pprint.pprint(dp)
print(min(dp[N][H:]))
Buggy code:
import sys
input = sys.stdin.buffer.readline
 
h,n = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]

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

for a,b in AB:
    for i in range(h):
        if dp[i]!=float("inf"):
            dp[min(i+a,h)]=min(dp[min(i+a,h),dp[i]+b])
print(dp[-1])
            

Fixed code:
import sys
input = sys.stdin.buffer.readline
 
h,n = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(n)]

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

for a,b in AB:
    for i in range(h):
        if dp[i]!=float("inf"):
            dp[min(i+a,h)]=min(dp[min(i+a,h)],dp[i]+b)
print(dp[-1])
Buggy code:
import sys
input = sys.stdin.readline

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)

Ama = max(A)

dp=[float("inf")]*(h+Ama)
dp[0]=0

for i in range(n):
    for j in range(A[i],h+Ama+1):
        dp[j]=min(dp[j],dp[j-A[i]+B[i]])
print(min(dp[h:]))

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

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)

Ama = max(A)

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

for i in range(n):
    for j in range(A[i], h+Ama+1):
        dp[j]=min(dp[j],dp[j-A[i]]+B[i])
print(min(dp[h:]))
Buggy code:
h,n=map(int,input().split())dp=[10**10 for i in range(h+1)]
dp[0]=0
for i in range(n):
    x,y=map(int,input().split())
    for j in range(h+1):
        nj=min(h,j+x)
        dp[nj]=min(dp[nj],dp[j]+y)
print(dp[h])

Fixed code:
h,n=map(int,input().split())
dp=[10**10 for i in range(h+1)]
dp[0]=0
for i in range(n):
    x,y=map(int,input().split())
    for j in range(h+1):
        nj=min(h,j+x)
        dp[nj]=min(dp[nj],dp[j]+y)
print(dp[h])
Buggy code:
def main():
    h, n = map(int, input().split())
    magic_list = []

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

    ans = solve(h, magic_list)
    print(ans)


def solve(h, magic_list):
    # 1-origin
    inf = 1 << 40
    max_attack = max(magic_list)[0]
    dp = [inf] * (h + max_attack)
    dp[0] = 0

    # time O(HN)
    ans = inf
    for hp in range(1, h + 1):
        for attack, mp in magic_list:
            if hp >= attack and dp[hp - attack] < inf:
                dp[hp] = min(dp[hp], dp[hp - attack] + mp)
                if hp >= h:
                    ans = min(ans, dp[hp])

    return ans


main()


Fixed code:
def main():
    h, n = map(int, input().split())
    magic_list = []

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

    ans = solve(h, magic_list)
    print(ans)


def solve(h, magic_list):
    # 1-origin
    inf = 1 << 40
    max_attack = max(magic_list)[0]
    dp = [inf] * (h + max_attack)
    dp[0] = 0

    # time O(HN)
    for hp in range(1, h + max_attack):
        for attack, mp in magic_list:
            if hp >= attack and dp[hp - attack] < inf:
                dp[hp] = min(dp[hp], dp[hp - attack] + mp)

    return min(dp[h:])


main()
Buggy 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])


Fixed 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[H])
Buggy code:
import time

def main():
    h, n = map(int, input().split())
    INF = 10 ** 9
    magic = []

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

    t = time.time()

    magic.sort(reverse=True, key=lambda x: x[0])

    dp = [INF] * (h + 10 ** 4)
    dp[0] = 0

    for a, b in magic:
        for i in range(h):
            if dp[i] != INF:
                tmp = dp[i] + b
                if tmp < dp[i + a]:
                    dp[i + a] = tmp

    ans = min(dp[h:])

    print(ans)
    print(time.time() - t)

main()

Fixed code:
import time

def main():
    h, n = map(int, input().split())
    INF = 10 ** 9
    magic = []

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

    # t = time.time()

    magic.sort(reverse=True, key=lambda x: x[0])

    dp = [INF] * (h + 10 ** 4)
    dp[0] = 0

    for a, b in magic:
        for i in range(h):
            if dp[i] != INF:
                tmp = dp[i] + b
                if tmp < dp[i + a]:
                    dp[i + a] = tmp

    ans = min(dp[h:])

    print(ans)
    # print(time.time() - t)

main()
Buggy code:
H, N = [int(_) for _ in input().split()]

A = []
ma = None

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

A.sort(key=lambda x: -x[2])
ans = 0
ans = (H // A[0][0]) * A[0][1]
h = H % A[0][0]

if h > 0:
    memo = [10 ** 9 + 7] * (h + 1)
    memo[h] = 0
    for i in range(N):
        a, b, c = A[i]
        for j in range(h, 0, -1):
            x = j // a
            if x > 0:
                y = max(j - a * x, 0)
                memo[y] = min(memo[y], memo[j] + x * b)

            if j % a > 0:
                x += 1
                memo[0] = min(memo[0], memo[j] + x * b)

    ans += memo[0]

print(ans)


Fixed code:
H, N = [int(_) for _ in input().split()]

A = []
ma = None

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

A.sort(key=lambda x: -x[2])
ans = 0
# ans = (H // A[0][0]) * A[0][1]
h = H # % A[0][0]

if h > 0:
    memo = [10 ** 9 + 7] * (h + 1)
    memo[h] = 0
    for j in range(h, 0, -1):
        for i in range(N):
            a, b, c = A[i]
            x = j - a
            if x < 0:
                x = 0
            memo[x] = min(memo[x], memo[j] + b)

    ans += memo[0]

print(ans)
Buggy code:
H, N = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]

dp = [[10 ** 9] * (H + 1) for _ in range(N + 1)]
dp[0][0] = 0
X.sort(key=lambda x: x[-1])

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

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


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

dp = [[10 ** 9] * (H + 1) for _ in range(N + 1)]
dp[0][0] = 0
X.sort(key=lambda x: -x[-1])

for i, (a, b) in enumerate(X):
    for j in range(H + 1):
        if j == 0:
            dp[i + 1][j] = 0
        if j + a < H:
            dp[i + 1][j + a] = min(dp[i][j + a], dp[i][j] + b, dp[i + 1][j] + b)
        else:
            dp[i + 1][-1] = min(dp[i][-1], dp[i + 1][-1], dp[i][j] + b, dp[i + 1][j] + b)            
            
print(dp[-1][-1])
Buggy code:
H,N =map(int, input().split())
a = [] 
M = 0 
for i in range(N):
	t = list(map(int, input().split()))
	a.append(t)
	M = max(M,t[0])
dp = [10000]*(H+1)
dp[0] = 0
for i in range(1,H+M):
	for j,k in a:
		dp[i+j] = min(dp[i+j],dp[i]+k)

print(min(dp[H:]))

Fixed code:
H,N =map(int, input().split())
a = [] 
M = 0 
for i in range(N):
	t = list(map(int, input().split()))
	a.append(t)
	M = max(M,t[0])
dp = [100000000]*(H+1+M)
dp[0] = 0
for i in range(H):
	for j,k in a:
		dp[i+j] = min(dp[i+j],dp[i]+k)

print(min(dp[H:]))
Buggy code:
f=lambda:map(int,input().split())
h,n=f()
dp=[0]+[10**9]*h
for i in range(n):
  a,b=f()
  for j in range(h+1): dp[j]=min(dp[j],dp[j-a]+b)
print(dp[h])

Fixed code:
f=lambda:map(int,input().split())
h,n=f()
dp=[0]+[10**9]*h
for i in range(n):
  a,b=f()
  for j in range(h+1): dp[j]=min(dp[j],dp[max(j-a,0)]+b)
print(dp[h])
Buggy code:
H,N = map(int,(input().split()))
dp = [10**9]*(H+1)
dp[0] = 0

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

for i in range(H+1):
    for j,k in A:
        next = min(H,i+j)
        dp[next] = min(dp[next],dp[i]+k)

print(dp[H])

Fixed code:
H,N = map(int,(input().split()))
dp = [10**9]*(H+1)
dp[0] = 0

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

for i in range(H+1):
    for j,k in A:
        next = min(H,i+j)
        dp[next] = min(dp[next],dp[i]+k)

print(dp[H])