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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
ll a, b, c, d;
map<ll, ll> ma;
ll solve(ll now) {
  if (ma.find(now) != ma.end()) {
    return ma[now];
  }
  ll res;
  ll k;
  if (now > inf * 2) {
    k = 2;
    if (now % k == 0) {
      res = solve(now / k) + a;
    } else {
      res = solve(now / k) + a + now % k * d;
      res = min(solve(now / k + 1) + a + (k - now % k) * d, res);
    }
  } else {
    res = now * d;
  }
  if (now / 2 > 0) {
    k = 2;
    if (now % k == 0) {
      res = min(solve(now / k) + a, res);
    } else {
      res = min(solve(now / k) + a + now % k * d, res);
      res = min(solve(now / k + 1) + a + (k - now % k) * d, res);
    }
  }
  if (now / 3 > 0) {
    k = 3;
    if (now % k == 0) {
      res = min(solve(now / k) + b, res);
    } else {
      res = min(solve(now / k) + b + now % k * d, res);
      res = min(solve(now / k + 1) + b + (k - now % k) * d, res);
    }
  } else if (now > 1) {
    k = 3;
    res = min(solve(now / k + 1) + b + (k - now % k) * d, res);
  }
  if (now / 5 > 0) {
    k = 5;
    if (now % k == 0) {
      res = min(solve(now / k) + c, res);
    } else {
      res = min(solve(now / k) + c + now % k * d, res);
      res = min(solve(now / k + 1) + c + (k - now % k) * d, res);
    }
  } else if (now > 1) {
    k = 5;
    res = min(solve(now / k + 1) + c + (k - now % k) * d, res);
  }
  ma[now] = res;
  return res;
}
signed main() {
  ll t;
  cin >> t;
  while (t) {
    t--;
    ll n;
    cin >> n >> a >> b >> c >> d;
    ma.clear();
    ma[0] = 0;
    ll ans = solve(n);
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
ll a, b, c, d;
map<ll, ll> ma;
ll solve(ll now) {
  if (ma.find(now) != ma.end()) {
    return ma[now];
  }
  ll res;
  ll k;
  if (now > inf) {
    k = 2;
    if (now % k == 0) {
      res = solve(now / k) + a;
    } else {
      res = solve(now / k) + a + now % k * d;
      res = min(solve(now / k + 1) + a + (k - now % k) * d, res);
    }
  } else {
    res = now * d;
  }
  if (now / 2 > 0) {
    k = 2;
    if (now % k == 0) {
      res = min(solve(now / k) + a, res);
    } else {
      res = min(solve(now / k) + a + now % k * d, res);
      res = min(solve(now / k + 1) + a + (k - now % k) * d, res);
    }
  }
  if (now / 3 > 0) {
    k = 3;
    if (now % k == 0) {
      res = min(solve(now / k) + b, res);
    } else {
      res = min(solve(now / k) + b + now % k * d, res);
      res = min(solve(now / k + 1) + b + (k - now % k) * d, res);
    }
  } else {
    k = 3;
    res = min(solve(now / k + 1) + b + (k - now % k) * d, res);
  }
  if (now / 5 > 0) {
    k = 5;
    if (now % k == 0) {
      res = min(solve(now / k) + c, res);
    } else {
      res = min(solve(now / k) + c + now % k * d, res);
      res = min(solve(now / k + 1) + c + (k - now % k) * d, res);
    }
  } else {
    k = 5;
    res = min(solve(now / k + 1) + c + (k - now % k) * d, res);
  }
  ma[now] = res;
  return res;
}
signed main() {
  ll t;
  cin >> t;
  while (t) {
    t--;
    ll n;
    cin >> n >> a >> b >> c >> d;
    ma.clear();
    ma[0] = 0;
    ll ans = solve(n);
    cout << ans << endl;
  }
}