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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define itn int
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; i--)
#define all(vec) vec.begin(), vec.end()
#define sortt(vec) sort((vec).begin(), (vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 2e9;
const ll lnf = 9e18;

ll kiri(ll a, ll b) { return a % b == 0 ? a / b : a / b + 1; }

ll a, b, c, d;
map<ll, ll> mp;

ll dfs(ll n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (mp.count(n))
    return mp[n];
  ll res = 1e18;
  if (n < res / d)
    res = n * d;
  ll ret = min({res, d * abs(n - n / 5 * 5) + c + dfs(n / 5),
                d * abs(n - kiri(n, 5) * 5) + c + dfs(kiri(n, 5)),
                d * abs(n - n / 3 * 3) + b + dfs(n / 3),
                d * abs(n - kiri(n, 3) * 3) + b + dfs(kiri(n, 3)),
                d * abs(n - n / 2 * 2) + a + dfs(n / 2),
                d * abs(n - kiri(n, 2) * 2) + a + dfs(kiri(n, 2))});
  mp[n] = ret;
  return ret;
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  ll T;
  cin >> T;
  vector<ll> ans(0);
  rep(z, T) {
    mp.clear();
    ll n;
    cin >> n >> a >> b >> c >> d;
    ;
    ans.push_back(dfs(n));
  }
  rep(i, T) { cout << ans[i] << endl; }
}

Error: -11

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

ll n, a, b, c, d;
map<ll, ll> mp;
void calc(ll x) {
  for (int i = -4; i <= 4; i++) {
    if (x + i < 0)
      continue;
    if (mp[x + i] != 0)
      chmin(mp[x + i], mp[x] + abs(i) * d);
    else
      mp[x + i] = mp[x] + abs(i) * d;
    if (x == 1 && x + i == 0)
      return;
    if ((x + i) % 2 == 0) {
      if (mp[(x + i) / 2] != 0)
        chmin(mp[(x + i) / 2], mp[x + i] + a);
      else
        mp[(x + i) / 2] = mp[x + i] + a;
      calc((x + i) / 2);
    }
    if ((x + i) % 3 == 0) {
      if (mp[(x + i) / 3] != 0)
        chmin(mp[(x + i) / 3], mp[x + i] + b);
      else
        mp[(x + i) / 3] = mp[x + i] + b;
      calc((x + i) / 3);
    }
    if ((x + i) % 5 == 0) {
      if (mp[(x + i) / 5] != 0)
        chmin(mp[(x + i) / 5], mp[x + i] + c);
      else
        mp[(x + i) / 5] = mp[x + i] + c;
      calc((x + i) / 5);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll T;
  cin >> T;
  while (T--) {
    map<ll, ll> mp;
    cin >> n >> a >> b >> c >> d;
    mp[0] = n * d;
    calc(n);
    cout << mp[0] << endl;
  }
}