Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define rep1(i, N) for (int i = 1; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
#define print(v)                                                               \
  {                                                                            \
    cerr << #v << ": [ ";                                                      \
    for (auto _ : v)                                                           \
      cerr << _ << ", ";                                                       \
    cerr << "]" << endl;                                                       \
  }
#define printpair(v)                                                           \
  {                                                                            \
    cerr << #v << ": [ ";                                                      \
    for (auto _ : v)                                                           \
      cerr << "{" << _.first << "," << _.second << "}"                         \
           << ", ";                                                            \
    cerr << "]" << endl;                                                       \
  }
#define dump(x) cerr << #x << ": " << x << endl;
#define bit(k) (1LL << (k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18 + 1;
const ll MOD = (ll)1e9 + 7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/
// dp[i]: i円引き出すのに必要な操作回数の最小値
ll dp[100100];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  rep(i, N + 1) dp[i] = INFLL;
  dp[0] = 0;
  ll e[] = {6, 9};
  rep(j, 2) {
    ll cost = 1;
    while (cost <= N) {
      rep(i, N) {
        if (dp[i] == INFLL)
          continue;
        chmin(dp[i + cost], dp[i] + 1);
      }
      cost *= e[j];
    }
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < int(N); ++i)
#define rep1(i, N) for (int i = 1; i < int(N); ++i)
#define all(a) (a).begin(), (a).end()
#define print(v)                                                               \
  {                                                                            \
    cerr << #v << ": [ ";                                                      \
    for (auto _ : v)                                                           \
      cerr << _ << ", ";                                                       \
    cerr << "]" << endl;                                                       \
  }
#define printpair(v)                                                           \
  {                                                                            \
    cerr << #v << ": [ ";                                                      \
    for (auto _ : v)                                                           \
      cerr << "{" << _.first << "," << _.second << "}"                         \
           << ", ";                                                            \
    cerr << "]" << endl;                                                       \
  }
#define dump(x) cerr << #x << ": " << x << endl;
#define bit(k) (1LL << (k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18 + 1;
const ll MOD = (ll)1e9 + 7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/
// dp[i]: i円引き出すのに必要な操作回数の最小値
ll dp[100100];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  rep(i, N + 1) dp[i] = INFLL;
  dp[0] = 0;
  ll e[] = {6, 9};
  rep(j, 2) {
    ll cost = 1;
    while (cost <= N) {
      rep(i, N) {
        if (dp[i] == INFLL)
          continue;
        if (i + cost > N)
          continue;
        chmin(dp[i + cost], dp[i] + 1);
      }
      cost *= e[j];
    }
  }
  cout << dp[N] << endl;
}