Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  int t;
  cin >> t;
  vl l{2, 3, 5};
  while (t--) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    vl p{0, 1, a, b, 4, c};
    ll lim = LINF / d;
    map<ll, ll> memo;
    function<ll(ll)> rec = [&](ll m) {
      if (m == 0)
        return 0LL;
      if (m == 1)
        return d;
      if (memo.count(m))
        return memo[m];
      ll ret = LINF;
      if (m <= lim)
        ret = d * m;
      for (auto k : l) {
        ll r = m % k;
        chmin(ret, p[k] + d * r + rec(m / k));
        chmin(ret, p[k] + d * (k - r) + rec((m + k - 1) / k));
      }
      return memo[m] = ret;
    };
    cout << rec(n) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n)-1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\\n'
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void prn(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << ' ';
}
template <class T> inline void printv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');
}
template <class T> inline void printvv(vector<vector<T>> &v) {
  for (auto u : v)
    printv(u);
}
template <class T> inline void printlnv(vector<T> &v) {
  int n = sz(v);
  REP(i, n) cout << v[i] << endl;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}

void solve() {
  int t;
  cin >> t;
  vl l{2, 3, 5};
  while (t--) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    vl p{0, 1, a, b, 4, c};
    ll lim = LINF / d;
    map<ll, ll> memo;
    memo[n] = 0;
    function<void(ll)> rec = [&](ll m) {
      // cout << m << ": " << memo[m] << endl;
      if (m == 1) {
        if (memo.count(0) == 0 or memo[0] > memo[1] + d)
          memo[0] = memo[1] + d;
        return;
      }
      for (auto k : l) {
        ll r = m % k;
        ll mn = p[k] + d * r;
        if (m - (m / k) <= lim)
          chmin(mn, d * (m - (m / k)));
        if (memo.count(m / k) == 0 or memo[m / k] > memo[m] + mn) {
          memo[m / k] = memo[m] + mn;
          rec(m / k);
        }
        mn = p[k] + d * (k - r);
        if (m - (m / k + 1) <= lim)
          chmin(mn, d * (m - (m / k + 1)));
        if (memo.count(m / k + 1) == 0 or memo[m / k + 1] > memo[m] + mn) {
          memo[m / k + 1] = memo[m] + mn;
          rec(m / k + 1);
        }
      }
    };
    rec(n);
    cout << memo[0] << endl;
  }
}