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):
import sys
sys.setrecursionlimit(10**7)
memo = [-1] * 110000  


def rec(n):
    if n == 0:
        return 0
    if memo[n] != -1:
        return memo[n]  

    res = n
    pow6 = 1
    while pow6 <= n:
        res = min(res, rec(n - pow6) + 1)
        pow6 *= 6

    pow9 = 1
    while pow9 <= n:
        res = min(res, rec(n - pow9) + 1)
        pow9 *= 9
    memo[n] = res
    return res


N = int(input())
rec(N)
print(memo[N])

Solution (Python):
import math
n = int(input())
dp = [float("inf")] * (10 ** 5 + 10)
dp[0] = 0
for i in range(1,n + 1):
  power = 1
  while n >= power:
    dp[i] = min(dp[i],dp[i - power] + 1)
    power *= 6
  power = 1
  while n >= power:
    dp[i] = min(dp[i],dp[i - power] + 1)
    power *= 9
print(dp[n])

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

LIST=[10**9]*(10**7)
LIST[0]=1

i=1
c=1
while(6**i <= N):
    LIST[c]=6**i
    c+=1
    if(9**i <= N):
        LIST[c]=9**i
        c+=1
    i+=1
LIST=LIST[0:c]
LIST=list(sorted(LIST,reverse=True))
MIN=10**10

def calc(L,i,c):
    global MIN
    global N
    if(MIN<c):
        return
    if(N-L >= LIST[i]):
        calc(L+LIST[i],i,c+1)
        if(MIN<c):
            return
    if(len(LIST)-2>=i):
        calc(L,i+1,c)
        if(MIN<c):
            return
    if(N==L):
        MIN=min(MIN,c)
  

calc(0,0,0)
print(MIN)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()


dp = [INF] * (N+1)
dp[0] = 0
for i in range(N):
    
    dp[i+1] = min(dp[i+1], dp[i] + 1)
    j = 6
    while i+j <= N:
        dp[i+j] = min(dp[i+j], dp[i] + 1)
        j *= 6
    j = 9
    while i+j <= N:
        dp[i+j] = min(dp[i+j], dp[i] + 1)
        j *= 9
print(dp[N])

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()


dp = [INF] * (N+1)
dp[0] = 0
for i in range(N):
    
    dp[i+1] = min(dp[i+1], dp[i] + 1)
    j = 1
    while i+6**j <= N:
        dp[i+6**j] = min(dp[i+6**j], dp[i] + 1)
        j += 1
    j = 1
    while i+9**j <= N:
        dp[i+9**j] = min(dp[i+9**j], dp[i] + 1)
        j += 1
print(dp[N])

Solution (Python):

n = int(input())

coin_list = [1]

i=1
while 6**i <= 10**5:
    coin_list.append(6**i)
    i+=1

i=1
while 9**i <= 10**5:
    coin_list.append(9**i)
    i+=1


dp = [[10**5 for i in range(n+1)] for j in range(len(coin_list))]
dp[0] = [i for i in range(n+1)]

for i in range(n+1):
    for j,coin in enumerate(coin_list):
        if j==0:
            continue
        if coin <= i:
            dp[j][i] = min(dp[j-1][i],dp[j][i-coin]+1)
        else:
            dp[j][i] = dp[j-1][i]
print(dp[-1][n])

Solution (Python):
import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())
    D = [k for k in range(N+1)]
    for d in range(10):
        for k in range(N-6**d+1):
            D[k+6**d] = min(D[k+6**d],D[k]+1)
    for d in range(10):
        for k in range(N-9**d+1):
            D[k+9**d] = min(D[k+9**d],D[k]+1)
    print(D[N])
if __name__ == '__main__':
    main()

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

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

dp[0] = 0

for i in range(n):
    six = 1
    while i + six <= n:
        dp[i + six] = min(dp[i + six], dp[i] + 1)
        six *= 6

    nine = 1
    while i + nine <= n:
        dp[i + nine] = min(dp[i + nine], dp[i] + 1)
        nine *= 9

print(dp[n])

Solution (Python):
n = int(input())
dp = [1000000] * 100010
dp[0] = 0
for i in range(1, n + 1):
    a = 1
    while a <= i:
        dp[i] = min(dp[i], dp[i - a] + 1)
        a *= 6
    b = 1
    while b <= i:
        dp[i] = min(dp[i], dp[i - b] + 1)
        b *= 9
print(dp[n])

Solution (Python):
n = int(input())
dp = [1000000] * 100010
dp[0] = 0
for i in range(n):
    a = 1
    while i + a <= 100000:
        dp[i + a] = min(dp[i + a], dp[i] + 1)
        a *= 6
    b = 1
    while i + b <= 100000:
        dp[i + b] = min(dp[i + b], dp[i] + 1)
        b *= 9
print(dp[n])

Solution (Python):
N = int(input())
ps = [1]

i6 = 1
while 6**i6 <=N:
  ps.append(6**i6)
  i6 += 1

i9 = 1
while 9**i9 <=N:
  ps.append(9**i9)
  i9 += 1

len_ps = len(ps)
dp = [float('inf') for _ in range(N+1)]
dp[0] = 0
for i in range(1,N+1):
  for j in range(len_ps):
    if i-ps[j]>=0:dp[i] = min(dp[i], 1+dp[i-ps[j]])

print(dp[-1])

Solution (Python):
n = int(input())
dp = [0]*(n+1)
for i in range(1,n+1):
    dp[i] = 10**9

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

        
print(dp[n])

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())

coin = [6**n for n in range(10) if 6**n <= N]
coin += [9**n for n in range(10) if 9**n <= N]

dp = [0] * (N+1)

for i in range(1,N+1):
    temp = []
    for c in coin:
        if i - c >= 0:
            temp.append(dp[i-c] + 1)
    dp[i] = min(temp)
print(dp[-1])

Solution (Python):
n = int(input())
def calc(c, p):
    res = 0
    while c:
        res += c % p
        c = c // p
    return res
ans = n
for i in range(n+1):
    cand = calc(i, 6) + calc(n - i, 9)
    ans = min(ans, cand)
print(ans)

Solution (Python):
N=int(input())
ans=10**5

for i in range(N+1):
    cc=0
    t,s=i,N-i
    while t>0:
        cc+=t%6
        t//=6
    while s>0:
        cc+=s%9
        s//=9
    ans=min(ans,cc)

print(ans)

Solution (Python):
n=int(input())
li6=[46656,7776,1296,216,36,6,1]
li9=[59049,6561,729,81,9,1]
dp6=[0]*(n+1)
dp9=[0]*(n+1)
dp6[1]=1
dp9[1]=1
ans=[]
for i in range(2,n+1):
    for j in range(7):
        if i>=li6[j]:
            k=i-li6[j]
            break
    dp6[i]=1+dp6[k] 
for i in range(2,n+1):
    for j in range(6):
        if i>=li9[j]:
            k=i-li9[j]
            break
    dp9[i]=1+dp9[k]  
for i in range(n+1):
    ans.append(dp6[i]+dp9[n-i])                    
print(min(ans))

Solution (Python):
n=int(input())
import math
ansa=[0]
ansb=[0]
ans=[]
cnt=0
x=0
y=0
for i in range(1,n+1):
    while 9**(x+1)<=i:
        x+=1
    for j in range(x,-1,-1):
        z=(math.floor(i/(9**j))%9)
        i-=(9**j)*z
        cnt+=z
    ansa.append(cnt)
    cnt=0    
    
for i in range(1,n+1):
    while 6**(y+1)<=i:
        y+=1
    for j in range(y,-1,-1):
        z=(math.floor(i/(6**j))%6)
        i-=(6**j)*z
        cnt+=z
    ansb.append(cnt)
    cnt=0
    
for i in range(n+1):
    ans.append(ansa[i]+ansb[n-i])
    
print(min(ans))

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

Solution (Python):
from sys import stdin
import bisect

n = int(stdin.readline().rstrip())
roku = [0]+[6**_ for _ in range(10) if 6**_ <= n]
kyu = [0]+[9**_ for _ in range(10) if 9**_ <= n]

ma = 10**10
for i in range(n+1):
    six = i
    nine = n-i
    count = 0
    while True:
        if six == 0:
            break
        six -= roku[bisect.bisect(roku,six)-1]
        count += 1
    while True:
        if nine <= 0:
            break
        nine -= kyu[bisect.bisect(kyu,nine)-1]
        count += 1
    ma = min(ma,count)
print(ma)

Solution (Python):
import math
N=int(input())
l=[1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
dp=[100000 for _ in range(N+1)]
dp[0] = 0
for i in l:
    if i > N:
        break
    for j in range(i,N+1):
        dp[j] = min(dp[j],dp[j-i]+1)
print(dp[-1])

Solution (Python):

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

Solution (Python):
N = int(input())
INF = float('inf')
dp = [INF for _ in range(N+1)]
dp[0] = 0

for n in range(N + 1):
    power = 0
    while pow(6, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(6, power)] + 1)
        power += 1

    power = 0
    while pow(9, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(9, power)] + 1)
        power += 1

print(dp[N])

Solution (Python):
N = int(input())
INF = float('inf')
dp = [INF for _ in range(N+1)]
dp[0] = 0

for n in range(N + 1):
    power = 0
    while pow(6, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(6, power)] + 1)
        power += 1

    power = 0
    while pow(9, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(9, power)] + 1)
        power += 1

print(dp[N])

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

for n in range(N + 1):
    power = 0
    while pow(6, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(6, power)] + 1)
        power += 1

    power = 0
    while pow(9, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(9, power)] + 1)
        power += 1

print(dp[N])

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

for n in range(N + 1):
    power = 0
    while pow(6, power) <= N:
        dp[n] = min(dp[n], dp[n - pow(6, power)] + 1)
        power += 1

    power = 0
    while pow(9, power) <= N:
        dp[n] = min(dp[n], dp[n - pow(9, power)] + 1)
        power += 1

print(dp[N])

Solution (Python):
def main():
    weight = [6**i for i in range(7)] + [9**i for i in range(1, 6)]
    W = int(input())
    n = len(weight)

    dp = [[1 << 62 for i in range(W + 1)] for j in range(n + 1)]
    for i in range(n):
        dp[i][0] = 0
    for i in range(n):
        for w in range(W + 1):
            if w - weight[i] >= 0:
                dp[i + 1][w] = min(dp[i][w], dp[i+1][w - weight[i]] + 1)
            else:
                dp[i + 1][w] = dp[i][w]

    print(dp[n][W])


if __name__ == '__main__':
    main()

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

ans = n
for k in range(0,n+1):
    count = 0
    for i in range(0,7):
        num6 = k//(6**i)%6
        count += num6

    for i in range(0,6):
        num9 = (n-k)//(9**i)%9
        count += num9

    if count < ans: 
        ans = count

print(ans)

Solution (Python):
def c6(x):
    res=0
    while x>0:
        res,x=res+x%6,x//6
    return res

def c9(y):
    res=0
    while y>0:
        res,y=res+y%9,y//9
    return res

N=int(input())
ans=100000
for x in range(N+1):
    
    ans=min(ans,c6(x)+c9(N-x))

print(ans)

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

s = 6
m = 9
Can = [1]
count = 2
while s < 10**5:
    Can.append(s)
    s = 6**count
    count += 1

count = 2
while m < 10**5:
    Can.append(m)
    m = 9**count
    count += 1

DP = [1]
for i in range(1,n):
    dp_can = []
    if i+1 in Can:
        
        DP.append(1)
        continue
    
    for num in Can:
        if num > i:
            continue
        dp_can.append(DP[i-num]+1)
    DP.append(min(dp_can))


print(DP[-1])

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

Solution (Python):
N=int(input())
count=N
for i in range(0,N+1,1):
    n=(i%9 + (N-i)%6)

    n9=0
    num9=i//9
    while num9:
        n9+=num9%9
        num9//=9

    n6=0
    num6=(N-i)//6
    while num6:
        n6+=num6%6
        num6//=6
    count=min(n9+n6+n,count)
print(count)

Solution (Python):

N=int(input())
dp=[float('inf') for i in range((10**5)*2)]
L6=[6**i for i in range(1,8)]
L9=[9**i for i in range(1,7)]
a=[1]
L=a+L6+L9
L.sort()
dp[0]=0
for i in range(N+1):
    for j in range(14):
        if i-L[j]>=0:
            dp[i]=min(dp[i],1+dp[i-L[j]])
print(dp[N])

Solution (Python):

N=int(input())
dp=[float('inf') for i in range((10**5)*2)]
L6=[6**i for i in range(1,8)]
L9=[9**i for i in range(1,7)]
a=[1]
L=a+L6+L9
L.sort()
dp[0]=0
for i in range(N+1):
    for j in range(14):
        if i+L[j]<=N:
            dp[i+L[j]]=min(dp[i+L[j]], dp[i]+1)
print(dp[N])

Solution (Python):

if __name__ == '__main__':
    N=int(input())

    dp=[100000000000]*(N+1)
    for i in range(min(6,N+1)):
        dp[i]=i
    for i in range(N+1):
        for j in range(1,7):
            if i+6**j<=N:
                dp[i+6**j]=min(dp[i+6**j],dp[i]+1)
        for j in range(1, 6):
            if i + 9 ** j <= N:
                dp[i + 9 ** j] = min(dp[i + 9 ** j], dp[i] + 1)


    print(dp[N])

Solution (Python):
N=int(input())
ans=[0, 1, 2, 3, 4, 5]+[100001]*(N-5)
  
for i in range(N):
  for j in range(1, 7):
    if i+6**j<=N:
      ans[i+6**j]=min(ans[i]+1, ans[i+6**j])
      
  for j in range(1, 6):
    if i+9**j<=N:
      ans[i+9**j]=min(ans[i]+1, ans[i+9**j])
      
print(ans[N])

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



dp=[100000000000000]*(n+1)
dp[0]=0
for i in range(1,min(6,n+1)):
    dp[i]=i

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

print(dp[n])

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

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

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

print(dp[N])

Solution (Python):
N=int(input())
dstb=[1]+[6**i for i in range(1,7)]+[9**j for j in range(1,6)]
DP=[10**5]*(10**5*2)
DP[0]=0
for i in range(N+1):
  for j in dstb:
    DP[i+j]=min(DP[i+j], DP[i]+1)

print(DP[N])

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, functools, copy




input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')

sys.setrecursionlimit(10**7)

inf = float('inf')
mod = 10**9+7
ans = inf ; count = 0; pro = 1

n=int(input())
count=0


@functools.lru_cache(maxsize=None)
def func(n):
    if n==0:return 0
    
    
    
    
    
    if n==0:
        return 0
    re=inf
    k=s=1
    while 9**s<=n:
        re=min(re,func(n-9**s)+1)
        s+=1
    while 6**k<=n:
        re=min(re,func(n-6**k)+1)
        k+=1
    re=min(re,func(n-1)+1)
    
    return re
    
print(func(n))

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, functools, copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ; count = 0; pro = 1

n=int(input())
count=0

memo=[0]*(n+1)
memo[0]=0
def func(n):
    if memo[n] or n==0:return memo[n]
    
    
    
    
    if n==0:
        return 0
    re=func(n-1)+1
    k=s=1
    while 9**s<=n:
        re=min(re,func(n-9**s)+1)
        s+=1
    while 6**k<=n:
        re=min(re,func(n-6**k)+1)
        k+=1
    re=min(re,func(n-1)+1)
    memo[n]=re
    return re
    
print(func(n))

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, functools, copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ; count = 0; pro = 1

n=int(input())
count=0

memo=[0]*(n+1)
memo[0]=0
def func(n):
    if memo[n] or n==0:return memo[n]
    
    
    
    
    if n==0:
        return 0
    re=inf
    k=s=1
    while 9**s<=n:
        re=min(re,func(n-9**s)+1)
        s+=1
    while 6**k<=n:
        re=min(re,func(n-6**k)+1)
        k+=1
    re=min(re,func(n-1)+1)
    memo[n]=re
    return re
    
print(func(n))

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, functools, copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ; count = 0; pro = 1

n=int(input())
count=0

memo=[0]*(n+1)
memo[0]=0
def func(n):
    if memo[n] or n==0:return memo[n]
    
    
    
    
    if n==0:
        return 0
    re=inf
    k=s=1
    while 6**k<=n:
      re=min(re,func(n-6**k)+1)
      k+=1
    while 9**s<=n:
      re=min(re,func(n-9**s)+1)
      s+=1

    re=min(re,func(n-1)+1)
    memo[n]=re
    return re
    
print(func(n))

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, functools, copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ; count = 0; pro = 1

n=int(input())
count=0

memo=[0]*(n+1)
memo[0]=0
def func(n):
    if memo[n] or n==0:return memo[n]
    
    
    
    
    if n==0:
        return 0
    re=inf
    k=s=1
    while 6**k<=n:
      re=min(re,func(n-6**k)+1)
      k+=1
    while 9**s<=n:
      re=min(re,func(n-9**s)+1)
      s+=1

    re=min(re,func(n-1)+1)
    memo[n]=re
    return re
    
print(func(n))

Solution (Python):

import sys, math, itertools, heapq, collections, bisect, functools, copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ; count = 0; pro = 1

n=int(input())
count=0

memo=[0]*(n+1)
memo[0]=0
def func(n):
    if memo[n] or n==0:return memo[n]
    
    
    
    
    if n==0:
        return 0
    re=inf
    k=s=1
    while 9**s<=n:
        re=min(re,func(n-9**s)+1)
        s+=1
    while 6**k<=n:
        re=min(re,func(n-6**k)+1)
        k+=1
    re=min(re,func(n-1)+1)
    memo[n]=re
    return re
    
print(func(n))

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

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

Solution (Python):

args = int(input())

ans = float('inf')
for i in range(args+1):
    count = 0

    args_six = i
    while args_six >= 6:
        count += args_six % 6
        args_six //= 6
    count += args_six

    args_nine = args - i
    while args_nine >= 9:
        count += args_nine % 9
        args_nine //= 9
    count += args_nine
    ans = min(ans, count)

print(ans)

Solution (Python):
N = int(input())
dp = [float("inf")]*(N + 1)
dp[0] = 0
pull_list_9 = [9**i for i in range(1, 6)]
pull_list_6 = [6**i for i in range(1, 7)]
pull_list = pull_list_9 + pull_list_6 + [1]
p_s = sorted(pull_list)

for i in range(1, N + 1):
  for j in p_s:
    if i - j < 0:
      break
    dp[i] = min(dp[i], dp[i-j] + 1)

print(dp[-1])