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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	"strings"
)

// 入る場合と入らない場合, 個数
var cnd = []int{1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049}

func (p *problem) solve() {
	N := p.NextInt()
	cmin := 100001
	q := []vec{{i: len(cnd) - 1, n: N}}
	for len(q) != 0 {
		next := q[0]
		if next.c > cmin {
			q = q[1:]
			continue
		}
		if next.n == 0 {
			cmin = min(cmin, next.c)
			q = q[1:]
			continue
		}
		if cnd[next.i] > next.n {
			q[0].i--
			continue
		}
		if next.i != 0 {
			q = append(q, vec{
				i: next.i - 1,
				n: next.n,
				c: next.c,
			})
		}
		q[0].n -= cnd[next.i]
		q[0].c++

	}
	p.PrintInt(cmin)
}

type vec struct {
	i, n, c int
}

// i = 現在試しているcnd, n = 残り, return 現在の試行回数
func rec(i, n int) int {
	switch {
	// 残りがなければend
	case n <= 0:
		return 0
	// 残り金額より大きい場合はcndを下げる
	case cnd[i] > n:
		return rec(i-1, n)
	case (i - 1) < 0:
		return rec(i, n-cnd[i]) + 1
	// 入れる場合と入れない場合で回数が小さい方を選択
	default:
		return min(rec(i, n-cnd[i])+1, rec(i-1, n))
	}
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

var (
	debug  bool
	output string
)

func main() {
	flag.BoolVar(&debug, "debug", false, "Print debug logs")
	flag.BoolVar(&debug, "d", false, "Print debug logs")
	flag.StringVar(&output, "output", "stdout", "Specify output target (Default: stdout)")
	flag.StringVar(&output, "o", "stdout", "Specify output target (Default: stdout)")
	flag.Parse()

	if debug {
		out := os.Stdout
		if output != "stdout" {
			logfile, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY, 0666)
			if err != nil {
				panic(err)
			}
			defer logfile.Close()
			out = logfile
		}
		log.SetOutput(out)
	} else {
		log.SetOutput(ioutil.Discard)
	}

	p := problem{
		in:  bufio.NewReader(os.Stdin),
		out: bufio.NewWriter(os.Stdout),
	}
	p.solve()

	if err := p.out.Flush(); err != nil {
		panic(err)
	}
}

type problem struct {
	in  *bufio.Reader
	out *bufio.Writer
}

func (p *problem) PrintInt(d int) {
	p.Printf("%d\n", d)
}

func (p *problem) Printf(format string, a ...interface{}) {
	if _, err := fmt.Fprintf(p.out, format, a...); err != nil {
		panic(err)
	}
}

func (p *problem) Printfln(format string, a ...interface{}) {
	if _, err := fmt.Fprintf(p.out, fmt.Sprintf("%v\n", format), a...); err != nil {
		panic(err)
	}
}

func (p *problem) NextInt() int {
	return stoi(p.Next())
}

func (p *problem) NextInts() []int {
	line := p.NextSSLine()
	ds := make([]int, len(line))
	for i, s := range line {
		ds[i] = stoi(s)
	}
	return ds
}

func (p *problem) NextSSLine() []string {
	return strings.Split(p.Next(), " ")
}

// Next reads line and return string.
func (p *problem) Next() string {
	var b []byte
	for {
		l, pre, err := p.in.ReadLine()
		if err != nil {
			panic(err)
		}
		b = append(b, l...)
		if !pre {
			break
		}
	}
	return string(b)
}

func stoi(s string) int {
	i, err := strconv.Atoi(s)
	if err != nil {
		fmt.Println(i)
		fmt.Println(err)
	}
	return i
}