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

Solution (Python):
N= int(input())
memo = [-1] * 100001

def Base10toN(X, n):
    if (int(X / n)):
        return Base10toN(int(X / n), n) + str(X % n)
    else:
        return str(X % n)

def f(amount):
    if memo[amount] != -1:
        return memo[amount]
    if amount <= 5:
        memo[amount] = amount
        return amount
    s = Base10toN(amount, 6)
    fs = f(amount - 6 ** (len(s) - 1))
    n = Base10toN(amount, 9)
    fn = f(amount - 9 ** (len(n) - 1))
    if (fs < fn):
        memo[amount] = fs + 1
        return fs + 1
    else:
        memo[amount] = fn + 1
        return fn + 1

print(f(N))

Solution (Python):

x=int(input())

def log(x,y):
  count=0
  while x>=y:
    x/=y
    count+=1
  return count

M=[-1 for x in range (0,100001)]
M[0]=0
def mini(x):
  if M[x]==-1:
    M[x]=1+min(mini(x-9**log(x,9)),mini(x-6**log(x,6)))
  return M[x]
print(mini(x))

Solution (Python):
N=int(input())
ans = 100000
for i in range(N+1):
    t = i
    count = 0
    while t > 0:
        count += t%6
        t = t//6
    t = N - i
    while t > 0:
        count += t%9
        t = t//9
    if count < ans:
        ans = count
print(ans)

Solution (Python):

N = int(input())

ans = 100000000000000
for i in range(N+1):
	tmp_ans = 0
	n6 = i
	while n6 > 0:
		tmp_ans += (n6%6)
		n6 = n6//6
	n9 = N-i
	while n9 > 0:
		tmp_ans += (n9%9)
		n9 = n9//9
	if tmp_ans < ans:
		ans = tmp_ans
		
print(ans)

Solution (Python):
N = int(input())

min_ = N

for i in range(N+1):
  x = 0
  n = i
  while n > 0:
    x += n % 6
    n //= 6  
  n = N-i
  while n > 0:
    x += n % 9
    n //= 9
  
  min_ = min(min_, x)
print(min_)

Solution (Python):
N = int(input())
wd = []
for i in range(1, 7):
    wd.append(6**i)
for i in range(1, 6):
    wd.append(9**i)
wd.append(1)

dp = [N]*(N+1)
dp[0] = 0

for i in wd:
    for j in range(i, N+1):
        dp[j] = min(dp[j], dp[j-i]+1)

print(dp[N])

Solution (Python):

N = int(input())

def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

ans = N

for i in range(N + 1):
    tmp = 0
    a_6 = Base_10_to_n(i, 6)
    for j in a_6:
        tmp += int(j)
    a_9 = Base_10_to_n(N - i, 9)
    for k in a_9:
        tmp += int(k)
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):
import math

N=int(input())
number_of_withdrawal=[100000]











for money_6 in range(0, N+1):
    money_9 = N-money_6

    i=0
    count_6_pow_number=0
    while pow(6,i)<=money_6:
        count_6_pow_number += (math.floor(money_6/pow(6,i)))%6
        i+=1
    j=0
    count_9_pow_number=0
    while pow(9,j)<=money_9:
        count_9_pow_number += (math.floor(money_9/pow(9,j)))%9
        j+=1

    
    number_of_withdrawal.append(count_6_pow_number+count_9_pow_number)

print(min(number_of_withdrawal))

Solution (Python):
n = int(input())
ans = n
for i in range(n+1):
  cc=0
  t=i
  while t>0:
    cc=cc+t%6
    t=t//6
  t=n-i
  while t>0:
    cc=cc+t%9
    t=t//9
  ans=min(ans, cc)
print(ans)

Solution (Python):
def funcDP(n):
    dp = [float('inf')] * (n + 10)
    dp[0] = 0
    for i in range(1, n + 1):
        for base in [6, 9]:
            powered = 1
            while powered <= i:
                dp[i] = min(dp[i], dp[i - powered] + 1)
                powered *= base
    return dp[n]

if __name__ == '__main__':
    print(funcDP(int(input())))

Solution (Python):
n = int(input())
res = n
ans = 0
for i in range(n+1):
    t = i
    cc = 0
    while t >= 1:
        cc = cc + t % 6
        t = t // 6

    t = n-i
    while t >= 1:
        cc = cc + t%9
        t = t // 9


    if res > cc:
        res = cc

print(res)

Solution (Python):
def sinsu(M,k):
    L=0
    MM=M
    while(True):
        L+=MM%k
        MM=MM//k
        if MM==0:
            break
    return L
N=int(input())
ans=N
for i in range(N+1):
    a=i
    b=N-i
    tmp=sinsu(a,6)+sinsu(b,9)
    if ans>tmp:
        ans=tmp
print(ans)

Solution (Python):
def min(a, b):
    if(a < b):
        return a
    else:
        return b

dp = [0]
n = 1

for n in range(100010):
    dp.append(10000000)
    power = 1
    while(power <= n):
        dp[n] = min(dp[n], dp[n-power] + 1)
        power *= 6
    power = 1
    while(power <= n):
        dp[n] = min(dp[n], dp[n-power] + 1)
        power *= 9

N = int(input())
print(dp[N])

Solution (Python):
N=int(input())
dp=[float('inf')]*100010
dp[0]=0

for i in range(N+1):
    power=1
    while power<=N:
        dp[i]=min(dp[i],dp[i-power]+1)
        power*=6
    power=1
    while power<=N:
        dp[i]=min(dp[i],dp[i-power]+1)
        power*=9

print(dp[N])

Solution (Python):
N=int(input())
dp=[float('inf')]*100010
dp[0]=0
base=[1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049]


for i in range(N+1):
    for power in base:
        dp[i]=min(dp[i],dp[i-power]+1)

print(dp[N])

Solution (Python):
N = int(input())
count = 0
ans = []
for i in range(N+1):
    count1 = 0
    count2 = 0
    N1 = N - i
    x = i
    while x:
        count1 += x % 6
        x //= 6
    while N1:
        count2 += N1 % 9
        N1 //= 9
    count = count1 + count2
    ans.append(count)
print(min(ans))

Solution (Python):
N = int(input())
count = 0
ans = []
for i in range(N+1):
    count1 = 0
    count2 = 0
    N1 = N - i
    x = i
    while x:
        count1 += x % 6
        x //= 6
    while N1:
        count2 += N1 % 9
        N1 //= 9
    count = count1 + count2
    ans.append(count)
print(min(ans))

Solution (Python):
'''a = [1, 6, 9, 6**2, ....]

dp[i+1][n]: i番目までのaから総和をｎにする組み合わせのうち、
　　　　　　選ぶ整数の数の最小値 (個数制限なし)
0 <= i <= len(a)
0 <= n <= N
'''

maxN = int(1e5)
N = int(input())
a = [1]
for i in range(1,100):
    if 6**i <= N: a.append(6**i)
    if 9**i <= N: a.append(9**i)
    if 6**i>N and 9**i>N: break
        
 
dp = [[0] + [maxN*10]*N] 

for i in range(len(a)):
    dp.append([0]*(N+1)) 
    for n in range(N+1):
        if a[i] <= n:
            dp[i+1][n] = min(dp[i][n], dp[i+1][n-a[i]] +1)
        else:
            dp[i+1][n] = dp[i][n]
            
print(dp[len(a)][N])

Solution (Python):
def main():
    N = int(input())

    dp = [0] + [float('inf')] * N
    for n in range(1, N + 1):
        for i in (6, 9):
            power = 1
            while power <= n:
                dp[n] = min(dp[n], 1 + dp[n - power])
                power *= i

    print(dp[N])

main()

Solution (Python):
global six
six = []
global nine
nine = []
ans = 1145141919810


def init():
	s = 6
	while (1):
		if s > 100000:
			break

		six.append(s)
		s *= 6

	n = 9
	while (1):
		if n > 100000:
			break

		nine.append(n)
		n *= 9

	six.reverse()
	nine.reverse()


def dfs(zan, did):
	global ans
	global six
	global nine
	if zan < 6:
		res = did + zan
		if res < ans:
			ans = res

		return

	for val in six:
		if val > zan:
			continue

		dfs(zan - val, did + 1)
		break

	for val in nine:
		if val > zan:
			continue

		dfs(zan - val, did + 1)
		break


init()
n = int(input())
dfs(n, 0)

print(ans)

Solution (Python):
n = int(input())

a = [1]
b = [1]

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

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

dp = [n] * (2 * n)
dp[0] = 0

for i in range(n):
    for j, a_ in enumerate(a):
        dp[i+a_] = min(dp[i+a_], dp[i]+1)
    for j, b_ in enumerate(b):
        dp[i+b_] = min(dp[i+b_], dp[i]+1)

print(dp[n])

Solution (Python):
INF = 1000000

dp = [INF for i in range(100040)]

dp[0]= 0

a = [1]
n = 6
while True:
    a.append(n)
    n *= 6
    if n > 100000:
        break
n= 9
while True:
    a.append(n)
    n *= 9
    if n > 100000:
        break
a.sort()
for i in range(len(a)):
    a_ =  a[i]
    for j in range(a[i],len(dp)):
        dp[j] = min(dp[j],dp[j-a_] + 1)

print(dp[int(input())])

Solution (Python):
INF = 1000000

dp = [INF for i in range(100040)]

dp[0]= 0

a = [1]
n = 6
while True:
    a.append(n)
    n *= 6
    if n > 100000:
        break
n= 9
while True:
    a.append(n)
    n *= 9
    if n > 100000:
        break

for i in range(len(a)):
    a_ =  a[i]
    for j in range(a[i],len(dp)):
        dp[j] = min(dp[j],dp[j-a_] + 1)

print(dp[int(input())])

Solution (Python):
N = int(input())
count = 0
ans = []
for i in range(N+1):
    count1 = 0
    count2 = 0
    N1 = N - i
    x = i
    for j in reversed(range(7)):
        while x >= 6**j:
            x -= 6**j
            count1 += 1
    for j in reversed(range(6)):
        while N1 >= 9**j:
            N1 -= 9**j
            count2 += 1
    count = count1 + count2
    ans.append(count)
print(min(ans))

Solution (Python):
N = int(input())
count = 0
ans = []
for i in range(N+1):
    count1 = 0
    count2 = 0
    N1 = N - i
    x = i
    while x:
        count1 += x % 6
        x //= 6
    while N1:
        count2 += N1 % 9
        N1 //= 9
    count = count1 + count2
    ans.append(count)
print(min(ans))

Solution (Python):
n = int(input())

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

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()
dp = {}

def solve(n, i):
    if i == len(c):
        return n
    if (i, n) in dp:
        return dp[(i, n)]
    if n < c[i]:
        dp[(i, n)] = solve(n, i+1)
        return solve(n, i+1)

    temp = []
    l = 9 if c[i] % 9 == 0 else 6
    l = min(l, n//c[i]+1)
    for j in range(l):
        temp.append(solve(n-j*c[i], i+1)+j)
    dp[(i, n)] = min(temp)
    return dp[(i, n)]

print(solve(n, 0))

Solution (Python):
N = int(input())
dp = [pow(10, 10)] * 100010
dp[0] = 0
dp[1] = 1
for n in range(100010):
    
    power = 1
    while n - power >= 0:
        dp[n] = min(dp[n], 1 + dp[n - power])
        power *= 6
    
    power = 1
    while n - power >= 0:
        dp[n] = min(dp[n], 1 + dp[n - power])
        power *= 9
print(dp[N])

Solution (Python):
from sys import stdin

def main():
    N = int(stdin.readline().rstrip())
    a = [y for y in [6 ** x for x in range(10)] if y <= N]
    b = [y for y in [9 ** x for x in range(10)] if y <= N]
    c = sorted(list(set(a) | set(b)))
    spam = [None] * N
    count = 0
    tmp = [-1]
    while True:
        count += 1
        tmp2 = tmp[:]
        tmp = []
        for x in c:
            for y in tmp2:
                idx = y + x
                if idx >= N:
                    continue
                if not spam[idx]:
                    spam[idx] = count
                    tmp.append(idx)
        if spam[-1]:
            print(spam[-1])
            break


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
dp = [float('inf')] * 100010
dp[0] = 0
for i in range(1, 100000 + 1):
    power = 1
    while (power <= i):
        dp[i] = min(dp[i], dp[i - power] + 1)
        power *= 6
    power = 1
    while (power <= i):
        dp[i] = min(dp[i], dp[i - power] + 1)
        power *= 9
print(dp[n])

Solution (Python):
n=int(input())
ll=[6,9,36,81,216,729,1296,6561,7776,46656,59049]
dp=[i for i in range(100010)]

for l in ll:
    for x in range(l,n+1):
        dp[x]=min(dp[x],dp[x-l]+1)
print(dp[n])

Solution (Python):
n = int(input())

withdraw = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
min_draw = [0] * 100001
for i in range(1, n + 1):
    draw = []
    for j in withdraw:
        if j <= i:
            draw.append(min_draw[i - j] + 1)
            min_draw[i] = min(draw)
print(min_draw[i])

Solution (Python):
def main():
    N = int(input())
    max_N = 100001
    dp = [N for _ in range(max_N)]
    dp[0] = 0

    for i in range(N):
        for k in range(10):
            if i+6**k <= 100000:
                dp[i+6**k] = min(dp[i+6**k], dp[i]+1)
        for k in range(10):
            if i+9**k <= 100000:
                dp[i+9**k] = min(dp[i+9**k], dp[i]+1)

    return dp[N]

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

Solution (Python):
N = int(input())

def digit_sum(n, d):
    sum = 0
    number = n
    while number > 0:
        sum += number % d
        number = number // d
    return sum

ans = float('inf')
for i in range(N+1):
    ans = min(ans, digit_sum(N-i, 6) + digit_sum(i, 9))
print(ans)

Solution (Python):
'''input
44852
'''
cur6 = [6]
cur9 = [9]
cur1 = [1]
for i in range(7):
	cur6.append(6*cur6[-1])
	cur9.append(9*cur9[-1])
n = input()
dp = [-1]*(n+1)
dp[0] = 0
dp[1] = 1
for i in range(7):
	if cur6[i] <= n:
		dp[cur6[i]] = 1
	if cur9[i] <= n:
		dp[cur9[i]] = 1
for i in range(2,n+1):
	if dp[i] == 1:
		continue
	else:
		
		dp[i] = dp[i-1]+1
		
		for j in range(7):
			if i-cur6[j] >= 0:
				dp[i] = min(dp[i],dp[i-cur6[j]]+1)
			if i-cur9[j] >= 0:
				dp[i] = min(dp[i],dp[i-cur9[j]]+1)
print dp[n]

Solution (Python):
N = int(input())


def count(base, total):
    count = 0
    exp = 1
    while exp * base <= total:
        exp *= base
    remain = total
    while exp != 1:
        count += remain // exp
        remain %= exp
        exp //= base
    count += remain
    return count


Min = float("inf")
for i in range(N + 1):
    c = 0
    c += count(6, i)
    c += count(9, N - i)
    Min = min(Min, c)
print(Min)

Solution (Python):
n = int(raw_input())

times = n
for i in range(n+1):
    c = 0
    six = i
    nine = n-i
    while six>0:
        c += six%6
        six /= 6
    while nine>0:
        c += nine%9
        nine /= 9
    if c<times:
        times = c

print times

Solution (Python):
N = int(input())
def shin(n,q):
    s = n
    a = 0
    while(s>0):
        t=s%q
        a +=t
        s=(s-t)//q
    return a
ans = N
for i in range(N+1):
    c=shin(i,6)+shin(N-i,9)
    ans = min(ans,c)
print(ans)

Solution (Python):


N = int(input())





result = N
for i in range(N+1):
    ans = 0

    six_remain = i
    six_exp = 1
    while six_exp * 6 <= six_remain:
        six_exp *= 6
    
    while six_exp != 1:
        ans += six_remain // six_exp
        six_remain %= six_exp
        six_exp //= 6
    ans += six_remain

    nine_remain = N - i
    nine_exp = 1
    while nine_exp * 9 <= nine_remain:
        nine_exp *= 9
    
    while nine_exp != 1:
        ans += nine_remain // nine_exp
        nine_remain %= nine_exp
        nine_exp //= 9
    ans += nine_remain

    result = min(result, ans)

print(result)

Solution (Python):


N = int(input())

A = []
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
B = []
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1





result = N
for i in range(N+1):
    ans = 0

    six_remain = i
    six_exp = 1
    while six_exp * 6 <= six_remain:
        six_exp *= 6
    
    while six_exp != 1:
        ans += six_remain // six_exp
        six_remain %= six_exp
        six_exp //= 6
    ans += six_remain

    nine_remain = N - i
    nine_exp = 1
    while nine_exp * 9 <= nine_remain:
        nine_exp *= 9
    
    while nine_exp != 1:
        ans += nine_remain // nine_exp
        nine_remain %= nine_exp
        nine_exp //= 9
    ans += nine_remain

    result = min(result, ans)

print(result)

Solution (Python):


N = int(input())

A = []
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1
A.sort(reverse=True)  


dp = [[-1 for j in range(N + 1)] for i in range(len(A) + 1)]


def func(i, j):
    
    if i == len(A):
        dp[i][j] = j
        return dp[i][j]

    
    if dp[i][j] >= 0:
        return dp[i][j]

    
    
    
    
    ret = 100001
    for times in range(88):
        if A[i] * times <= j:
            
            ret = min(ret, func(i+1, j-A[i]*times) + times)
        else:
            break
    dp[i][j] = ret
    return ret


print(func(0, N))

Solution (Python):


N = int(input())

A = []
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1
A.sort(reverse=True)  


dp = [[-1 for j in range(N + 1)] for i in range(len(A) + 1)]


def func(i, j):
    
    if i == len(A):
        dp[i][j] = j
        return dp[i][j]

    
    if dp[i][j] >= 0:
        return dp[i][j]

    
    
    
    
    ret = 100001
    for times in range(88):
        if A[i] * times <= j:
            
            ret = min(ret, func(i+1, j-A[i]*times) + times)
        else:
            break
    dp[i][j] = ret
    return ret


print(func(0, N))

Solution (Python):

n = int(input())

count = n

for i in range(0, n + 1):

    current_count = 0
    t = i
    while t > 0:
        current_count += t%6
        t //= 6

    t = n - i
    while t > 0:
        current_count += t%9
        t //= 9

    if count > current_count:
        count = current_count
print(count)

Solution (Python):
n = int(input())

dp = [0 for i in range(max(n+1, 36))]

for i in range(35):
    j = i // 9
    cand = []
    for k in range(j+1):
       _i = i
       _i -= 9*k
       l = _i // 6
       for m in range(l+1):
           __i = _i
           __i -= 6*m
           cand.append(k+m+__i)
    dp[i] = min(cand)
nine_list = [pow(9, i) for i in range(1, 6)]
six_list = [pow(6, i) for i in range(1, 7)]
for i in range(35, n+1):
    max_nine = nine_list[len(nine_list) - 1]
    max_six = six_list[len(six_list) - 1]
    for j in range(len(nine_list)):
        if nine_list[j] > i:
            max_nine = nine_list[j-1]
            break
    for j in range(len(six_list)):
        if six_list[j] > i:
            max_six = six_list[j-1]
            break
    dp[i] = min(dp[i-max_nine]+1, dp[i-max_six]+1)

print(dp[n])

Solution (Python):


N = int(input())

A = [1]
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1
A.sort()  


dp = [1000000 for _ in range(N + 1)]
dp[0] = 0

for i in range(1, N+1):
    a = 0
    while 6 ** a <= i:
        
        dp[i] = min(dp[i], dp[i - 6 ** a] + 1)
        a += 1
    b = 0
    while 9 ** b <= i:
        
        dp[i] = min(dp[i], dp[i - 9 ** b] + 1)
        b += 1

print(dp[N])

Solution (Python):
n=int(input())
ans=n
for i in range(n+1):
  cc=0
  t=i
  while t>0:
    cc=cc+t%6
    t=t//6
  t=n-i
  while t>0:
    cc=cc+t%9
    t=t//9
  if ans>cc:
    ans=cc
print(ans)

Solution (Python):
n = int(input())
x = 0 
s = []
L1 = [6**k for k in range(1, 7)]
L2 = [9**k for k in range(0, 6)]
for k in range(0, n//6 + 1):
    x = 0
    j = 0
    n0 = 6 * k
    while n0 > 0:
        if n0 >= L1[5 - j]:
            n0 -= L1[5 - j]
            x += 1
        else:
            j += 1
    i = 0
    n1 = n - 6 * k
    while n1 > 0:
        if n1 >= L2[5 - i]:
            n1 -= L2[5 - i]
            x += 1
        else:
            i += 1
    s.append(x)
print(min(s))

Solution (Python):
N = int(input())
res = N
for i in range(N + 1):

	cc = 0
	t = N - i
	while t > 0:
		cc += int(t % 9)
		t = int(t / 9)
	t = i

	while t > 0:
		cc += int(t % 6)
		t = int(t / 6)

	if res > cc:
		res = cc
print(res)

Solution (Python):


N = int(input())

A = [1]
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1
A.sort()  


table = [1000000 for _ in range(N + 1)]
table[0] = 0

for i in range(1, N+1):
    a = 0
    while 6 ** a <= i:
        
        table[i] = min(table[i], table[i - 6 ** a] + 1)
        a += 1
    b = 0
    while 9 ** b <= i:
        
        table[i] = min(table[i], table[i - 9 ** b] + 1)
        b += 1

print(table[N])

Solution (Python):
N = int(input())
dp = [float('inf')] * (10**5+1)
dp[0] = 0

for i in range(1, N+1):
    dp[i] = min(dp[i-1], dp[i-6], dp[i-6**2], dp[i-6**3], dp[i-6**4], dp[i-6**5], dp[i-6**6],
                dp[i-9], dp[i-9**2], dp[i-9**3], dp[i-9**4], dp[i-9**5]) + 1

print(dp[N])

Solution (Python):
N = int(input())
res = N
for i in range(N + 1):
	cc = 0
	t = i
	while t > 0:
		cc += int(t % 6)
		t = int(t / 6)
	t = N - i
	while t > 0:
		cc += int(t % 9)
		t = int(t / 9)
	if res > cc:
		res = cc
print(res)

Solution (Python):
from itertools import count, takewhile, dropwhile

def dfs(n, max9, max6, d={0:0}):
  if n in d: return d[n]
  
  max9 = next(dropwhile(lambda x: x>n, (max9 / 9**i for i in count())))
  max6 = next(dropwhile(lambda x: x>n, (max6 / 6**i for i in count())))
  d[n] = min(dfs(n-max9, max9, max6), dfs(n-max6, max9, max6)) + 1
  return d[n]

n = int(input())

max9 = max(takewhile(lambda x: x<=n, (9**i for i in count())))
max6 = max(takewhile(lambda x: x<=n, (6**i for i in count())))

print(dfs(n, max9, max6))

Solution (Python):
N = int(raw_input())
p = [1]
for j in [6, 9]:
  i = j
  while i <= N:
    p.append(i)
    i *= j
c = [N] * (N+1)
c[0] = 0
for i in range(0, N+1):
  for j in p:
    if i+j <= N:
      c[i+j] = min(c[i+j], c[i]+1)
print c[N]

Solution (Python):


import math
import os
import random
import re
import sys

def dp(N):
    global yen
    if table.get(N) != None:
        return table.get(N)
    if N == 0:
        return 0
    if N < 0:
        return sys.maxint
    result = sys.maxint
    for i in range(len(yen)):
        result = min(result,dp(N-yen[i]))
    table[N] = 1+result
    return 1+result

def solution(N):
    return dp(N)
        
table = {}

if __name__ == '__main__':
    minVal = sys.maxint
    N = int(raw_input())
    yen = [59049,46656,7776,6561,1296,729,216,81,36,9,6,1]
    result = solution(N)

    print result

Solution (Python):
INF = 10 ** 20

n = int(input())

dp = [INF for _ in range(n + 1)]
dp[0] = 0
lst = [1]
acc = 6
while acc <= n:
  lst.append(acc)
  acc *= 6
acc = 9
while acc <= n:
  lst.append(acc)
  acc *= 9
lst.sort()

for i in range(1, n + 1):
  for value in lst:
    if i - value >= 0:
      dp[i] = min(dp[i], dp[i - value] + 1)

print(dp[n])

Solution (Python):
def s(a,b):return a if a<b else s(a//b,b)+a%b
n=int(input());print(min(s(i,6)+s(n-i,9)for i in range(n+1)))

Solution (Python):
n = int(input())

dp = [i for i in range(n+1)]
dp[1] = 1
if n >= 6:
    dp[6] = 1
if n >= 9:
    dp[9] = 1
for i in range(1, n+1):
    for j in range(7):
        if i - 6**j >= 0:
            dp[i] = min(dp[i], dp[i - 6**j] + 1)
    for j in range(6):
        if i - 9**j >= 0:
            dp[i] = min(dp[i], dp[i - 9**j] + 1)
print(dp[n])

Solution (Python):


N = int(input())

A = [1]
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1
A.sort()  

table = [1000000 for _ in range(N + 1)]  
table[0] = 0

for i in range(1, N+1):
    a = 0
    while 6 ** a <= i:
        table[i] = min(table[i], table[i - 6 ** a] + 1)  
        a += 1
    b = 0
    while 9 ** b <= i:
        table[i] = min(table[i], table[i - 9 ** b] + 1)  
        b += 1

print(table[N])

Solution (Python):


N = int(input())

A = [1]
i = 1
while 6 ** i <= 100000:
    A.append(6 ** i)
    i += 1
j = 1
while 9 ** j <= 100000:
    A.append(9 ** j)
    j += 1
A.sort()

table = [1000000 for _ in range(N + 1)]
table[0] = 0

for i in range(1, N+1):
    a = 0
    while 6 ** a <= i:
        table[i] = min(table[i], table[i - 6 ** a] + 1)
        a += 1
    b = 0
    while 9 ** b <= i:
        table[i] = min(table[i], table[i - 9 ** b] + 1)
        b += 1

print(table[N])

Solution (Python):
n = int(input())
options = []
x = 6
while x <= n:
    options.append(x)
    x *= 6
x = 9
while x <= n:
    options.append(x)
    x *= 9
dp = list(range(n + 1))
for opt in options:
    for i in range(n - opt + 1):
        dp[i + opt] = min(dp[i + opt], dp[i] + 1)
print(dp[-1])

Solution (Python):
def by6(x):
    res = 0
    k = 6**7
    while x > 0:
        while x >= k:
            x -= k
            res += 1
        k /= 6
    return res
def by9(x):
    res = 0
    k = 9**6
    while x > 0:
        while x >= k:
            x -= k
            res += 1
        k /= 9
    return res
n = int(input())
ans = n
for i in range(n + 1):
    ans = min(ans, by6(i) + by9(n - i))
print(ans)