Problem p03329 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define RREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
#define YY cout << "Yes" << endl
#define NN cout << "No" << endl
const long long INF = 1000000007;
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
vector<ll> dp(100010, -1);
ll f(ll n) {
  if (n == 0)
    return 0;
  ll r = INF;
  for (int pow6 = 1; pow6 <= n; pow6 *= 6)
    r = min(r, f(n - pow6) + 1);
  for (int pow9 = 1; pow9 <= n; pow9 *= 9)
    r = min(r, f(n - pow9) + 1);
  return dp[n] = r;
}
int solve() {
  int n;
  cin >> n;
  cout << f(n) << endl;
  return 0;
}
int main() {
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  // cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctype.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define RREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
#define YY cout << "Yes" << endl
#define NN cout << "No" << endl
const long long INF = 1000000007;
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
vector<ll> dp(100010, -1);
ll f(ll n) {
  if (n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  ll r = INF;
  for (int pow6 = 1; pow6 <= n; pow6 *= 6)
    r = min(r, f(n - pow6) + 1);
  for (int pow9 = 1; pow9 <= n; pow9 *= 9)
    r = min(r, f(n - pow9) + 1);
  return dp[n] = r;
}
int solve() {
  int n;
  cin >> n;
  cout << f(n) << endl;
  return 0;
}
int main() {
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  // cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}