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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

#define in(s) cin >> s
#define out(s) cout << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define rept(i, a, b) for (int i = a; i < b; i++)
#define pcnt __builtin_popcount
#define pcntll(x) __builtin_popcountll(x)
#define DMAX DBL_MAX;
#define IMIN INT_MIN;
#define IMAX INT_MAX;

// const ll MOD = 1e9+7;
// const ll MOD = 998244353;
// const ll MAX = 3e5+1;
const ll INF = (ll)4e18;
// const ld PI = acos(-1L);
//
ll a, b, c, d;
unordered_map<ll, ll> cache;

ll calc(ll x) {
  if (cache.find(x) != cache.end())
    return cache[x];
  ll ans = INF;
  if ((ld)x * (ld)d < INF)
    ans = x * d;

  if (x % 2 == 0) {
    ll ca = calc(x / 2) + a;
    ans = min(ans, ca);
  } else {
    ll ca = calc(x / 2 + 1) + a + d;
    ans = min(ans, ca);
  }

  if (x % 3 == 0) {
    ll ca = calc(x / 3) + b;
    ans = min(ans, ca);
  } else {
    ll D = x % 3;
    ll U = 3 - D;
    ll ca = min(calc(x / 3) + b + D * d, calc(x / 3 + 1) + b + U * d);
    ans = min(ans, ca);
  }

  if (x % 5 == 0) {
    ll ca = calc(x / 5) + c;
    ans = min(ans, ca);
  } else {
    ll D = x % 5;
    ll U = 5 - D;
    ll ca = min(calc(x / 5) + c + D * d, calc(x / 5 + 1) + c + U * d);
    ans = min(ans, ca);
  }

  // out(x << ' ' << ans);
  return cache[x] = ans;
}

signed main() {
  // cin.tie(nullptr);
  // ios::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  ll T;
  cin >> T;
  while (T--) {
    ll N;
    cin >> N >> a >> b >> c >> d;
    cache.clear();
    cache[0] = 0;
    cache[1] = d;
    ll ans = calc(N);
    out(ans);
  }

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  unordered_map::at


Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

#define in(s) cin >> s
#define out(s) cout << s << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define rept(i, a, b) for (int i = a; i < b; i++)
#define pcnt __builtin_popcount
#define pcntll(x) __builtin_popcountll(x)
#define DMAX DBL_MAX;
#define IMIN INT_MIN;
#define IMAX INT_MAX;

// const ll MOD = 1e9+7;
// const ll MOD = 998244353;
// const ll MAX = 3e5+1;
const ll INF = 1e18;
// const ld PI = acos(-1);

unordered_map<ll, ll> cache;

ll calc(ll n, ll a, ll b, ll c, ll d, ll curr) {
  if (cache.find(n) != cache.end())
    return curr + cache.at(n);
  ll ans = curr + abs(n) * d;

  if (n % 2 == 0) {
    ll ca = calc(n / 2, a, b, c, d, curr + a);
    ans = min(ans, ca);
  }
  if (n % 3 == 0) {
    ll ca = calc(n / 3, a, b, c, d, curr + b);
    ans = min(ans, ca);
  }
  if (n % 5 == 0) {
    ll ca = calc(n / 5, a, b, c, d, curr + c);
    ans = min(ans, ca);
  }
  if (n % 2 != 0 && n % 3 != 0 && n % 5 != 0) {
    for (int i = 1; i < 2; i++) {
      ll ca = calc(n - i, a, b, c, d, curr + d);
      ans = min(ans, ca);
      ca = calc(n + i, a, b, c, d, curr + d);
      ans = min(ans, ca);
    }
  }
  out(n << ' ' << ans - curr);
  cache.insert(make_pair(n, ans - curr));
  return ans;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  int T;
  in(T);
  rep(i, T) {
    ll N, A, B, C, D;
    in(N);
    in(A);
    in(B);
    in(C);
    in(D);
    cache.clear();
    cache.at(0LL) = 0LL;
    cache.at(1LL) = D;
    ll ans = calc(N, A, B, C, D, 0LL);
    out(ans);
  }

  return 0;
}