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

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a) / (b) + (((a) % (b)) ? 1 : 0)
#define ln '\\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = 1e18;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

vector<LL> Div(LL n) {
  vector<LL> ret;
  for (LL i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

LL N, A, B, C, D;
int ddig;
LL res, t;
map<LL, LL> mp;

int Dig(LL num) {
  int dig = 0;
  while (num > 0) {
    num /= 10;
    ++dig;
  }

  return dig;
}

void dfs(LL n, LL cost) {
  if (n == 1) {
    if (cost == -1)
      cost = 0;
    res = min(res, cost + D);
    return;
  }
  if (res <= cost || (mp[n] != 0 && mp[n] <= cost))
    return;
  mp[n] = cost;
  if (cost == -1)
    cost = 0;

  for (LL i = max(2ll, n - 4); i <= n + 4; ++i) {
    if (i % 5 == 0) {
      t = LINF;
      if (Dig(i - (i / 5)) + ddig < 10)
        t = (i - i / 5) * D;
      dfs(i / 5, cost + min(C, t) + D * llabs(i - n));
    }
  }
  for (LL i = max(2ll, n - 2); i <= n + 2; ++i) {
    if (i % 3 == 0) {
      t = LINF;
      if (Dig(i - (i / 3)) + ddig < 10)
        t = (i - i / 3) * D;
      dfs(i / 3, cost + min(B, t) + D * llabs(i - n));
    }
  }
  for (LL i = max(2ll, n - 1); i <= n + 1; ++i) {
    if (i % 2 == 0) {
      t = LINF;
      if (Dig(i - (i / 2)) + ddig < 10)
        t = (i - i / 2) * D;
      dfs(i / 2, cost + min(A, t) + D * llabs(i - n));
    }
  }

  if (Dig(n) + ddig < 16)
    res = min(res, cost + n * D);

  return;
}

void solve() {
  cin >> N >> A >> B >> C >> D;

  mp.clear();
  res = LINF;
  ddig = Dig(D);
  dfs(N, -1);

  cout << res << endl;
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int T;
  cin >> T;
  while (T--) {
    solve();
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a) / (b) + (((a) % (b)) ? 1 : 0)
#define ln '\\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};

vector<LL> Div(LL n) {
  vector<LL> ret;
  for (LL i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.pb(i);
      if (i * i != n)
        ret.pb(n / i);
    }
  }
  sort(all(ret));
  return ret;
}

LL dp_solve(vector<int> v) {
  LP dp[4][64];
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 64; ++j) {
      dp[i][j].F = LINF;
    }
  }

  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 60; ++j) {
    }
  }
}

void solve() {
  LL N, A, B, C, D;
  cin >> N >> A >> B >> C >> D;

  LL res = LINF;
  vector<int> v = {2, 3, 5};

  do {
    res = min(res, dp_solve(v));
  } while (next_permutation(all(v)));
  cout << res << endl;

  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int T;
  cin >> T;
  while (T--) {
    solve();
  }

  return 0;
}