Problem p03329 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long

const int INF = 1e9;
const long long LLINF = 1e18;

using namespace std;

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};

/*==================================================*/
/*variable*/
int N;
vector<int> v;

int dp[15][100000]; // 金額iを作るための最小コスト
/*==================================================*/
/*function*/

/*==================================================*/

int main() {
  cin >> N;

  v.push_back(1);
  int t1 = 6;
  while (t1 < 100000) {
    v.push_back(t1);
    t1 *= 6;
  }
  int t2 = 9;
  while (t2 < 100000) {
    v.push_back(t2);
    t2 *= 9;
  }
  sort(v.begin(), v.end());

  REP(i, 0, v.size() + 1) {
    REP(j, 0, 100010) {
      if (i > 0)
        dp[i][j] = j;
      else
        dp[i][j] = INF;

      if (j == 0)
        dp[i][j] = 0;
    }
  }

  REP(i, 1, v.size() + 1) {
    REP(j, 1, 100010) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j - v[i - 1] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i - 1]] + 1);
        dp[i][j] = min(dp[i][j], dp[i][j - v[i - 1]] + 1);
      }
    }
  }

  cout << dp[v.size()][N] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long

const int INF = 1e9;
const long long LLINF = 1e18;

using namespace std;

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};

/*==================================================*/
/*variable*/
int N;
vector<int> v;

int dp[15][100050]; // 金額iを作るための最小コスト
/*==================================================*/
/*function*/

/*==================================================*/

int main() {
  cin >> N;

  v.push_back(1);
  int t1 = 6;
  while (t1 < 100000) {
    v.push_back(t1);
    t1 *= 6;
  }
  int t2 = 9;
  while (t2 < 100000) {
    v.push_back(t2);
    t2 *= 9;
  }
  sort(v.begin(), v.end());

  REP(i, 0, v.size() + 1) {
    REP(j, 0, 100010) {
      if (i > 0)
        dp[i][j] = j;
      else
        dp[i][j] = INF;

      if (j == 0)
        dp[i][j] = 0;
    }
  }

  REP(i, 1, v.size() + 1) {
    REP(j, 1, 100010) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j - v[i - 1] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i - 1]] + 1);
        dp[i][j] = min(dp[i][j], dp[i][j - v[i - 1]] + 1);
      }
    }
  }

  cout << dp[v.size()][N] << endl;

  return 0;
}