Problem p02669 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

bool overflowAdd(ll a, ll b) { return LLONG_MAX - b < a; }

bool overflowProduct(ll a, ll b) {
  if (a == 0 || b == 0)
    return false;
  return a > LLONG_MAX / b;
}

ll dfs(ll N, ll A, ll B, ll C, ll D, unordered_map<ll, ll> &memo) {
  if (memo.find(N) != memo.end())
    return memo[N];
  ll cost = LLONG_MAX;
  vector<pair<ll, ll>> pairList{{2, A}, {3, B}, {5, C}};
  for (const auto &p : pairList) {
    for (const ll diff : {1, -1}) {
      ll n = N;
      while (n % p.first != 0)
        n += diff;
      if (n / p.first >= N)
        continue;
      ll newCost = abs(N - n) * D + p.second;
      if (overflowAdd(newCost, dfs(n / p.first, A, B, C, D, memo)))
        continue;
      newCost += dfs(n / p.first, A, B, C, D, memo);
      cost = min(cost, newCost);
    }
  }
  if (!overflowProduct(N, D)) {
    cost = min(cost, N * D);
  }
  memo[N] = cost;
  return cost;
}

signed main() {
  int T;
  cin >> T;
  rep(ti, 0, T) {
    ll N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    unordered_map<ll, ll> memo;
    memo[0] = 0;
    memo[1] = D;
    cout << dfs(N, A, B, C, D, memo) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

bool overflowAdd(ll a, ll b) { return LLONG_MAX - b <= a; }

bool overflowProduct(ll a, ll b) {
  if (a == 0 || b == 0)
    return false;
  ll product = a * b;
  return product / a != b;
}

ll dfs(ll N, ll A, ll B, ll C, ll D, unordered_map<ll, ll> &memo) {
  if (memo.find(N) != memo.end())
    return memo[N];
  ll cost = LLONG_MAX;
  vector<pair<ll, ll>> pairList{{2, A}, {3, B}, {5, C}};
  for (const auto &p : pairList) {
    for (const ll diff : {1, -1}) {
      ll n = N;
      while (n % p.first != 0)
        n += diff;
      if (n / p.first >= N)
        continue;
      ll newCost = abs(N - n) * D + p.second;
      if (overflowAdd(newCost, dfs(n / p.first, A, B, C, D, memo)))
        continue;
      newCost += dfs(n / p.first, A, B, C, D, memo);
      cost = min(cost, newCost);
    }
  }
  if (!overflowProduct(N, D)) {
    cost = min(cost, N * D);
  }
  memo[N] = cost;
  assert(cost > 0);
  return cost;
}

signed main() {
  int T;
  cin >> T;
  rep(ti, 0, T) {
    ll N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    unordered_map<ll, ll> memo;
    memo[0] = 0;
    memo[1] = D;
    cout << dfs(N, A, B, C, D, memo) << endl;
  }
  return 0;
}