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 in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        new Program().Calc();
    }

    Scanner cin;

    public Program() { }

    int[] _minValueCount;
    int[] _value;

    void Calc()
    {
        cin = new Scanner();

        int n = cin.nextInt();

        const int ValueCount = 12;
        _value = new int[ValueCount] { (int)Math.Pow(9, 5), (int)Math.Pow(9, 4), (int)Math.Pow(9, 3), (int)Math.Pow(9, 2), (int)Math.Pow(9, 1),
            (int)Math.Pow(6, 6), (int)Math.Pow(6, 5), (int)Math.Pow(6, 4), (int)Math.Pow(6, 3), (int)Math.Pow(6, 2), (int)Math.Pow(6, 1), 1 };

        Array.Sort(_value);

        _minValueCount = new int[n + 1];
        for (int i = 1; i <= n; ++i)
        {
            _minValueCount[i] = int.MaxValue;
        }

        for (int sum = 1; sum <= n; ++sum)
        {
            int minValueCount = int.MaxValue;

            for (int valueIndex = 0; valueIndex < ValueCount; ++valueIndex)
            {
                int value = _value[valueIndex];

                if (sum < value) { break; }

                minValueCount = Math.Min(minValueCount, _minValueCount[sum - value] + 1);
            }

            _minValueCount[sum] = minValueCount;
        }

        int ans = _minValueCount[n];

        Console.WriteLine(ans);
    }
}

class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}