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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define FAST                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

map<LLONG, LLONG> num2MinCostMap;
LLONG costs[3];
LLONG ks[3];
LLONG N, D;

const LLONG DFS(const LLONG n) {
  if (num2MinCostMap.count(n))
    return num2MinCostMap[n];
  LLONG minCost = log10(n) * log10(D) < 18.0 ? n * D : 9e18;
  for (int i = 0; i < 3; ++i) {
    LLONG k = ks[i];
    LLONG lower = n / k;
    LLONG upper = lower + 1;
    minCost = min(minCost, min(DFS(lower) + costs[i] + (n - lower * k) * D,
                               DFS(upper) + costs[i] + (upper * k - n) * D));
  }
  return num2MinCostMap[n] = minCost;
}

void Solve() {
  cin >> N >> costs[0] >> costs[1] >> costs[2] >> D;
  num2MinCostMap.clear();
  num2MinCostMap[0] = 0;
  num2MinCostMap[1] = D;
  num2MinCostMap[2] = min(D * 2, D + costs[0]);
  ks[0] = 2, ks[1] = 3, ks[2] = 5;
  cout << DFS(N) << endl;
}

int main() {
  FAST;
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i) {
    Solve();
  }
}


Error: -4

Fixed submission:

#pragma GCC target("avx")
#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define FAST                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

map<LLONG, LLONG> num2MinCostMap;
LLONG N, A, B, C, D;

LLONG Digit(LLONG n) {
  LLONG ret = 0;
  while (n) {
    n /= 10;
    ++ret;
  }
  return ret;
}

map<LLONG, LLONG> m;
LLONG calc(LLONG n) {
  // cout<<n<<endl;
  if (m.count(n))
    return m[n];
  LLONG x = n / 2, y = n / 3, z = n / 5;
  vector<LLONG> v(7);
  v[0] = calc(x) + (n - x * 2) * D + A,
  v[1] = calc(x + 1) + ((x + 1) * 2 - n) * D + A;
  v[2] = calc(y) + (n - y * 3) * D + B,
  v[3] = calc(y + 1) + ((y + 1) * 3 - n) * D + B;
  v[4] = calc(z) + (n - z * 5) * D + C,
  v[5] = calc(z + 1) + ((z + 1) * 5 - n) * D + C;
  if (Digit(n) + Digit(D) > 18)
    v[6] = 1.1e18;
  else
    v[6] = n * D;
  // for(LL l:v)cout<<l<<" ";
  // cout<<endl;
  sort(v.begin(), v.end());
  // cout<<n<<endl;
  return m[n] = v[0];
}

// LLONG DFS(LLONG n)
//{
//     if (num2MinCostMap.count(n)) return num2MinCostMap[n];
//     LLONG x = n / 2, y = n / 3, z = n / 5;
//     vector<LLONG> vs(7);
//     vs[0] = DFS(x) + (n - x * 2) * D + A;
//     vs[1] = DFS(x + 1) + ((x + 1) * 2 - n) * D + A;
//     vs[2] = DFS(y) + (n - y * 3) * D + B;
//     vs[3] = DFS(y + 1) + ((y + 1) * 3 - n) * D + B;
//     vs[4] = DFS(z) + (n - z * 5) * D + C;
//     vs[5] = DFS(z + 1) + ((z + 1) * 5 - n) * D + C;
//     vs[6] = Digit(n) * Digit(D) > 18 ? 1145141919810364364LL : n * D;
//     sort(vs.begin(), vs.end());
//     return num2MinCostMap[n] = vs[0];
// }

int main() {
  FAST;
  int T;
  cin >> T;
  for (int i = 0; i < T; ++T) {
    cin >> N >> A >> B >> C >> D;
    num2MinCostMap.clear();
    num2MinCostMap[0] = 0;
    num2MinCostMap[1] = D;
    num2MinCostMap[2] = min(D * 2, D + A);
    cout << calc(N) << endl;
  }
}