qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
from functools import lru_cache
from bisect import bisect_left as lb, bisect_right as ub
from collections import deque, defaultdict
from heapq import heapify, heappop, heappush
from math import gcd, ceil, sqrt
rd = lambda: int(sys.stdin.readline())
rds = lambda: map(int, sys.stdin.readline().split())
rdl = lambda: list(map(int, sys.stdin.readline().split()))
inf = 2 * 10 ** 18
sys.setrecursionlimit(10 ** 7)
rg = range
pr = print


T = rd()
for i in rg(T):
  n, a, b, c, d = rds()

  @lru_cache(maxsize=10 ** 7)
  def f(m):
    # print(m)
    if m == 0:
      return 0
    if m == 1:
      return d
    ret = inf
    # 2
    q, r = m // 2, m % 2
    ret = min(ret, f(q) + d * r + a)
    if r:
      ret = min(ret, f(q + 1) + d * (2 - r) + a)

    q, r = m // 3, m % 3
    ret = min(ret, f(q) + d * r + b)
    if r:
      ret = min(ret, f(q + 1) + d * (3 - r) + b)

    q, r = m // 5, m % 5
    ret = min(ret, f(q) + d * r + c)
    if r:
      ret = min(ret, f(q + 1) + d * (5 - r) + c)
    return ret

  pr(f(n))


Fixed code:
import sys
from functools import lru_cache
from bisect import bisect_left as lb, bisect_right as ub
from collections import deque, defaultdict
from heapq import heapify, heappop, heappush
from math import gcd, ceil, sqrt
rd = lambda: int(sys.stdin.readline())
rds = lambda: map(int, sys.stdin.readline().split())
rdl = lambda: list(map(int, sys.stdin.readline().split()))
inf = 2 * 10 ** 18
sys.setrecursionlimit(10 ** 7)
rg = range
pr = print


T = rd()
for i in rg(T):
  n, a, b, c, d = rds()

  @lru_cache(maxsize=10 ** 7)
  def f(m):
    # print(m)
    if m == 1:
      return d
    ret = d * m
    # 2
    q, r = m // 2, m % 2
    if q:
      ret = min(ret, f(q) + d * r + a)
    if r:
      ret = min(ret, f(q + 1) + d * (2 - r) + a)

    q, r = m // 3, m % 3
    if q:
      ret = min(ret, f(q) + d * r + b)
    if r:
      ret = min(ret, f(q + 1) + d * (3 - r) + b)

    q, r = m // 5, m % 5
    if q:
      ret = min(ret, f(q) + d * r + c)
    if r:
      ret = min(ret, f(q + 1) + d * (5 - r) + c)
    return ret

  pr(f(n))
Buggy code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**5)

T=int(input())

def saiki(n):
    if n==0:
        return 0
    elif n==1:
        return D
    else:
        value=min(
            D*n,
            saiki(n//2)+A+D*abs(n - n//2*2),
            saiki((n+1)//2)+A+D * abs(n - (n+4)//5*5),
            saiki(n//3)+B+D*abs(n - n//3*3),
            saiki((n+2)//3)+B+D * abs(n - (n+2)//3*3),
            saiki(n//5)+C+D*abs(n - n//5*5),
            saiki((n+4)//5)+C+D * abs(n - (n+4)//5*5)
            )
    return value

for _ in range(T):
    ans=[]
    N,A,B,C,D=map(int,input().split())
    print(saiki(N))

Fixed code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**7)

T=int(input())



def saiki(n):

    if n in memo:
        return memo[n]
        
    if n==0:
        return 0
    elif n==1:
        return D
    else:
        value=min(
            D*n,
            saiki(n//2)    +A+ D *abs(n - n//2*2),
            saiki((n+1)//2)+A+ D *abs(n - (n+1)//2*2),
            saiki(n//3)    +B+ D *abs(n - n//3*3),
            saiki((n+2)//3)+B+ D *abs(n - (n+2)//3*3),
            saiki(n//5)    +C+ D *abs(n - n//5*5),
            saiki((n+4)//5)+C+ D *abs(n - (n+4)//5*5)
            )
        
    memo[n]=value
    return value

for _ in range(T):
    memo={}
    ans=[]
    N,A,B,C,D=map(int,input().split())
    print(saiki(N))
Buggy code:
import sys
sys.setrecursionlimit(10**8)
def main():
    n, a, b, c, d = map(int, input().split())
    memo = {}
    def dfs(x):
        if x == 0:
            return 0
        elif x == 1:
            return d
        elif x in memo:
            return memo[x]
        else:
            ret = min(
                d * x,
                d * (x - x//2*2) + a + dfs(x//2),
                d * ((x+1)//2*2-x) + a + dfs((x+1)//2),
                d * (x - x//3*2) + b + dfs(x//3),
                d * ((x+2)//3*3 - x) + b + dfs((x+2)//3),
                d * (x - x//5*5) + c + dfs(x//5),
                d * ((x+4)//5*5 - x) + c + dfs((x+4)//5)
            )
            memo[x] = ret
            return ret
    print(dfs(n))

t = int(input())
for i in range(t):
    main()

Fixed code:
import sys
sys.setrecursionlimit(10**8)
def main():
    n, a, b, c, d = map(int, input().split())
    memo = {}
    def dfs(x):
        if x == 0:
            return 0
        elif x == 1:
            return d
        elif x in memo:
            return memo[x]
        else:
            ret = min(
                d * x,
                d * (x - x//2*2) + a + dfs(x//2),
                d * ((x+1)//2*2 - x) + a + dfs((x+1)//2),
                d * (x - x//3*3) + b + dfs(x//3),
                d * ((x+2)//3*3 - x) + b + dfs((x+2)//3),
                d * (x - x//5*5) + c + dfs(x//5),
                d * ((x+4)//5*5 - x) + c + dfs((x+4)//5)
            )
            memo[x] = ret
            return ret
    print(dfs(n))

t = int(input())
for i in range(t):
    main()
Buggy code:
import sys
sys.setrecursionlimit(2147483647)
from functools import lru_cache

@lru_cache(maxsize=None)
def solve(n, a, b, c, d):
    if n == 0:
        return 0
    if n == 1:
        return d
    res = 10**18
    res = min(res, d * n)
    res = min(res, d * (n - n // 2 * 2) + a + solve(n // 2, a, b, c, d))
    res = min(res, d * (n - n // 3 * 3) + b + solve(n // 3, a, b, c, d))
    res = min(res, d * (n - n // 5 * 5) + c + solve(n // 5, a, b, c, d))
    res = min(res, d * (n - (n + 2 - 1) // 2 * 2 + a + solve((n + 2 - 1) // 2, a, b, c, d)))
    res = min(res, d * (n - (n + 3 - 1) // 3 * 3 + b + solve((n + 3 - 1) // 3, a, b, c, d)))
    res = min(res, d * (n - (n + 5 - 1) // 5 * 5 + c + solve((n + 5 - 1) // 5, a, b, c, d)))
    return res

T = int(input())
for _ in range(T):
    N, A, B, C, D = map(int, input().split())
    print(solve(N, A, B, C, D))

Fixed code:
import sys
sys.setrecursionlimit(2147483647)
from functools import lru_cache

@lru_cache(maxsize=None)
def solve(n, a, b, c, d):
    if n == 0:
        return 0
    if n == 1:
        return d
    res = 10**18
    res = min(res, d * n)
    res = min(res, d * (n - n // 2 * 2) + a + solve(n // 2, a, b, c, d))
    res = min(res, d * (n - n // 3 * 3) + b + solve(n // 3, a, b, c, d))
    res = min(res, d * (n - n // 5 * 5) + c + solve(n // 5, a, b, c, d))
    res = min(res, d * ((n + 2 - 1) // 2 * 2 - n) + a + solve((n + 2 - 1) // 2, a, b, c, d))
    res = min(res, d * ((n + 3 - 1) // 3 * 3 - n) + b + solve((n + 3 - 1) // 3, a, b, c, d))
    res = min(res, d * ((n + 5 - 1) // 5 * 5 - n) + c + solve((n + 5 - 1) // 5, a, b, c, d))
    return res

T = int(input())
for _ in range(T):
    N, A, B, C, D = map(int, input().split())
    print(solve(N, A, B, C, D))
Buggy code:
def main()
  def dfs(x):
    m=x//2
    if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a:
      dp[m]=dp[x]+abs(2*m-x)*d+a
      dfs(m)
    m=0--x//2
    if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a:
      dp[m]=dp[x]+abs(2*m-x)*d+a
      dfs(m)
    m=x//3
    if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b:
      dp[m]=dp[x]+abs(3*m-x)*d+b
      dfs(m)
    m=0--x//3
    if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b:
      dp[m]=dp[x]+abs(3*m-x)*d+b
      dfs(m)
    m=x//5
    if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c:
      dp[m]=dp[x]+abs(5*m-x)*d+c
      dfs(m)
    m=0--x//5
    if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c:
      dp[m]=dp[x]+abs(5*m-x)*d+c
      dfs(m)
  def solve():
    dp[n]=0
    dfs(n)
    return min(dp[i]+abs(i)*d for i in dp)
  for _ in range(int(input())):
    n,a,b,c,d=map(int,input().split())
    dp={}
    print(solve())
if __name__ == "__main__":
  main()

Fixed code:
import sys
input=sys.stdin.readline
def main():
  def dfs(x):
    m=x//2
    if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a:
      dp[m]=dp[x]+abs(2*m-x)*d+a
      dfs(m)
    m=0--x//2
    if m not in dp or dp[m]>dp[x]+abs(2*m-x)*d+a:
      dp[m]=dp[x]+abs(2*m-x)*d+a
      dfs(m)
    m=x//3
    if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b:
      dp[m]=dp[x]+abs(3*m-x)*d+b
      dfs(m)
    m=0--x//3
    if m not in dp or dp[m]>dp[x]+abs(3*m-x)*d+b:
      dp[m]=dp[x]+abs(3*m-x)*d+b
      dfs(m)
    m=x//5
    if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c:
      dp[m]=dp[x]+abs(5*m-x)*d+c
      dfs(m)
    m=0--x//5
    if m not in dp or dp[m]>dp[x]+abs(5*m-x)*d+c:
      dp[m]=dp[x]+abs(5*m-x)*d+c
      dfs(m)
  def solve():
    dp[n]=0
    dfs(n)
    return min(dp[i]+abs(i)*d for i in dp)
  for _ in range(int(input())):
    n,a,b,c,d=map(int,input().split())
    dp={}
    print(solve())
if __name__ == "__main__":
  main()
Buggy code:
import heapq
from collections import deque

T = int(input())
for t in range(T):
    N,A,B,C,D = map(int,input().split())
    q = [(0,N)]
    heapq.heapify(q)
    seen = set()
    while q:
        cost, n = heapq.heappop(q)
        if n in seen:
            continue
        seen.add(n)
        if n == 0:
            print(cost)
            break

        heapq.heappush(q, (cost + N * D, 0))
        if n % 2 ==0:
            heapq.heappush(q, (cost + A, n // 2))
        else: # odd number
            heapq.heappush(q, (cost + D + A, (n + 1) // 2))
            heapq.heappush(q, (cost + D + A, (n - 1) // 2))
        if n % 3 ==0:
            heapq.heappush(q, (cost + B, n // 3))
        else:
            rest = n % 3
            heapq.heappush(q, (cost + rest * D + B, (n - rest) // 3))
            heapq.heappush(q, (cost + (3-rest) * D + B, (n - rest + 3) // 3))

        if n % 5 ==0:
            heapq.heappush(q, (cost + C, n // 5))
        else:
            rest = n % 5
            heapq.heappush(q, (cost + rest * D + C, (n - rest) // 5))
            heapq.heappush(q, (cost + (5-rest) * D + C, (n - rest + 5) // 5))



Fixed code:
import sys
import heapq
T = int(input())
from collections import deque
for t in range(T):
  N,A,B,C,D = map(int,input().split())
  q = [(0, N)]
  heapq.heapify(q)
  seen = set()
  while q:
    cost,n = heapq.heappop(q)
    if n in seen:
      continue
    seen.add(n)
    if n == 0:
      print(cost)
      break

    heapq.heappush(q, (cost + n * D, 0))
    if n % 2 == 0:
      heapq.heappush(q, (cost + A, n // 2))
    else:
      heapq.heappush(q, (cost + D + A, (n + 1) // 2))
      heapq.heappush(q, (cost + D + A, (n - 1) // 2))

    if n % 3 == 0:
      heapq.heappush(q, (cost + B, n // 3))
    else:
      rest = n % 3
      heapq.heappush(q, (cost + rest * D + B, (n - rest) // 3))
      heapq.heappush(q, (cost + (3 - rest) * D + B, (n - rest + 3) // 3))

    if n % 5 == 0:
      heapq.heappush(q, (cost + C, n // 5))
    else:
      rest = n % 5
      heapq.heappush(q, (cost + rest * D + C, (n - rest) // 5))
      heapq.heappush(q, (cost + (5 - rest) * D + C, (n - rest + 5) // 5))
Buggy code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

T = int(input())


for _ in range(T):
    N, A, B, C, D = map(int, input().split())
    memo = {}


    def f(n):
        if n == 0:
            return 0
        if n == 1:
            return D
        if n in memo.keys():
            return memo[n]

        ret = 1 << 100
        ret = min(ret, f(n // 2) + (n - n // 2 * 2) * D + A, f(n // 2) + (n - n // 2) * D)
        ret = min(ret, f(n // 3) + (n - n // 3 * 3) * D + B, f(n // 3) + (n - n // 3) * D)
        ret = min(ret, f(n // 5) + (n - n // 5 * 5) * D + C, f(n // 5) + (n - n // 5) * D)
        ret = min(ret, f(n // 2) + ((n + 1) // 2 * 2 - n) * D + A, f((n + 1) // 2) + (n - (n + 1) // 2) * D)
        ret = min(ret, f(n // 3) + ((n + 2) // 3 * 3 - n) * D + B, f((n + 2) // 3) + (n - (n + 2) // 3) * D)
        ret = min(ret, f(n // 5) + ((n + 4) // 5 * 5 - n) * D + C, f((n + 4) // 5) + (n - (n + 4) // 5) * D)
        memo[n] = ret
        return ret

    print(f(N))

Fixed code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

T = int(input())


for _ in range(T):
    N, A, B, C, D = map(int, input().split())
    memo = {}


    def f(n):
        if n == 0:
            return 0
        if n == 1:
            return D
        if n in memo.keys():
            return memo[n]

        ret = 1 << 100
        ret = min(ret, f(n // 2) + (n - n // 2 * 2) * D + A, f(n // 2) + (n - n // 2) * D)
        ret = min(ret, f(n // 3) + (n - n // 3 * 3) * D + B, f(n // 3) + (n - n // 3) * D)
        ret = min(ret, f(n // 5) + (n - n // 5 * 5) * D + C, f(n // 5) + (n - n // 5) * D)
        ret = min(ret, f((n + 1) // 2) + ((n + 1) // 2 * 2 - n) * D + A, f((n + 1) // 2) + (n - (n + 1) // 2) * D)
        ret = min(ret, f((n + 2) // 3) + ((n + 2) // 3 * 3 - n) * D + B, f((n + 2) // 3) + (n - (n + 2) // 3) * D)
        ret = min(ret, f((n + 4) // 5) + ((n + 4) // 5 * 5 - n) * D + C, f((n + 4) // 5) + (n - (n + 4) // 5) * D)
        memo[n] = ret
        return ret

    print(f(N))
Buggy code:
import sys
sys.setrecursionlimit(10 ** 9)
t = int(input())
for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    memo = {i: -1 for i in range(n+1)}
    def f(x):
        if memo[x] != -1:
            return memo[x]
        if x == 0:
            return 0
        if abs(x) == 1:
            return d

        ans = []
        if x % 2 == 0:
            ans.append(f(x // 2) + a)
        else:
            ans.append(f((x+1) // 2) + a + d)
            ans.append(f((x-1) // 2) + a + d)

        if x % 3 == 0:
            ans.append(f(x // 3) + b)
        elif x % 3 == 1:
            ans.append(f((x-1) // 3) + b + d)
        elif x % 3 == 2:
            ans.append(f((x+1) // 3) + b + d)

        if x % 5 == 0:
            ans.append(f(x // 5) + c)
        elif x % 5 == 1:
            ans.append(f((x-1) // 5) + c + d)
        elif x % 5 == 2:
            ans.append(f((x-2) // 5) + c + 2 * d)
        elif x % 5 == 3:
            ans.append(f((x+2) // 5) + c + 2 * d)
        elif x % 5 == 4:
            ans.append(f((x+1) // 5) + c + d)

        memo[x] = min(ans)
        return memo[x]

    print(f(n))

Fixed code:
import sys
sys.setrecursionlimit(10 ** 9)
t = int(input())
for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    memo = {}
    def f(x):
        if x in memo:
            return memo[x]
        if x == 0:
            return 0
        if abs(x) == 1:
            return d

        ans = [x * d]
        if x % 2 == 0:
            ans.append(f(x // 2) + a)
        else:
            ans.append(f((x+1) // 2) + a + d)
            ans.append(f((x-1) // 2) + a + d)

        if x % 3 == 0:
            ans.append(f(x // 3) + b)
        elif x % 3 == 1:
            ans.append(f((x-1) // 3) + b + d)
        elif x % 3 == 2:
            ans.append(f((x+1) // 3) + b + d)

        if x % 5 == 0:
            ans.append(f(x // 5) + c)
        elif x % 5 == 1:
            ans.append(f((x-1) // 5) + c + d)
        elif x % 5 == 2:
            ans.append(f((x-2) // 5) + c + 2 * d)
        elif x % 5 == 3:
            ans.append(f((x+2) // 5) + c + 2 * d)
        elif x % 5 == 4:
            ans.append(f((x+1) // 5) + c + d)

        memo[x] = min(ans)
        return memo[x]

    print(f(n))
Buggy code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10**6)


def main():
    T = int(input())
    TESTCASEs = [list(map(int, input().split())) for _ in range(T)]


    def calc_cost(N, A, B, C, D):


        #@lru_cache(maxsize=None)
        def calc(N):
            if N == 0:
                return 0
            if N == 1:
                return D

            cost = N * D
            
            for T, Tcost in ((2, A), (3, B), (5, C)):
                div, mod = divmod(N, T)

                cost = min(cost, calc(div) + Tcost + mod * D)
                if mod:
                    cost = min(cost, calc(div + 1) + Tcost + (T - mod) * D)

            return cost


        return calc(N)


    for TESTCASE in TESTCASEs:
        total_cost = calc_cost(*TESTCASE)
        print(total_cost)


if __name__ == '__main__':
    main()

sys.exit()


Fixed code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10**6)


def main():
    T = int(input())
    TESTCASEs = [list(map(int, input().split())) for _ in range(T)]


    def calc_cost(N, A, B, C, D):


        @lru_cache(maxsize=None)
        def calc(N):
            if N == 0:
                return 0
            if N == 1:
                return D

            cost = N * D
            
            for T, Tcost in ((2, A), (3, B), (5, C)):
                div, mod = divmod(N, T)

                cost = min(cost, calc(div) + Tcost + mod * D)
                if mod:
                    cost = min(cost, calc(div + 1) + Tcost + (T - mod) * D)

            return cost


        return calc(N)


    for TESTCASE in TESTCASEs:
        total_cost = calc_cost(*TESTCASE)
        print(total_cost)


if __name__ == '__main__':
    main()

sys.exit()
Buggy code:
T=int(input())
for i in range(T):
  N,a,b,c,d=map(int, input().split())
  memo={}
  memo[0]=0
  memo[1]=d
  
  def f(n):
    if n in memo:
      return memo[n]
    e2,e3,e5=n%2,n%3,n%5
    dd=min(d*n,d*2+a+f((n-e2)//2),d*2+a+f((n+e2)//2),d*e3+b+f((n-e3)//3),d*(3-e3)+b+f((n+3-e3)//3),d*e5+c+f((n-e5)//5),d*(5-e5)+c+f((n+5-e5)//5))
    memo[n]=dd
    return dd
  print(f(n))

Fixed code:
T=int(input())
for i in range(T):
  N,a,b,c,d=map(int, input().split())
  memo={}
  memo[0]=0
  memo[1]=d
  
  def f(n):
    if n in memo:
      return memo[n]
    e2,e3,e5=n%2,n%3,n%5
    dd=min(d*n,d*e2+a+f((n-e2)//2),d*e2+a+f((n+e2)//2),d*e3+b+f((n-e3)//3),d*(3-e3)+b+f((n+3-e3)//3),d*e5+c+f((n-e5)//5),d*(5-e5)+c+f((n+5-e5)//5))
    memo[n]=dd
    return dd
  print(f(N))
Buggy code:
def dist(n):
  if n==0:
    return 0
  if n==1:
    return d
  if n in memo:
    return memo[n]
  
  res=min(
    d*n,
    d*abs(n-n//5*5)+c+dist(n//5),
    d*abs(n-(n+4)//5*5)+c+dist(n//5),
    d*abs(n-n//3*3)+b+dist(n//3),
    d*abs(n-(n+2)//3*3)+b+dist(n//3),
    d*abs(n-n//2*2)+a+dist(n//2),
    d*abs(n-(n+1)//2*2)+a+dist(n//2)
  )
  memo[n]=res
  return res

T=int(input())
for i in range(T):
  n,a,b,c,d=map(int,input().split())
  memo={}
  print(dist(n))



Fixed code:
def dist(n):
  if n==0:
    return 0
  if n==1:
    return d
  if n in memo:
    return memo[n]
  
  res=min(
    d*n,
    d*abs(n-n//5*5)+c+dist(n//5),
    d*abs(n-(n+4)//5*5)+c+dist((n+4)//5),
    d*abs(n-n//3*3)+b+dist(n//3),
    d*abs(n-(n+2)//3*3)+b+dist((n+2)//3),
    d*abs(n-n//2*2)+a+dist(n//2),
    d*abs(n-(n+1)//2*2)+a+dist((n+1)//2)
  )
  memo[n]=res
  return res

T=int(input())
for _ in range(T):
  n,a,b,c,d=map(int,input().split())
  memo={}
  print(dist(n))
Buggy code:
from heapq import heappop,heappush
from collections import defaultdict
t = int(input())
for i in range(t):
  n,a,b,c,d=map(int,input().split())
  q=[(0,n)]
  memo = defaultdict(int)
  while q:
    s,p = heappop(q)
    if memo[p]:
      continue
    memo[p] = 1
    if p == 0:
      print(s)
      return
    heappush(q,(s+d*p,0))
    if p & 1:
      heappush(q,(s+d+a, p//2))
      heappush(q,(s+d+a, p//2+1))
    else:
      heappush(q,(s+a, p//2))
    if p % 3:
      heappush(q,(s+d*(p%3)+b, p//3))
      heappush(q,(s+d*(-p%3)+b, p//3+1))
    else:
      heappush(q,(s+b, p//3))
    if p % 5:
      heappush(q,(s+d*(p%5)+c, p//5))
      heappush(q,(s+d*(-p%5)+c, p//5+1))
    else:
      heappush(q,(s+c, p//5))
    

Fixed code:
from heapq import heappop,heappush
from collections import defaultdict
t = int(input())
for i in range(t):
  n,a,b,c,d=map(int,input().split())
  q=[(0,n)]
  memo = defaultdict(int)
  while q:
    s,p = heappop(q)
    if memo[p]:
      continue
    memo[p] = 1
    if p == 0:
      print(s)
      break
    heappush(q,(s+d*p,0))
    if p & 1:
      heappush(q,(s+d+a, p//2))
      heappush(q,(s+d+a, p//2+1))
    else:
      heappush(q,(s+a, p//2))
    if p % 3:
      heappush(q,(s+d*(p%3)+b, p//3))
      heappush(q,(s+d*(-p%3)+b, p//3+1))
    else:
      heappush(q,(s+b, p//3))
    if p % 5:
      heappush(q,(s+d*(p%5)+c, p//5))
      heappush(q,(s+d*(-p%5)+c, p//5+1))
    else:
      heappush(q,(s+c, p//5))
Buggy code:
def calc(x):
    if x not in dp:
        divs = [5, 3, 2]
        costs = [c, b, a]
        values = lambda div, cost: [calc(x // div) + cost + x % div * d, calc((x + div - 1) // div) + cost + (-x) % div * d]
        dp[x] = min(x * d, *[value for div, cost in zip(divs, costs) for value in values(div, cost)])
    return dp[x]

for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    dp = {0: 0, 1: d}
    print(calc(n))


Fixed code:
t = int(input())

def calc(x):
    if x not in dp:
        divs = [5, 3, 2]
        costs = [c, b, a]
        values = lambda div, cost: [calc(x // div) + cost + x % div * d, calc((x + div - 1) // div) + cost + (-x) % div * d]
        dp[x] = min(x * d, *[value for div, cost in zip(divs, costs) for value in values(div, cost)])
    return dp[x]

for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    dp = {0: 0, 1: d}
    print(calc(n))
Buggy code:
memo=dict()
t=int(input())
def f(x):
	if x in memo:
		return memo[x]
	if x<=1:
		return D*x
	ret=D*x

	if x%2==0:
		ret=min(ret,f(x//2)+A)
	else:
		ret=min(ret,f(x//2)+A+D,f(x//2+1)+A+D)
	
	if x%3==0:
		ret=min(ret,f(x//3)+B)
	elif x%3==1:
		ret=min(ret,f(x//3)+B+D)
	else:
		ret=min(ret,f(x//3)+B+D)

	if x%5<=2:
		ret=min(ret,f(x//5)+C+D*(x%5))
	else:
		ret=min(ret,f(x//5)+C+D*(5-x%5))
	memo[x]=ret
	return ret
for _ in range(t):
	memo=dict()
	n,A,B,C,D=map(int,input().split())
	print(f(n))

Fixed code:
memo=dict()
t=int(input())
def f(x):
	if x in memo:
		return memo[x]
	if x<=1:
		return D*x
	ret=D*x

	if x%2==0:
		ret=min(ret,f(x//2)+A)
	else:
		ret=min(ret,f(x//2)+A+D,f(x//2+1)+A+D)
	
	if x%3==0:
		ret=min(ret,f(x//3)+B)
	elif x%3==1:
		ret=min(ret,f(x//3)+B+D)
	else:
		ret=min(ret,f(x//3+1)+B+D)

	if x%5<=2:
		ret=min(ret,f(x//5)+C+D*(x%5))
	else:
		ret=min(ret,f(x//5+1)+C+D*(5-x%5))
	memo[x]=ret
	return ret
for _ in range(t):
	memo=dict()
	n,A,B,C,D=map(int,input().split())
	print(f(n))
Buggy code:
from math import ceil
import sys
sys.setrecursionlimit(10**7)
memo=dict()
def dfs(x,a,b,c,d):
    if x==0:
        return 0
    if x==1:
        return d
    if x in memo:
        return memo[x]
    l2=2*(x//2)
    r2=2*ceil(x/2)
    l3=3*(x//3)
    r3=3*ceil(x/3)
    l5=5*(x//5)
    r5=5*ceil(x/5)
    mini=10**18
    mini=min(mini,abs(l2-x)*d+a+dfs(x//2,a,b,c,d))
    mini=min(mini,abs(r2-x)*d+a+dfs(ceil(x/2),a,b,c,d))
    mini=min(mini,abs(l3-x)*d+b+dfs(x//3,a,b,c,d))
    mini=min(mini,abs(r3-x)*d+b+dfs(ceil(x/3),a,b,c,d))
    mini=min(mini,abs(l5-x)*d+c+dfs(x//5,a,b,c,d))
    mini=min(mini,abs(r5-x)*d+c+dfs(ceil(x/5),a,b,c,d))
    memo[x]=mini
    return mini
t=int(input())
for _ in range(t):
    memo=dict()
    n,a,b,c,d=map(int,input().split())
    print(dfs(n,a,b,c,d))

Fixed code:
from math import ceil
import sys
sys.setrecursionlimit(10**7)
t=int(input())
for _ in range(t):
    memo=dict()
    n,a,b,c,d=map(int,input().split())
    def dfs(x):
        if x==0:
            return 0
        if x==1:
            return d
        if x in memo:
            return memo[x]
        l2=2*(x//2)
        r2=2*((x+1)//2)
        l3=3*(x//3)
        r3=3*((x+2)//3)
        l5=5*(x//5)
        r5=5*((x+4)//5)
        mini=d*x
        mini=min(mini,abs(l2-x)*d+a+dfs(l2//2))
        mini=min(mini,abs(r2-x)*d+a+dfs(r2//2))
        mini=min(mini,abs(l3-x)*d+b+dfs(l3//3))
        mini=min(mini,abs(r3-x)*d+b+dfs(r3//3))
        mini=min(mini,abs(l5-x)*d+c+dfs(l5//5))
        mini=min(mini,abs(r5-x)*d+c+dfs(r5//5))
        memo[x]=mini
        return mini
    print(dfs(n))