Problem:


Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

Multiply the number by 2, paying A coins.

Multiply the number by 3, paying B coins.

Multiply the number by 5, paying C coins.

Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

1 \le T \le 10

1 \le N \le 10^{18}

1 \le A, B, C, D \le 10^9

All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases.
Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.

Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 1 to multiply by 2 (x = 4).

Pay 2 to multiply by 3 (x = 12).

Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 2 to multiply by 5 (x = 10).

Pay 8 to increase by 1 (x = 11).



Sample input:
5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321


Sample output:
20
19
26
3821859835
23441258666

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
t = int(input())


class Node:
    has, cost = 0, 0
    aDone, bDone, cDone, dDone = False, False, False, False

    def __init__(self, has, cost):
        self.has, self.cost = has, cost



class Calculator:
    n, a, b, c, d = 0, 0, 0, 0, 0
    result = -1

    def __init__(self, n, a, b, c, d):
        self.n, self.a, self.b, self.c, self.d = n, a, b, c, d

    def calc(self):
        # 最初は1増やす
        cost = d
        has = 1
        self.calcCostRecurse2(has, cost)
        print(self.result)


    def calcCostRecurse(self, has, curCost):
        if n < has:
            moreCost = (has - n) * d  # 減らす
            return curCost + moreCost

        if n == has:
            return curCost
        
        costCaseA = self.calcCostRecurse(has * 2, curCost + a)

        costCaseB = self.calcCostRecurse(has * 3, curCost + b)

        costCaseC = self.calcCostRecurse(has * 5, curCost + c)

        costCaseD = self.calcCostRecurse(has + 1, curCost + d)

        return min([costCaseA, costCaseB, costCaseC, costCaseD])

    def calcCostRecurse2(self, has, curCost):
        root = Node(has, curCost) 
        stack = [root]
        while root.aDone == False or root.bDone == False or root.cDone == False or root.dDone == False:
            curNode = stack[-1]

            if curNode.has == n:
                self.result = curNode.cost if self.result < 0 else min([self.result, curNode.cost])
                stack.pop()
                continue

            if curNode.has > n:
                moreCost = (curNode.has - n) * d
                self.result = curNode.cost + moreCost if self.result < 0 else min([self.result, (curNode.cost + moreCost)])
                stack.pop()
                continue

            if curNode.aDone == False:
                newNode = Node(curNode.has * 2, curNode.cost + a)
                stack.append(newNode)
                curNode.aDone = True
                continue

            if curNode.bDone == False:
                newNode = Node(curNode.has * 3, curNode.cost + b)
                stack.append(newNode)
                curNode.bDone = True
                continue
            
            if curNode.cDone == False:
                newNode = Node(curNode.has * 5, curNode.cost + c)
                stack.append(newNode)
                curNode.cDone = True
                continue

            if curNode.dDone == False:
                newNode = Node(curNode.has + 1, curNode.cost + d)
                stack.append(newNode)
                curNode.dDone = True
                continue

            stack.pop()


for i in range(0, t):
    line = input().split(" ")
    n, a, b, c, d = int(line[0]), int(line[1]), int(line[2]), int(line[3]), int(line[4])
    calc = Calculator(n, a, b, c, d)
    calc.calc()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の呼び出し制限


import numpy as np
from collections import deque

# T = int(input())
N, A, B, C, D = map(int, input().split())

rate_list = [2, 3, 5, 1, -1]
num_list = [A, B, C, D, D]

Submission status: Runtime Error

Solution code:
T=int(input())
N =[]

for i in range(T):
  x=0
  N = map(int,input().split())
  x += N[3]
  min = min(N)
if N[0] == 2:
	print(x + N[1])  
elif N[0] == 3:
      print(x + N[2])
elif N[0] == 5:
      print(x + N[3])
elif N[0] % min == 0:
      print(x + (min * ( N[0] / min)))
elif N[0] % min == 1:
      print(x + (min * ( N[0] / min) + N[3]))

Submission status: Runtime Error

Solution code:

t=int(input())

a = [input().split() for i in range(t)]

x=0
n=0

print(type(a))

for i in range(t):
	x+=1
	print(a[i][4])
	n+=a[i][4]
	b1=int([1/a[i][1],2/a[i][2],4/a[i][3],1/a[i][4]])
	b2=int([x/a[i][1],2*x/a[i][2],4*x/a[i][3]])

	if min(b1)==b1[3]:
		x+=1
	else:
		x+=min(b1)*a[i][b1.index(min(b1))]

	print(x)

	while x<a[i][0]:
		print("すぎた")

Submission status: Runtime Error

Solution code:
T=int(input())

def agc_min(n):
	if n==0:
		return 0
	elif n==1:
		return a
	else:
		if n%30==0:
			return min(a+agc_min(n-1),b+agc_min(n/2),c+agc_min(n/3),d+agc_min(n/5))
		elif n%15==0:
			return min(a+agc_min(n+1),a+agc_min(n-1),c+agc_min(n/3),d+agc_min(n/5))
		elif n%10==0:
			return min(a+agc_min(n+1),a+agc_min(n-1),b+agc_min(n/2),d+agc_min(n/5))
		elif n%6==0:
			return min(a+agc_min(n+1),a+agc_min(n-1),b+agc_min(n/2),c+agc_min(n/3))
		elif n%5==0:
			return min(a+agc_min(n+1),a+agc_min(n-1),d+agc_min(n/5))
		elif n%3==0:
			return min(a+agc_min(n+1),a+agc_min(n-1),c+agc_min(n/3))
		elif n%2==0:
			return min(a+agc_min(n+1),a+agc_min(n-1),b+agc_min(n/2))
		else:
			return min(a+agc_min(n+1),a+agc_min(n-1))
	

def agc(n,a,b,c,d):
	return agc_min(n)
	
for _ in range(T):
	n,a,b,c,d=map(int,input().split())
	
	print(agc(n,a,b,c,d))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[]
ans=1
an=0
for i in range(1,N+1):
  B.append(i)
for i in range(K):
  an=A[ans-1]
  ans=an
print(ans)

Submission status: Runtime Error

Solution code:
T=int(input())
i=0
while i < T:
N,A,B,C,D = map(int, input().split())
i =+ 1

Submission status: Runtime Error

Solution code:
from math import log, floor
t = int(input())
flag = False
mn = int()

def solve(n,a,b,c,d):
    global flag
    global mn

    if n <= 0:
        return
    if (n-c) % 5 == 0:
        mn = (n-c) // 5 ** floor(log(n-c, 5))
        return
    if (n-b) % 3 == 0:
        mn = (n-b) // 3 ** floor(log(n-b, 3))
        return
    if (n-a) % 2 == 0:
        mn = (n-a) // 2 ** floor(log(n-a, 2))
        return
    solve(n+d-1, a, b, c, d)
    solve(n+d+1, a, b, c, d)


for i in range(t):
    mn = 10 ** 18
    t0, t1, t2, t3, t4 = [int(i) for i in input().split()]
    solve(t0,t1,t2,t3,t4)
    print(mn)

Submission status: Runtime Error

Solution code:
t = int(input())
tcs = [list(map(int, input().split())) for _ in range(t)]

def rec(n, a, b, c, d, x, coins, best):
    #print(n, a,b,c,d,x,coins)

    if coins > best:
        # cut branch
        return best + 1
    elif x >= n:
        #print('terminal')
        total_coins = coins + d * (x - n)
        #print('total:',total_coins)
        return total_coins
    else:
        # a
        va = rec(n, a, b, c, d, x * 2, coins + a, best)
        best = min(va, best)
        # b
        vb = rec(n, a, b, c, d, x * 3, coins + b, best)
        best = min(vb, best)
        # c
        vc = rec(n, a, b, c, d, x * 5, coins + c, best)
        best = min(vc, best)
        # d(1)
        vd1 = rec(n, a, b, c, d, x + 1, coins + d, best)
        best = min(vd1, best)
        # d(2)
        #if x > 1:
        #    vd2 = rec(n, a, b, c, d, x - 1, coins + d, best)
        #    best = min(vd2, best)

        return best

for i, tc in enumerate(tcs):
    n, a, b, c, d = tc
    ans = rec(n, a, b, c, d, 1, d, d*n)
    #print("case",i,ans)
    print(ans)

Submission status: Runtime Error

Solution code:
I = input()
x = 0

for N<>x

N/x ==０なら　D1
N＞5xなら　C
N＜5xかつN>=3x なら　B
N>3x かつ　N＞=2x なら　A


if (x = 0)
D1(c, x)
end



def A(c, x)
c =　A＋c
x　= 2*x
return c, x
def B(c, x)
c = B + c
x　= 3*x
return c, x

def C(c, x)
c = C + c
x　= 5*x
return c, x

def D1(c, x)
c = D + c
x　= x+1
return c, x

def D2(c, x)
c = D + c
x　= x-1
return c, x

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)
T = int(input())

def findall(n, count):
    if n == N:
        l.append(count)
        return
    if n > N:
        l.append(count+(n-N)*D)
        return
    findall(n*2, count+A)
    findall(n*3, count+B)
    findall(n*5, count+C)
    findall(n+1, count+D)

for i in range(T):
    N,A,B,C,D = map(int, input().split())
    l = []
    findall(1, D)
    print(min(l))

Submission status: Runtime Error

Solution code:
_

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
T = int(input())

def findall(n, count):
    if n == N:
        l.append(count)
        return
    if n > N:
        l.append(count+(n-N)*D)
        return
    findall(n*2, count+A)
    findall(n*3, count+B)
    findall(n*5, count+C)
    findall(n+1, count+D)

for i in range(T):
    N,A,B,C,D = map(int, input().split())
    l = []
    findall(1, D)
    print(min(l))

Submission status: Runtime Error

Solution code:
"""
A枚のコインを支払い、持っている数を 2倍する。
B枚のコインを支払い、持っている数を 3倍する。
C枚のコインを支払い、持っている数を 5倍する。
D枚のコインを支払い、持っている数を 1増やす、または 1減らす。
持っている数をNとするには、最小で何枚のコインが必要でしょうか。
"""

N, A, B, C, D = map(int, input().split())
arr = [0]*N
arr[0]=D

arr[1]=min(2*D,D+A)
arr[2]=min(3*D,D+B,2*D+A)
arr[3]=min(4*D,D+2*A,2*D+B,2*D+C)
arr[4]=min(5*D,2*D+2*A,A+B+2*D,D+C)

for i in range(5,N,1):
    arr[i] = min(arr[((i+1)//2)+1]+A+D*abs(i+1-(2*((i+1)//2))),
    arr[((i+1)//2)+2]+A+D*abs(i+1-(2*(((i+1)//2))+1)),
    arr[((i+1)//3)+1]+B+D*abs(i+1-(3*((i+1)//3))),
    arr[((i+1)//3)+2]+B+D*abs(i+1-(3*(((i+1)//3))+1)),
    arr[((i+1)//5)+1]+C+D*abs(i+1-(5*((i+1)//5))),
    arr[((i+1)//5)+2]+C+D*abs(i+1-(5*(((i+1)//5))+1))
)

print(arr[-1])

Submission status: Runtime Error

Solution code:
sadfa

Submission status: Runtime Error

Solution code:
def R(x):
    x.insert(0, x.pop(-1))
    return x
    
def S(x, n):
    ex = [0]*n
    for j in range(N//3):
        
        k = j
        tmp = 0
        m = 1
        q = j*3
        while k > 0:
            if k%3 == 1:
                tmp = tmp + 2*(3**m)
            elif k%3 == 2:
                tmp = tmp + 1*(3**m)
            k = k//3
            m = m +1
        ex[q] = x[tmp]
        ex[q + 1] = x[tmp + 2]
        ex[q + 2] = x[tmp + 1]
    return(ex)
            
    
def main():
    N = 3**int(input())
    T = input()
    l = list(range(N))
    p = [0]*N
    
    for i in range(len(T)):
        if T[i] == "R":
            l = R(l)
        else
            l = S(l, N)            
    print(l)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from math import ceil
T = int(input())
for i in range(T):
    N,A,B,C,D = map(int,input().split())
    memo = {0:0,1:D}
    def f(n):
        if n in memo:
            return memo[n]
        else:
            res = D*n
            r2 = n%2
            r3 = n%3
            r5 = n%5
            res = min(res,f(n//2)+A+r2*D,f(n//3)+B+r3*D,f(n//5)+C+r5*D)
            res = min(res,f(n//2+1)+A+(2+r2)*D,f(n//3+1)+B+(3+r3)*D,f(n//5+1)+C+(5+r5)*D)
            res = min(res,f(ceil(n/2))+A+(2-r2)*D,f(ceil(n/3))+B+(3-r3)*D,f(ceil(n/5))+C+(5-r5)*D)
            memo[n] = res
            return res
    print(f(N))

Submission status: Runtime Error

Solution code:
import numpy as np
import math
t = int(input())
in_de = np.array([2, 3, 5])

for _ in range(t):
    n, a, b, c, d = map(int, input().split())
    r = max(a, b, c, d) * n
    for i in range(int(math.log(n, 5)) + 1, -1, -1):
        ans = 5 ** i
        for j in range(int(math.log((abs(n - ans)), 3)), -1, -1):
            ans = 5 ** i + 3 ** j
            ans += int(abs(n - ans) ** 0.5) ** 2
            r = min(d * abs(n - ans), r)
    print(r)

Submission status: Runtime Error

Solution code:
t = int(input())
a, b, c, d = map(int,input().split())

Submission status: Runtime Error

Solution code:
t = int(input())
l =list([map(int,input().split()) for _ in range(t)])
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
for j in range(t):
    a = prime_factorize(l[j][0])
    ans = 0
    for i in range(len(a)) :
        if a[i]==2 :
            ans += l[j][1]
        elif a[i]==3:
            ans += l[j][2]
        elif a[i]==5:
            ans += l[j][3]
        else :
            exit()
     ans += (n-(2**a.count(2))*(3**a.count(3))*(5**a.count(5)))*(l[j][4]+1)
     print(ans)

Submission status: Runtime Error

Solution code:
T = int(input())
N, A, B, C, D = list(map(int, input().split()))
print(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

def dp(n):
    if n==1:return D
    if n in memo:
        return memo[n]
    memo[n] = dp(n-1)+D
    if n%2==0:
        memo[n] = min(memo[n],dp(n//2)+A)
    else:
        memo[n] = min(memo[n],dp((n+1)//2)+A+D)
    if n%3==0:
        memo[n] = min(memo[n],dp(n//3)+B)
    elif n%3==1:
        memo[n] = min(memo[n],dp((n+2)//3)+B+2*D)
    elif n%3==2:
        memo[n] = min(memo[n],dp((n+1)//3)+B+D)
    if n%5==0:
        memo[n] = min(memo[n],dp(n//5)+C)
    elif n%5==1:
        memo[n] = min(memo[n],dp((n+4)//5)+C+4*D)
    elif n%5==2:
        memo[n] = min(memo[n],dp((n+3)//5)+C+3*D)
    elif n%5==3:
        memo[n] = min(memo[n],dp((n+2)//5)+C+2*D)
    elif n%5==4:
        memo[n] = min(memo[n],dp((n+1)//5)+C+D)
    return memo[n]
T = int(input())
for _ in range(T):
    memo = {}
    N,A,B,C,D = map(int,input().split())
    print(dp(N))

Submission status: Runtime Error

Solution code:
t = int(input())

n_list = list()
a_list = list()
b_list = list()
c_list = list()
d_list = list()

for x in range(t):
    n, a, b, c, d = map(int, input().split())
    n_list.append(n)
    a_list.append(a)
    b_list.append(b)
    c_list.append(c)
    d_list.append(d)

for x in range(t):
    # 初期化
    ans = 0
    num = n_list[x]

    first = min()

    while True:

        # 終了条件
        if num == 1:
            ans += d_list[x]
            break

        if num % 5 == 0:
            ans += c_list[x]
            num //= 5

        elif num % 2 == 0:
            ans += a_list[x]
            num //= 2

        elif num % 3 == 0:
            ans += b_list[x]
            num //= 3
        else:
            num -= 1
            ans += d_list[x]
    print(ans)

Submission status: Runtime Error

Solution code:
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def check_sosu(a1,a2,a3):
    aee = [a1,a2,a3]
    check = [2 ,3 , 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    for i in range(3,len(check)):
        aa1 = factorization(check[i]+1)
        aa2 = factorization(check[i]-1)
        dd1 = 0
        dd2 = 0
        for j in range(len(aa1)):
            bb1 = check.index(aa1[j][0])
            dd1 += aee[bb1] * aa1[j][1]
        for j in range(len(aa2)):
            bb2 = check.index(aa2[j][0])
            dd2 += aee[bb2] * aa2[j][1]
        aee.append(min(dd1,dd2))
    
    return aee

def n_(input_,nn):
    aa_ = 0
    oo = 1
    while True:
        if input_ <= oo:
             break
        oo *= nn
        aa_ += 1
    return aa_

t = int(input())

cc = [2 ,3 , 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

for i in range(t):
    ans = 0
    n,a,b,c,d = map(int,input().split())
    cost = check_sosu(a,b,c)
    ans += d
    check_ = 0
    h1 = 0
    h2 = 0
    h3 = 0
    if n % 2 == 0:
        e = factorization(n)
        for j in range(len(e)):
            b1 = cost.index(e[j][0])
            ans += cost[b1] * e[j][1]
    else:
        ans += d
        e1 = factorization(n+1)
        e2 = factorization(n-1)
        d1 = 0
        d2 = 0
        for j in range(len(e1)):
            b1 = cc.index(e1[j][0])
            d1 += cost[b1] * e1[j][1]
        for j in range(len(e2)):
            b2 = cc.index(e2[j][0])
            d2 += cost[b2] * e2[j][1]
        ans += min(d1,d2)
    if a > d:
        g1 = a // d
        h1 = n_(g1,2)
        check_ = 1
    if b > d:
        g2 = b // d
        h2 = n_(g2,3)
        check_ = 1
    if c > d:
        g3 = c // d
        h3 = n_(g3,5)
        check_ = 1
    if check_ != 0:
        ans -= a * h1
        ans -= b * h2
        ans -= c * h3
        ans += d * (2*h1 + 3*h2 + 5*h3)
    print(ans)

Submission status: Runtime Error

Solution code:
r

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

memo = {}
def dp(n):
    if n==1:return D
    if n in memo:
        return memo[n]
    memo[n] = dp(n-1)+D
    if n%2==0:
        memo[n] = min(memo[n],dp(n//2)+A)
    else:
        memo[n] = min(memo[n],dp((n+1)//2)+A+D)
    if n%3==0:
        memo[n] = min(memo[n],dp(n//3)+B)
    elif n%3==1:
        memo[n] = min(memo[n],dp((n+2)//3)+B+2*D)
    elif n%3==2:
        memo[n] = min(memo[n],dp((n+1)//3)+B+D)
    if n%5==0:
        memo[n] = min(memo[n],dp(n//5)+C)
    elif n%5==1:
        memo[n] = min(memo[n],dp((n+4)//5)+C+4*D)
    elif n%5==2:
        memo[n] = min(memo[n],dp((n+3)//5)+C+3*D)
    elif n%5==3:
        memo[n] = min(memo[n],dp((n+2)//5)+C+2*D)
    elif n%5==4:
        memo[n] = min(memo[n],dp((n+1)//5)+C+D)
    return memo[n]

N,A,B,C,D = map(int,input().split())
print(dp(N))

Submission status: Runtime Error

Solution code:
def get(target,cost_one,cost_two,cost_three,cost_five):
  dp = [float("inf") for _ in range(target+100)]
  if target ==1:
    return cost_one
  if target == 0:
    return 0
  dp[1] = cost_one
  dp[2] = min(dp[1]*2,dp[1] + cost_two,dp[1] + cost_three + cost_one,dp[1] + cost_five + 3*cost_one)
  for i in range(3,target + 101):
    minima = float("inf")
    minima = min(minima, dp[1]*i)
    if i%2 == 0:
      minima = min(minima,dp[i//2] + cost_two)
    if i%3 == 0:
      minima = min(minima,dp[i//3] + cost_three)
    if i%5 == 0:
      minima = min(minima,dp[i//5] + cost_five)
    dp[i] = min(minima,dp[i-1] + cost_one)
  for j in range(target+1,target+101):
    dp[target] = min(dp[target],dp[j] + (j-target)*cost_one)
  return dp[target]

for _ in range(int(input())):
  target,cost_two,cost_three,cost_five,cost_one  =list(map(int,input().split()))
  print(get(target,cost_one,cost_two,cost_three,cost_five))

Submission status: Runtime Error

Solution code:
T=int(input())
N,A,B,C,D=[],[],[],[],[]
for i in range(T):
    input_line=input().split()
    N.append(input_line[0])
    A.append(input_line[1])
    B.append(input_line[2])
    C.append(input_line[3])
    D.append(input_line[4])
num=0

ans=ans+D[0]

Submission status: Runtime Error

Solution code:
'''
T = 5
N = 11
A = 1#*2
B = 2#*3
C = 4#*5
D = 8#+1
'''


T = int(input())
for _ in range(T):
	N, A, B, C, D = map(int, input().split())
	#[A, B, C, D]
	memo = [0, D]#{1: D}
	for i in range(2, N+2):
		cost = memo[i-1] + D
		if not i%2:
			cost = min(cost, memo[i/2] + A)
		if not i%3:
			cost = min(cost, memo[i/3] + B)
		if not i%5:
			cost = min(cost, memo[i/5] + C)
		#memo[i] = cost
		memo.append(cost)
		memo[i-1] = min(memo[i-1], cost + D)
		#print(str(i)+' >>> '+str(cost))
	print(memo[N])
print()

Submission status: Runtime Error

Solution code:
aaa

Submission status: Runtime Error

Solution code:
T=input()
N,A,B,C,D=input().split()
N_t=0
coin=0
while(N_t=N):

Submission status: Runtime Error

Solution code:
import math
def closest(div, goal):
  div1 = goal - goal % div
  div2 = goal + (div - goal % div)
  if div1 == div2:
   return div1, div1+div, div1-div
  else:
    return div1, div2, div2
def process(closest, multi, highCP):
  close = closest/multi
  log = math.log(highCP, close)
  if log % 1 ==0:
    return math.log(highCP, close)
  else:
    return False
T = int(input())
li = []
for _ in range(T):
  N, A, B, C, D = map(int, input().split())
  cost = [999999999999999999]
  if 15*A <= 10*B:
    if 15*A <= 6*C:
      highCP = 2
      hcp = A
    else:
      highCP = 5
      hcp = C
  elif 10*B <= 6*D:
    highCP = 3
    hcp = B
  else:
    highCP = 5
    hcp = C
  for multi in (2, 3, 5):
    if multi == 2:
      last = A
    elif multi == 3:
      last = B
    else:
      last = C
    cadi1, cadi2, cadi3 = closest(multi, N)
    for cadi in (cadi1, cadi2, cadi3):
      hcpt = process(cadi, multi, highCP)
      if hcpt:
        cost.append(D + hcpt*hcp + last + abs(N - cadi)*D)
  li.append(min(cost))
for i in range(T):
  print(li[i])

Submission status: Runtime Error

Solution code:
def a_job(n_plus, a, c_plus):
  c_plus += a
  n_plus *= 2
def b_job(n_plus, b, c_plus):
  c_plus += b
  n_plus *= 3
def c_job(n_plus, c, c_plus):
  c_plus += c
  n_plus *= 5
def d_job(n, n_plus, d, c_plus):
  c_plus += d
  if n_plus < n:
    n_plus += 1
  elif n_plus > n:
    n_plus -= 1
  else:
    pass
def nc(n, a, b, c, d):
  n_plus = 0
  c_plus = 0
  d_job(n, n_plus, d, c_plus)
  while true:
    if n_plus * 5 <= n:
      c_job(n_plus, a, c_plus)
    elif n_plus * 3 <= n:
      b_job(n_plus, a, c_plus)
    elif n_plus * 2 <= n:
      a_job(n_plus, a, c_plus)
    else:
      d_job(n, n_plus, a, c_plus)
    if n_plus == n:
      put(c_plus)
      break
    elif n_plus > n:
      c_plus = 0
      n_plus = 0
    else:
      pass
nc(n, a, b, c, d)

Submission status: Runtime Error

Solution code:
from collections import deque
t=int(input())
for i in range(t):
    n,a,b,c,d=map(int,input().split())
    lis=deque([[1,d]])
    ans=[0,1]
    anssum=[]
    while lis:
        x=lis.popleft()
        if x[0]!=0 and x[0]<n:
            if x[0]*2==n:
                anssum.append(x[1]+a)
            else:
                if x[0]*2 not in ans:
                    lis.append([x[0]*2,x[1]+a])
                    ans.append(x[0]*2)
            if x[0]*3==n:
                anssum.append(x[1]+b)
            else:
                if x[0]*3 not in ans:
                    lis.append([x[0]*3,x[1]+b])
                    ans.append(x[0]*3)
            if x[0]*5==n:
                anssum.append(x[1]+c)
            else:
                if x[0]*5 not in ans:
                    lis.append([x[0]*5,x[1]+c])
                    ans.append(x[0]*5)
            if x[0]+1==n or x[0]-1==n:
                anssum.append(x[1]+d)
            else:
                if x[0]+1 not in ans:
                    lis.append([x[0]+1,x[1]+d])
                    ans.append(x[0]+1)
                if x[0]-1 not in ans:
                    lis.append([x[0]-1,x[1]+d])
                    ans.append(x[0]-1)
        else:
            if x[0]-1==n:
                anssum.append(x[1]+d)
            else:
                if x[0]-1 not in ans:
                    lis.append([x[0]-1,x[1]+d])
                    ans.append(x[0]-1)
    print(min(anssum))

Submission status: Runtime Error

Solution code:
T = int(input())
query = [0]*T
for i in range(T):
    query[i] = list(map(int,input().split()))
# print(query)
# A枚のコインを支払い、持っている数を 2倍する。 
# B枚のコインを支払い、持っている数を3倍する。
# C枚のコインを支払い、持っている数を 5倍する。
# D枚のコインを支払い、持っている数を 1増やす、
# または1減らす。
# 11 1 2 4 8
# N  A B C D
for i in range(T):
    A = query[i][1]*2.5
    B = query[i][2]/3*5
    C = query[i][3]/5 
    print(A,B,C)
    pay = min(A,(min(B,C)))
    N = query[i][0]
    while all:
        if(N%2 != 0 and N%3 != 0 and N%5 != 0):
            if(N-1 % ==0):
                

    # have_num = 0
    # coin = 0
    # coin += query[i][4]
    # have_num += 1

Submission status: Runtime Error

Solution code:
t = int(input())
for i in range(t):
    n,a,b,c,d = map(int,u[i])
    print(d*n)

Submission status: Runtime Error

Solution code:
t=int(input())
 
# for loop 
 
for i in range(t):
    n,a,b,c,d=map(int,input().split())
n,A,B,C=map(int,input().split())
 
# new vars
 
 
l=[int(input()) for _ in range(n)]
 
def dfs(count,a,b,c):
  
  # if-else looping 
    if count==n:
        if min(a,b,c)>0:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:
            return 10**9+1
          
          # new vars
          
    x = dfs(count+1, a, b, c)
    y = dfs(count+1, a+l[count], b, c)+10
    z = dfs(count+1, a, b+l[count], c)+10
    w = dfs(count+1, a, b, c+l[count])+10
    return min(x,y,z,w)
 
# printing answer 
 
print(dfs(0,0,0,0))

Submission status: Runtime Error

Solution code:
T = int(input())
n,a,b,c = list(map(int,input().split()))
x = 0
coin = 0
if n % 2 == 0:
  x +=1
  coin = coin +d
  while (n == 1):
    if n%2 == 0:
      n = n//2
      x = x*2
      coin = coin + a
    elif n%3 ==0:
      n = n//3
      x = x*3
      coin = coin +b
    else:
      n = n//5
      x = x*5
      coin = coin +c
  print(int(coin))
if n % 2 != 0:
  x += 1
  coin = coin +d
  n = n - 1
  while (n == 1):
    if n%2 == 0:
      n = n//2
      x = x*2
      coin = coin + a
    elif n%3 ==0:
      n = n//3
      x = x*3
      coin = coin +b
    else:
      n = n//5
      x = x*5
      coin = coin +c
  coin1 = coin + d
  n = x + 1
  coin = d
  n = n + 1
  while (n == 1):
    if n%2 == 0:
      n = n//2
      x = x*2
      coin = coin + a
    elif n%3 ==0:
      n = n//3
      x = x*3
      coin = coin +b
    else:
      n = n//5
      x = x*5
      coin = coin +c
  coin2 = coin +d
  print(coin2 if coin2 < coin1 else coin1)

Submission status: Runtime Error

Solution code:
t = int(input())

for i in range(t):
  n_target, a, b, c, d = map(int, input().split())
  nc_list = [1, d]
  coin_min = 10**20
  while(1):
    nc_list = [nc_list[k] for k in range(len(nc_list)) if i not in nc_list_dlt]
    nc_list.extend(nc_list_add)
    if len(nc_list) == 0:
      break
    nc_list_add = []
    nc_list_dlt = []
    for j, n in enumerate(nc_list):
      if n[1] >= coin_min:
        nc_list_dlt.extend(j)
        continue
      else:
        nc_list_now = [[n*2, a],[n*3, b],[n*5, c],[n+1, d],[n-1, d]]
        finish_list = [x for x in nc_list_now if x[0] == n_target]
        if finish_list != 0:
          coin_num = [x[1] for x in finish_list]
          coin_min = min(coin_min, min(coin_num))
          nc_list_now = [x for x in nc_list_now if x not in finish_list]
      nc_list_add = [nc_list_now]
print(coin_min)

Submission status: Runtime Error

Solution code:
import math
def dfs(i, j):
    global ans
    if i == n:
        ans = min(ans, j)
    elif i>n+5 or i==0:
      i,j=n,float("inf")
    elif i>n:
      dfs(i-1,j+d)

    else:
        dfs(i*2,j+a)
        dfs(i*3,j+b)
        dfs(i*5,j+c)
        dfs(i+1,j+d)

t = int(input())
q = [list(map(int,input().split())) for i in range(t)]
ans = float("inf")

for i in range(t):
  n,a,b,c,d=q[i]
  if n*d <= (int(math.log(n,2))*a and int(math.log(n,3))*b and int(math.log(n,5))*c):
    print(n*d)
  elif int(math.log(n,5))*c <= (int(math.log(n,2))*a and int(math.log(n,3))*b):
    dfs(5**(int(math.log(n,5))-1),d+(int(math.log(n,5))-1)*c)
    print(ans)
  elif int(math.log(n,3))*b >int(math.log(n,2))*a:
    dfs(3**(int(math.log(n,5))-1),d+(int(math.log(n,3))-1)*c)
    print(ans)
  else :
    dfs(2**(int(math.log(n,2))-1),d+(int(math.log(n,2))-1)*c)
    print(ans)

Submission status: Runtime Error

Solution code:
'''
T = 5
N = 11
A = 1#*2
B = 2#*3
C = 4#*5
D = 8#+1
'''


T = int(input())
for _ in range(T):
	A, B, C, D = map(int, input().split())
	#[A, B, C, D]
	memo = {1: D}
	for i in range(2, N+2):
		cost = memo[i-1] + D
		if not i%2:
			cost = min(cost, memo[i/2] + A)
		if not i%3:
			cost = min(cost, memo[i/3] + B)
		if not i%5:
			cost = min(cost, memo[i/5] + C)
		memo[i] = cost
		memo[i-1] = min(memo[i-1], cost + D)
		#print(str(i)+' >>> '+str(cost))
	print(memo[N])
print()

Submission status: Runtime Error

Solution code:
t = int(input())
	for i in range(t):
        n, a, b, c, d = list(map(int, input().split()))

Submission status: Runtime Error

Solution code:
T = int(input())
n,a,b,c = map(int,input().split())
x = 0
coin = 0
if n % 2 == 0:
  x +=1
  coin = coin +d
  while (n == 1):
    if n%2 == 0:
      n = n//2
      x = x*2
      coin = coin + a
    elif n%3 ==0:
      n = n//3
      x = x*3
      coin = coin +b
    else:
      n = n//5
      x = x*5
      coin = coin +c
  print(int(coin))
if n % 2 != 0:
  x += 1
  coin = coin +d
  n = n - 1
  while (n == 1):
    if n%2 == 0:
      n = n//2
      x = x*2
      coin = coin + a
    elif n%3 ==0:
      n = n//3
      x = x*3
      coin = coin +b
    else:
      n = n//5
      x = x*5
      coin = coin +c
  coin1 = coin + d
  n = x + 1
  coin = d
  n = n + 1
  while (n == 1):
    if n%2 == 0:
      n = n//2
      x = x*2
      coin = coin + a
    elif n%3 ==0:
      n = n//3
      x = x*3
      coin = coin +b
    else:
      n = n//5
      x = x*5
      coin = coin +c
  coin2 = coin +d
  print(coin2 if coin2 < coin1 else coin1)

Submission status: Runtime Error

Solution code:
'''
T = 5
N = 11
A = 1#*2
B = 2#*3
C = 4#*5
D = 8#+1
'''
T = int(input())
S = [input() for _ in range(T)]


for _ in range(T):
	A, B, C, D = map(int, input().split())
	#[A, B, C, D]
	memo = {1: D}
	for i in range(2, N+2):
		cost = memo[i-1] + D
		if not i%2:
			cost = min(cost, memo[i/2] + A)
		if not i%3:
			cost = min(cost, memo[i/3] + B)
		if not i%5:
			cost = min(cost, memo[i/5] + C)
		memo[i] = cost
		memo[i-1] = min(memo[i-1], cost + D)
		#print(str(i)+' >>> '+str(cost))
	print(memo[N])
print()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

T = int(input())

qs = list()

def dfs(n, a, b, c, d):
    global ans
    if n == 0:
        ans = 1
        return True

    if a > 0 and n % 2 == 0 and dfs(n // 2, a - 1, b , c, d):
        ans += 1
        return True
    if b > 0 and n % 3 == 0 and dfs(n // 3, a, b - 1, c, d):
        ans += 1
        return True
    if c > 0 and n % 5 == 0 and dfs(n // 5, a, b, c - 1, d):
        ans += 1
        return True
    if d > 0 and dfs(n - 1, a, b, c, d - 1):
        ans += 1
        return True
    if d > 0 and dfs(n + 1, a, b, c, d - 1):
        ans += 1
        return True

    return False

for i in range(T):
    qs.append(map(int, input().split()))

for q in qs:
    ans = 0
    dfs(*q)
    print(ans)