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

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define int long long
#define ll unsigned long long
#define ld long double
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vvc vector<vector<char>>
#define vc vector<char>
#define vvb vector<vector<bool>>
#define vb vector<bool>
#define elif else if
#define maxe(x) *max_element(all(x))
#define mine(x) *min_element(all(x))
#define Size(x) ((int)(x).size())
const long long INF = 1LL << 60;
const long double pi = 3.1415926535897932;
long long MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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;
}

// mint,UnionFind,modnCr,modpow,soinsubunkai
// yakusurekkyo
ll T, N, A, B, C, D;
map<ll, ll> m;
ll dfs(ll n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return D;
  if (m[n] != 0)
    return m[n];
  ll res = D * n;
  chmin(res, D * (n % 5) + C + dfs(n / 5));
  if (n % 5 != 0)
    chmin(res, D * (5 - n % 5) + C + dfs(n / 5 + 1));
  chmin(res, D * (n % 3) + B + dfs(n / 3));
  if (n % 3 != 0)
    chmin(res, D * (3 - n % 3) + B + dfs(n / 3 + 1));
  chmin(res, D * (n % 2) + A + dfs(n / 2));
  if (n % 2 != 0)
    chmin(res, D * (2 - n % 2) + A + dfs(n / 2 + 1));
  return m[n] = res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> T;
  rep(i, T) {
    cin >> N >> A >> B >> C >> D;
    cout << dfs(N) << endl;
    m.clear();
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ll long long
#define ull unsigned long long
#define ld long double
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vvc vector<vector<char>>
#define vc vector<char>
#define vvb vector<vector<bool>>
#define vb vector<bool>
#define elif else if
#define maxe(x) *max_element(all(x))
#define mine(x) *min_element(all(x))
#define Size(x) ((int)(x).size())
const long long INF = 1LL << 60;
const long double pi = 3.1415926535897932;
long long MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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;
}

// mint,UnionFind,modnCr,modpow,soinsubunkai
// yakusurekkyo
ll T, N, A, B, C, D;
map<ll, ll> m;
ll dfs(ll n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return D;
  if (m[n] != 0)
    return m[n];
  ll res = D * n;
  chmin(res, D * (n % 5) + C + dfs(n / 5));
  chmin(res, D * (5 - n % 5) + C + dfs(n / 5 + 1));
  chmin(res, D * (n % 3) + B + dfs(n / 3));
  chmin(res, D * (3 - n % 3) + B + dfs(n / 3 + 1));
  chmin(res, D * (n % 2) + A + dfs(n / 2));
  chmin(res, D * (2 - n % 2) + A + dfs(n / 2 + 1));
  return m[n] = res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> T;
  rep(i, T) {
    cin >> N >> A >> B >> C >> D;
    cout << dfs(N) << endl;
    m.clear();
  }
}