Problem p02669 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

map<i64, i64> memo;
i64 a, b, c, d;

i64 dp(i64 n) {
  if (n < 0)
    return 1e18;

  if (n == 0)
    return 0;
  if (memo.count(n))
    return memo[n];
  i64 &res = memo[n];
  res = 1e18;
  if (abs(n) < 2e9)
    res = abs(n) * d;

  fore(delta, -4, 4) {
    i64 x = n + delta;

    if (x % 2 == 0)
      uin(res, dp(x / 2) + a + abs(delta) * d);
    if (x % 3 == 0)
      uin(res, dp(x / 3) + b + abs(delta) * d);
    if (x % 5 == 0)
      uin(res, dp(x / 5) + c + abs(delta) * d);
  }
  return memo[n] = res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;

  freopen("input.txt", "r", stdin);

  int T;
  cin >> T;
  while (T--) {
    i64 n;
    cin >> n >> a >> b >> c >> d;
    memo.clear();
    cout << dp(n) << '\n';
  }

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

map<i64, i64> memo;
i64 a, b, c, d;

i64 dp(i64 n) {
  if (n < 0)
    return 1e18;

  if (n == 0)
    return 0;
  if (memo.count(n))
    return memo[n];
  i64 &res = memo[n];
  res = 1e18;
  if (abs(n) < 2e9)
    res = abs(n) * d;

  fore(delta, -4, 4) {
    i64 x = n + delta;

    if (x % 2 == 0)
      uin(res, dp(x / 2) + a + abs(delta) * d);
    if (x % 3 == 0)
      uin(res, dp(x / 3) + b + abs(delta) * d);
    if (x % 5 == 0)
      uin(res, dp(x / 5) + c + abs(delta) * d);
  }
  return memo[n] = res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;

  // freopen("input.txt", "r", stdin);

  int T;
  cin >> T;
  while (T--) {
    i64 n;
    cin >> n >> a >> b >> c >> d;
    memo.clear();
    cout << dp(n) << '\n';
  }

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}