Problem:


Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16



Sample input:
127


Sample output:
4

Submission status: Wrong Answer

Solution code:
N = int(input())

six1 = 6        #     6
nin1 = 9        #     9
six2 = six1 * 6 #    36
nin2 = nin1 * 9 #    81
six3 = six2 * 6 #   216
nin3 = nin2 * 9 #   729
six4 = six3 * 6 #  1296
nin4 = nin3 * 9 #  6561
six5 = six4 * 6 #  7776
six6 = six5 * 6 # 46656
nin5 = nin4 * 9 # 59049

def calc(n):
	if n < six1:
		#0 - 5
		return n
	elif n < nin1:
		#6 - 8
		n_six1 = int(n / six1)
		a_six1 = n % six1
		c_six1 = calc(a_six1)
		return(min(n, n_six1+c_six1))
	elif n < six2:
		#9 - 35
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		a_six1 = n % six1
		a_nin1 = n % nin1
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		return(min(n, n_six1+c_six1, n_nin1+c_nin1))
	elif n < nin2:
		#36 - 80
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_nin1+c_nin1))
	elif n < six3:
		#81 - 215
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_nin1+c_nin1, n_nin2+c_nin2))
	elif n < nin3:
		#216 - 728
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_nin1+c_nin1, n_nin2+c_nin2))
	elif n < six4:
		#729 - 1295
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		n_nin3 = int(n / nin3)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		a_nin3 = n % nin3
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		c_nin3 = calc(a_nin3)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_nin1+c_nin1, n_nin2+c_nin2, n_nin3+c_nin3))
	elif n < nin4:
		#1296 - 6560
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		n_nin3 = int(n / nin3)
		n_six4 = int(n / six4)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		a_nin3 = n % nin3
		a_six4 = n % six4
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		c_nin3 = calc(a_nin3)
		c_six4 = calc(a_six4)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_six4+c_six4, n_nin1+c_nin1, n_nin2+c_nin2, n_nin3+c_nin3))
	elif n < six5:
		#6561 - 7775
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		n_nin3 = int(n / nin3)
		n_six4 = int(n / six4)
		n_nin4 = int(n / nin4)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		a_nin3 = n % nin3
		a_six4 = n % six4
		a_nin4 = n % nin4
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		c_nin3 = calc(a_nin3)
		c_six4 = calc(a_six4)
		c_nin4 = calc(a_nin4)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_six4+c_six4, n_nin1+c_nin1, n_nin2+c_nin2, n_nin3+c_nin3, n_nin4+c_nin4))
	elif n < six6:
		#7776 - 46655
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		n_nin3 = int(n / nin3)
		n_six4 = int(n / six4)
		n_six5 = int(n / six5)
		n_nin4 = int(n / nin4)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		a_nin3 = n % nin3
		a_six4 = n % six4
		a_six5 = n % six5
		a_nin4 = n % nin4
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		c_nin3 = calc(a_nin3)
		c_six4 = calc(a_six4)
		c_six5 = calc(a_six5)
		c_nin4 = calc(a_nin4)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_six4+c_six4, n_six5+c_six5, n_nin1+c_nin1, n_nin2+c_nin2, n_nin3+c_nin3, n_nin4+c_nin4))
	elif n < nin5:
		#46656 - 59048
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		n_nin3 = int(n / nin3)
		n_six4 = int(n / six4)
		n_six5 = int(n / six5)
		n_nin4 = int(n / nin4)
		n_six6 = int(n / six6)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		a_nin3 = n % nin3
		a_six4 = n % six4
		a_six5 = n % six5
		a_nin4 = n % nin4
		a_six6 = n % six6
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		c_nin3 = calc(a_nin3)
		c_six4 = calc(a_six4)
		c_six5 = calc(a_six5)
		c_nin4 = calc(a_nin4)
		c_six6 = calc(a_six6)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_six4+c_six4, n_six5+c_six5, n_six6+c_six6, n_nin1+c_nin1, n_nin2+c_nin2, n_nin3+c_nin3, n_nin4+c_nin4))
	else:
		#59049-
		n_six1 = int(n / six1)
		n_nin1 = int(n / nin1)
		n_six2 = int(n / six2)
		n_nin2 = int(n / nin2)
		n_six3 = int(n / six3)
		n_nin3 = int(n / nin3)
		n_six4 = int(n / six4)
		n_six5 = int(n / six5)
		n_nin4 = int(n / nin4)
		n_six6 = int(n / six6)
		n_nin5 = int(n / nin5)
		a_six1 = n % six1
		a_nin1 = n % nin1
		a_six2 = n % six2
		a_nin2 = n % nin2
		a_six3 = n % six3
		a_nin3 = n % nin3
		a_six4 = n % six4
		a_six5 = n % six5
		a_nin4 = n % nin4
		a_six6 = n % six6
		a_nin5 = n % nin5
		c_six1 = calc(a_six1)
		c_nin1 = calc(a_nin1)
		c_six2 = calc(a_six2)
		c_nin2 = calc(a_nin2)
		c_six3 = calc(a_six3)
		c_nin3 = calc(a_nin3)
		c_six4 = calc(a_six4)
		c_six5 = calc(a_six5)
		c_nin4 = calc(a_nin4)
		c_six6 = calc(a_six6)
		c_nin5 = calc(a_nin5)
		return(min(n, n_six1+c_six1, n_six2+c_six2, n_six3+c_six3, n_six4+c_six4, n_six5+c_six5, n_six6+c_six6, n_nin1+c_nin1, n_nin2+c_nin2, n_nin3+c_nin3, n_nin4+c_nin4, n_nin5+c_nin5))

print(calc(N))

Submission status: Wrong Answer

Solution code:
N = int(input())


def solve():
    global N
    ans = 0
    a = []
    values = sorted([1] + [pow(6, _) for _ in range(1, 10)] + [pow(9, _) for _ in range(1, 10)])
    while N > 0:
        if 12 <= N <= 14:
            ans += 2 + (N - 12)
            break
        if 30 <= N <= 32:
            ans += 5 + (N - 30)
            break
        else:
            for i in range(len(values)):
                if N < values[i]:
                    ans += 1
                    a.append((values[i - 1], N))
                    N -= values[i-1]
                    break
                elif N == values[i]:
                    ans += 1
                    a.append((values[i], N))
                    N -= values[i]
                    break
            if N == 0:
                break
    return ans

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
import math
n=int(raw_input())
a=[10**6]*(n+1)
a[0]=0
for i in xrange(1,min(6,n)):
    a[i]=i
for i in xrange(6,n+1):
    fac9=int(math.log(i,9))
    fac6=int(math.log(i,6))
    if 9**fac9==i or 6**fac6==i:
        a[i]=1
    else:
        while(fac9>=0):
            a[i]=min(a[i],1+a[i-9**fac9])
            fac9-=1
        while(fac6>=0):
            a[i]=min(a[i],1+a[i-6**fac6])
            fac6-=1

print a[n]

Submission status: Wrong Answer

Solution code:
n = int(input())

nine = []
six = []


for i in range(10):
	nine.append(9**i)
	six.append(6**i)



nine.extend(six)
nine.sort()

count = 0
while n > 0:
	if n < 6 : 
		count += n
		n = 0
	elif n == 6:
		count +=1
		n = 0 
	else :
		i = 0
		while nine[i+1] <= n and (n-nine[i+1] > 5 ) :
			i += 1
		
		count += 1
		n = n - nine[i]

		for k in range(2,10):
			if n%nine[k] == 0:
				count += n/nine[k] 
				n = 0
				break
		
		
print count

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
memo = [0]*(N+1)
memo[0] = 0

for i in range(1,N+1):
	memo[i] = memo[i-1]+1
	for j in range(1,math.floor(math.log(i,6))+1):
		memo[i] = min(memo[i],memo[i-pow(6,j)]+1)
	for j in range(1,math.floor(math.log(i,9))+1):
		memo[i] = min(memo[i],memo[i-pow(9,j)]+1)

print(memo[-1])

Submission status: Wrong Answer

Solution code:
N = int(input())
record = {0:0}
def max_6(x):
	i = 1
	while x >= i:
		i *= 6
	return i//6

def max_9(x):
	i = 1
	while x >= i:
		i *= 9
	return i//9

def count_money(x, c=0):
	if x in record.keys():
		return record[x] + c
	else:
		j = max_9(x)
		i = max_6(x)
		if i < j:
			b = count_money(x-j,c+1)
			a = count_money(x-i,c+1)
		else:
			b = count_money(x-i,c+1)
			a = count_money(x-j,c+1)
		if a > b:
			ans = b
		else:
			ans = a
		record[x] = ans
		return ans

print(count_money(N))

Submission status: Wrong Answer

Solution code:
N = int(input())


def hoge(kind, target):
    i = 0
    while kind**i <= target:
        i += 1
    rest = target - kind**(i - 1)
    return rest


def search(count, target):
    if target <= 5:
        return count + target
    six_rest = hoge(6, target)
    nine_rest = hoge(9, target)
    return min(search(count + 1, six_rest), hoge(count + 1, nine_rest))


print(search(0, N))

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0

        
if N >= 9**5:
    count += int(N/(9**5))
    N = N - int(N/(9**5)) * (9**5)

if N >= 6**6:
    count += int(N/(6**6))
    N = N - int(N/(6**6)) * (6**6)
        

for i in range(4):
    s = 4 - i
    
    if N >= 6**(s+1):
        count += int(N/(6**(s+1)))
        N = N - int(N/(6**(s+1))) * (6**(s+1))
        
    if (N >= 9**s) and (s > 1):
        count += int(N/(9**s))
        N = N - int(N/(9**s)) * (9**s)

        
if (int(N/6)+(N%6)) > (int(N/9)+(N%9)):
    count += int(N/9)
    N = N -  int(N/9)*9



else:
    
    count += int(N/6)
    N = N -  int(N/6)*6
    

print(count+N)

Submission status: Wrong Answer

Solution code:
nine, six = [9 ** i for i in range(1, 6)], [6 ** i for i in range(1, 7)]


def minimize(n, a_idx, b_idx, t):
    if n == 0:
        return t
    elif a_idx == -1 and b_idx == -1:
        return t + n

    t1, t2 = 100000, 100000
    if a_idx != -1:
        p = n // nine[a_idx]
        t1 = minimize(n-p*nine[a_idx], a_idx-1, b_idx, t+p)

    if b_idx != -1:
        q = n // six[b_idx]
        t2 = minimize(n-q*six[b_idx], a_idx, b_idx-1, t+q)

    return min(t1, t2, t+n-1)


def main():
    n = int(input())

    cnt9, cnt6 = 0, 0
    while 9 ** (cnt9+1) <= n:
        cnt9 += 1

    while 6 ** (cnt6+1) <= n:
        cnt6 += 1

    t = minimize(n, cnt9-1, cnt6-1, 0)

    print(t)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=input()
ans=0

while N>=6:
	for i in range(1000):
		t=9**i
		if t>N:
			t1=9**(i-1)
			break

	for i in range(1000):
		t=6**i
		if t>N:
			t2=6**(i-1)
			break
	#print "t1,t2:",t1,t2
	N-=max(t1,t2)
	ans+=1

	print ans,N


print ans+N

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 10**10
for a in range(5):
    for b in range(2):
        for c in range(5):
            for d in range(5):
                for e in range(5):
                    for f in range(5):
                        for g in range(1):
                            for h in range(3):
                                for i in range(7):
                                    for k in range(8):
                                        for l in range(8):
                                            for m in range(1):
                                                if 1*a + 6*b + 36*c + 6**3*d + 6**4*e + 6**5*f + 6**6*g + 9*h + 9**2*i + 9**3*k + 9**4*l + 9**5*m == n:
                                                    ans = min(ans, a+b+c+d+e+f+g+h+i+k+l+m)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
result = 0
L = [1] + [9**i for i in range(1,6)] + [6**i for i in range(1,7)]
L = sorted(L,reverse=True)
for i in L:
    while N >= i:
        N -= i
        result += 1
print(result)

Submission status: Wrong Answer

Solution code:
from math import log
n=int(input())
r=0
while n>6:
    l9=int(log(n,9));l6=int(log(n,6))
    n-=max(9**l9,6**l6);r+=1#;print(l9,l6,max(9**l9,6**l6),n)
r+=n
print(r)

Submission status: Wrong Answer

Solution code:
n = int(input())
nine = [9**i for i in range(6)]
six = [6**i for i in range(7)]
mix = sorted(list(set(nine + six)),reverse=True)
mix_test = [59049,46656, 7776,1296,6561,729,216,36,81,6,9]
mix_ch = [59049,46656,7776,6561,1296,216,729,36,81,6,9]

cnt = n%3
cnt_test = n%3
cnt_ch = n%3
n -= n%3
n_test = n - n%3
n_ch = n - n%3
for money in mix:
    cnt += int(n/money)
    n -= money*int(n/money)
for money in mix_test:
    cnt_test += int(n_test/money)
    n_test -= money*int(n_test/money)
for money in mix_ch:
    cnt_ch += int(n_ch/money)
    n_ch -= money*int(n_ch/money)
if cnt_test==0:
    cnt_test = 999
if cnt_ch == 0:
    cnt_ch = 999
print(min(cnt,cnt_test,cnt_ch))

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
l = [0]

for i in range(1, n+1):
    a = int(math.log(i, 6))
    b = int(math.log(i, 9))
    l += [min([l[i-6**p] for p in range(1, a+1)] + [l[i-9**q] for q in range(1, b+1)] + [l[i-1]]) + 1]

print(l[n])

Submission status: Wrong Answer

Solution code:
count = 0
money = int(input())
while 1:
    if money >= 59049:
        money -= 59049
        count += 1
    elif money >= 46656:
        money -= 46656
        count += 1
    elif money >= 7776:
        money -= 7776
        count += 1
    elif money >= 6561:
        money -= 6561
        count += 1
    elif money >= 1296:
        money -= 1296
        count += 1
    elif money >= 729:
        money -= 729
        count += 1
    elif money >= 216:
        money -= 216
        count += 1
    elif money >= 81:
        money -= 81
        count += 1
    elif money >= 36:
        money -= 36
        count += 1
    elif money >= 9:
        money -= 9
        count += 1
    elif money >= 6:
        money -= 6
        count += 1
    elif money > 0:
        money -= 1
        count += 1
    else:
        print(count)
        break

Submission status: Wrong Answer

Solution code:
#abc99c

from itertools import chain,combinations
def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

n = int(input())

M = []
c = 1
while 1:
    a = 6**c
    b = 9**c
    
    if a <= 100000:
        M.append(a)
    if b <= 100000:
        M.append(b)
    elif a > 100000 and b > 100000:
        break
    c += 1
M.sort(reverse=True)
#print(M)

lst = []
for i in range(len(M)):
    lst.append([])
    
M_master = list(powerset(M))
M_master = M_master[1:]

ans_lst = []
for i in range(len(M_master)):    
    res = n
    ans = 0
    M = M_master[i]
    for m in M:
        ans += int(res/m)
        res = res%m
        #print(ans,res,m)
    ans += res
    ans_lst.append(ans)
    #print("ans",ans)
ans_lst.sort()
print(ans_lst[0])

Submission status: Wrong Answer

Solution code:
N=int(input())
b_list=[]
c_list=[]
for i in range(5):
    b_list.append(9**(i+1))

for i in range(6):
    c_list.append(6**(i+1))

if N<min(c_list):
    for i in range(1,min(c_list)):
        if N==i:
            count=1*i
            print(count)
elif min(c_list)<N<min(b_list):
    N=N-min(c_list)
    for i in range(1,min(b_list)-min(c_list)):
        if N==i:
            count=i+1
            print(count)
elif min(c_list)==N or min(b_list)==N:
    count=1
    print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

counter = 0

cand = set()

cand.add(1)
i = 1
while 6 ** i < N:
    cand.add(6 ** i)
    i += 1

i = 1
while 9 ** i < N:
    cand.add(9 ** i)
    i += 1

c = sorted(cand, reverse=True)

T = [N+1] * (N + 1)
T[0] = 0
for i in range(len(c)):
    for j in range(N - c[i] + 1):
        T [j + c[i]] = min(T[j + c[i]], T[j] + 1)

print(T[N])

Submission status: Wrong Answer

Solution code:
ar = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
n = int(input())
c = 0
ar.reverse()
min = 100000
z = 0
def dfs( m, c):
    global min, z
    z += 1
    if z > 50000:
        return
    if c >= min:
        return
    if m == 0:
        min = c
    for j in range(12):
        if m - ar[j] >= 0:
            if c + 1 >= min:
                return
            dfs(m - ar[j], c + 1)
dfs(n, 0)
print(min)

Submission status: Wrong Answer

Solution code:
def power_of_six(n):
    now = 1
    power = 0
    while now <= n:
        now = now * 6
        power = power + 1
    return power-1

def power_of_nine(n):
    now = 1
    power = 0
    while now <= n:
        now = now * 9
        power = power + 1
    return power-1

def main():
    N = input()
    x = 0
    powsix = 0
    pownine = 0
    while N > 0:
        if N < 15:
            if N<12:
                x = x + N/9 + N%9
                break
            else:
                x = x + N/6 + N%6
                break
        else:
            powsix = power_of_six(N)
            pownine = power_of_nine(N)
            if 6**powsix > 9**pownine:
                N = N - 6**powsix
                x = x + 1
            else:
                N = N - 9**pownine
                x = x + 1
    print(x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
i = 0
j = 0
n = N
while(n > 5):
    while (n-pow(6,i+1) > 0):
        i += 1
        #print("i=",i)
    while (n-pow(9,j+1) > 0):
        j += 1
        #print("j=",j)
    if pow(6,i) < pow(9,j) & pow(9,j)>0:
        n -= pow(9,i)
        #print ('n9=',n)
    elif pow(6,i) > 0 :
        n -= pow(6,i)
        #print ('n6=',n)
    count += 1
    i = 0
    j = 0
print(count + n)

Submission status: Wrong Answer

Solution code:
from math import *
ansarr=[] 
def func1(n,ans):
    
    if(n==0):
        ansarr.append(ans)
    else:
        i=0
        j=0
        while(pow(9,i)<=n):
            i+=1
    
        while(pow(6,j)<=n):
            j+=1

        if(i>0 and j>0):
            k1=n//pow(9,i-1)
            k2=n//pow(6,j-1)
            
            func1(n-(pow(9,i-1)*k1),ans+k1)
            k1-=1

    
            func1(n-(pow(6,j-1)*k2),ans+k2)
            k2-=1

        elif(i>0):
            k2=n//pow(9,i-1)
            func1(n-(pow(9,i-1)*k2),ans+k2)

        elif(j>0):
            k2=n//pow(6,j-1)
        
            func1(n-(pow(6,j-1)*k2),ans+k2)
            k2-=1

n=int(input())
ans=0
func1(n,ans)
#print(ansarr)
print(int(min(ansarr)))

Submission status: Wrong Answer

Solution code:
money = []
N=int(input())
n=9
m=6
while n < 100000:
    money.append (n)
    n = n*9

while m < 100000:
    money.append (m)
    m = m*6

money.sort()
money.reverse()

times = 0

for i in money:
    times += int(N/i)
    N -= int(N/i) * i
    if N<36:
        break

possibility = []
for j in range(0,int(N/9)+1):
    M = N-9*j
    possibility.append(j + int(M/6) + M%6)

print (times + min(possibility))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6]
ans = 0

for i in range(0, 11):
    if n >= a[i]:
        temp = n // a[i]
        print(a[i], temp)
        n -= a[i]*temp
        ans += temp

ans += n
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0

def solve(ans,n):
    sq_nine=1
    sq_six=1
    while n>=9**sq_nine:
        sq_nine+=1
    sq_nine-=1
    while n>=6**sq_six:
        sq_six+=1
    sq_six-=1
    if sq_nine==0 and sq_six==0:
        ans+=n
        return print(ans)
    else:
        n=n-max(9**sq_nine,6**sq_six)
        ans+=1
        print(max(9**sq_nine,6**sq_six),n)
        solve(ans,n)

solve(ans,n)

Submission status: Wrong Answer

Solution code:
nine, six = [9 ** i for i in range(1, 6)], [6 ** i for i in range(1, 7)]


def minimize(n, a_idx, b_idx, t):
    if n == 0:
        return t
    elif a_idx == -1 and b_idx == -1:
        return t + n

    t1, t2 = 100000, 100000
    if a_idx != -1:
        p = n // nine[a_idx]
        t1 = minimize(n-p*nine[a_idx], a_idx-1, b_idx, t+p)

    if b_idx != -1:
        q = n // six[b_idx]
        t2 = minimize(n-q*six[b_idx], a_idx, b_idx-1, t+q)

    return min(t1, t2, t+n)


def main():
    n = int(input())

    cnt9, cnt6 = 0, 0
    while 9 ** (cnt9+1) <= n:
        cnt9 += 1

    while 6 ** (cnt6+1) <= n:
        cnt6 += 1

    t = minimize(n, cnt9-1, cnt6-1, 0)

    print(t)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

m = 100000
a = [6]
b = [9]
ans = 0

while True:
    d = a[-1] * a[0]
    if d > m:
        break
    a.append(a[-1] * a[0])

while True:
    d = b[-1] * b[0]
    if d > m:
        break
    b.append(d)

c = a + b
c.sort()
c.reverse()

def solve(n, i, m):
    if i == len(c):
        return m+n
    return min(solve(n, i+1, m), solve(n%c[i], i+1, m+n//c[i]))

print(solve(n, 0, 0))

Submission status: Wrong Answer

Solution code:
N = int(input())
ArrayCardinals = [59049,46656,7776,6561,1296,729,216,81,36,9,6,1]
Residual = N
NumOperations = 0
for i in range(len(ArrayCardinals)-3):
  CurrentCardinal = ArrayCardinals[i]
  Quotient = Residual//CurrentCardinal
  if(Quotient>0):
    NumOperations += Quotient
    Residual = Residual%CurrentCardinal
Num = Residual
Q9 = Num//9
Residual = Num
Min = Num
if (Q9>0):
    for i in range(Q9+1):
        Residual = Num-(i)*9
        TempMin = (Residual//6)+(Residual%6)+i
        if(Min>TempMin): Min = TempMin
else: Min=(Residual//6)+(Residual%6)
NumOperations = NumOperations+Min
print(NumOperations)

Submission status: Wrong Answer

Solution code:
# num = int(input())
num = 44852

arr = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6]
carr = [0] * 11
count = 0

flag = True
while(flag):
    for i in range(len(arr)):
        if num > arr[i]:
            num = num - arr[i]
            count += 1
            carr[i] += 1
            break
        elif num == arr[i]:
            count += 1
            flag = False
        elif i == len(arr)-1:
            flag = False
count += num
base_count = count
print(count)

Submission status: Wrong Answer

Solution code:
import fractions as f

n = int(input())

count = 0

while n:


    for i in range(1,7):
        if n < 9**i:
            tmp_nine = 9**(i-1)
            break
                



    for i in range(1,8):
        if n < 6**i:
            tmp_six = 6**(i-1)
            break


    if n < 6 :
        count += n
        break
    else:
        n -= max(tmp_nine,tmp_six)
        count +=1
    
print(count)

Submission status: Wrong Answer

Solution code:
num = int(input())

arr = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6]
carr = [0] * 11
count = 0

flag = True
while(flag):
    for i in range(len(arr)):
        if num > arr[i]:
            num = num - arr[i]
            count += 1
            carr[i] += 1
            break
        elif num == arr[i]:
            count += 1
            flag = False
        elif i == len(arr)-1:
            flag = False
count += num
base_count = count
print(count)

Submission status: Wrong Answer

Solution code:
import math
n=int(raw_input())
a=[10**6]*(n+1)
a[0]=0
for i in xrange(1,min(7,n+1)):
    a[i]=i
for i in xrange(7,n+1):
    fac9=int(math.log(i,9))
    fac6=int(math.log(i,6))
    if 9**fac9==i or 6**fac6==i:
        a[i]=1
    else:
        while(fac9>=0):
            a[i]=min(a[i],1+a[i-9**fac9])
            fac9-=1
        while(fac6>=0):
            a[i]=min(a[i],1+a[i-6**fac6])
            fac6-=1

print a[n]

Submission status: Wrong Answer

Solution code:
import math
N =int(input())

count=0
if N < 6:
    print(N)
else:
    while (1):
        n9 = int(math.log(N, 9))
        n6 = int(math.log(N, 6))
        p9 = pow(9, n9)
        p6=pow(6,n6)
        

        
        #print(nn,nj)
        if p9>p6:
            N -= p9
            count+=1
            #print('A')
        else:
            N -= p6
            #print('B')
            count+=1

        #print(N)
        if N < 18 and N > 11:
            if N == 17.0 or N==14.0:
                print(int(4 + count))
                break
            elif N == 16.0 or N==13.0:
                print(int(3 + count))
                break
            elif N == 15.0 or N==12.0:
                print(int(2 + count))
                break
        if N < 6:
            print(int(count + N))
            break

Submission status: Wrong Answer

Solution code:
A = int(input())
def maxize(A,co):
    if A > 0: 
        num = [59049,46656,7776,6561,1296,729,216,81,36,9,6,1]
        for q in range(0,12):
            if A >= num[q]:
                num = num[q:]
                break
        
        B = A - num[0]
    
        co = co + 1
        maxize(B,co)
    else:
        print(co)
        
    return co
 
maxize(A,0)

Submission status: Wrong Answer

Solution code:

N = int(input())


def solve():
    global N
    ans = 0
    a = []
    values = sorted([1] + [pow(6, _) for _ in range(1, 10)] + [pow(9, _) for _ in range(1, 10)])
    while N > 0:
        if 12 <= N <= 14:
            ans += 2 + (N - 12)
            break
        if 30 <= N <= 32:
            ans += 4 + (N - 30)
            break
        else:
            for i in range(len(values)):
                if N < values[i]:
                    ans += 1
                    a.append((values[i - 1], N))
                    N -= values[i-1]
                    break
                elif N == values[i]:
                    ans += 1
                    a.append((values[i], N))
                    N -= values[i]
                    break
            if N == 0:
                break
    return ans


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
ar = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
n = int(input())
c = 0
ar.reverse()
min = 10000
z = 0
def dfs( m, c):
    global min, z
    z += 1
    if z > 10000:
        return
    if c >= min:
        return
    if m == 0:
        min = c
    for j in range(12):
        if m - ar[j] >= 0:
            if c + 1 >= min:
                return
            dfs(m - ar[j], c + 1)
dfs(n, 0)
print(min)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n = int(input())

min_count = 9999


def max_digit(max, cardinal) -> int:
    count = 0
    while True:
        if max < pow(cardinal, count):
            return count-1
        count += 1


def calc_answer(num, sum_count):
    global min_count
    six_max = pow(6, max_digit(num, 6))
    nine_max = pow(9, max_digit(num, 9))
    if num < 6:
        if min_count > sum_count + num:
            min_count = sum_count + num
    elif num % nine_max == num:
        return
    else:
        calc_answer(num % six_max, sum_count + num // six_max)
        calc_answer(num % nine_max, sum_count + num // nine_max)


calc_answer(n, 0)
print(min_count)

Submission status: Wrong Answer

Solution code:
n = int(input())
six_si = 10
nine_si = 10
count = 0
while(True):
    while(n >= max(9**nine_si,6**six_si)):
#        print("nine")
#        print(9**nine_si)
        n -= max(9**nine_si,6**six_si)
        count += 1

    while(n >= min(9**nine_si,6**six_si)):
#        print("six")
#        print(6**six_si)
        n -= min(9**nine_si,6**six_si)
        count += 1
    
    nine_si -= 1
    six_si -= 1
#    print(n)
    if(n < 36):
        break
    
min_sub_count = 100000
min_n = n
for i in range(3, 0, -1):
    sub_count = 0
    sub_n = n
    if(sub_n >= 9*i):
        sub_n -= 9*i
        sub_count += i
#        print(sub_count)
    while(sub_n > 6):
        sub_n -= 6
        sub_count += 1
    if(sub_count <= min_sub_count and sub_n < min_n):
        min_n = sub_n
        min_sub_count = sub_count
        
if(min_sub_count < 10):
    print(count + min_sub_count + min_n)
else:
    print(count + n)

Submission status: Wrong Answer

Solution code:
n = int(input())
list = []

while n > 9**5:
    n = n - 9**5
    list.append(1)

while n > 6**6:
    n = n - 6**6
    list.append(1)


while n > 6**5:
    n = n - 6**5
    list.append(1)


for i in [4,3,2,1]:

    for r in [9,6,]:
        while n > r**i:
            n = n - r**i
            list.append(r**i)



length = len(list) + n
print(length)

Submission status: Wrong Answer

Solution code:
N = int(input())


def solve():
    global N
    ans = 0
    a = []
    values = sorted([1] + [pow(6, _) for _ in range(1, 10)] + [pow(9, _) for _ in range(1, 10)])
    while N > 0:
        if 12 <= N <= 14:
            ans += 2 + (N - 12)
            break
        if 30 <= N <= 32:
            ans += 4 + (N - 30)
            break
        else:
            for i in range(len(values)):
                if N < values[i]:
                    ans += 1
                    a.append((values[i - 1], N))
                    N -= values[i-1]
                    break
                elif N == values[i]:
                    ans += 1
                    a.append((values[i], N))
                    N -= values[i]
                    break
            if N == 0:
                break
    return ans

Submission status: Wrong Answer

Solution code:
ar = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
n = int(input())
c = 0
ar.reverse()
min = 1000
z = 0
def dfs( m, c):
    global min, z
    z += 1
    if z > 1000:
        return
    if c >= min:
        return
    if m == 0:
        min = c
    for j in range(12):
        if m - ar[j] >= 0:
            if c + 1 >= min:
                return
            dfs(m - ar[j], c + 1)
dfs(n, 0)
print(min)

Submission status: Wrong Answer

Solution code:
n = int(input())
nine = [9**i for i in range(6)]
six = [6**i for i in range(7)]
mix = sorted(list(set(nine + six)),reverse=True)
mix_test = [59049,46656, 7776,1296,6561,729,216,36,81,6,9]
cnt = n%3
cnt_test = n%3
n -= n%3
n_test = n - n%3
for money in mix:
    cnt += int(n/money)
    n -= money*int(n/money)
for money in mix_test:
    cnt_test += int(n_test/money)
    n_test -= money*int(n_test/money)
if cnt_test==0:
    cnt_test = 999
print(min(cnt,cnt_test))