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

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define pli pair<long long, int>
#define pil pair<int, long long>
#define ll long long
#define el '\\n'

using namespace std;

const ll INF = 2e18;
const ll BORD = 1e9;

int q;
ll n, a, b, c, d;

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

  cin >> q;
  while (q--) {
    cin >> n >> a >> b >> c >> d;
    map<ll, ll> dp;
    map<ll, bool> vis;

    dp[n] = 0;
    priority_queue<ll> pq;
    pq.push(n);
    while (!pq.empty()) {
      auto temp = pq.top();
      pq.pop();
      if (vis[temp])
        continue;
      vis[temp] = 1;

      ll dpcur = dp[temp];
      for (ll mar = -4; mar <= 4; mar++) {
        ll tam = abs(mar) * d;
        ll ncur = mar + temp;
        if (ncur < 0)
          continue;
        if (ncur == 0) {
          if (dp[ncur] == 0)
            dp[ncur] = dp[temp] + tam;
          else
            dp[ncur] = min(dp[ncur], dp[temp] + tam);
          continue;
        }
        if (ncur % 2 == 0) {
          ll sim = dp[ncur / 2];
          if (sim != 0)
            dp[ncur / 2] = min(sim, dp[temp] + tam + a);
          else
            dp[ncur / 2] = dp[temp] + tam + a;
          pq.push(ncur / 2);
        }
        if (ncur % 3 == 0) {
          ll sim = dp[ncur / 3];
          if (sim != 0)
            dp[ncur / 3] = min(sim, dp[temp] + tam + b);
          else
            dp[ncur / 3] = dp[temp] + tam + b;
          pq.push(ncur / 3);
        }
        if (ncur % 5 == 0) {
          ll sim = dp[ncur / 5];
          if (sim != 0)
            dp[ncur / 5] = min(sim, dp[temp] + tam + c);
          else
            dp[ncur / 5] = dp[temp] + tam + c;
          pq.push(ncur / 5);
        }
      }
    }
    ll ans = INF;
    for (auto x : dp) {
      if (x.fi >= INF / d)
        continue;
      ans = min(ans, x.se + x.fi * d);
    }
    cout << ans << el;
  }

  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define pli pair<long long, int>
#define pil pair<int, long long>
#define ll long long
#define el '\\n'

using namespace std;

const ll INF = 2e18;
const ll BORD = 1e9;

int q;
ll n, a, b, c, d;

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

  cin >> q;
  while (q--) {
    cin >> n >> a >> b >> c >> d;
    map<ll, ll> dp;
    map<ll, bool> vis;

    dp[n] = 0;
    priority_queue<ll> pq;
    pq.push(n);
    while (!pq.empty()) {
      auto temp = pq.top();
      pq.pop();
      if (vis[temp])
        continue;
      vis[temp] = 1;

      ll dpcur = dp[temp];
      for (ll mar = -4; mar <= 4; mar++) {
        ll tam = abs(mar) * d;
        ll ncur = mar + temp;
        if (ncur % 2 == 0) {
          ll sim = dp[ncur / 2];
          if (sim != 0)
            dp[ncur / 2] = min(sim, dp[temp] + tam + a);
          else
            dp[ncur / 2] = dp[temp] + tam + a;
          pq.push(ncur / 2);
        }
        if (ncur % 3 == 0) {
          ll sim = dp[ncur / 3];
          if (sim != 0)
            dp[ncur / 3] = min(sim, dp[temp] + tam + b);
          else
            dp[ncur / 3] = dp[temp] + tam + b;
          pq.push(ncur / 3);
        }
        if (ncur % 5 == 0) {
          ll sim = dp[ncur / 5];
          if (sim != 0)
            dp[ncur / 5] = min(sim, dp[temp] + tam + c);
          else
            dp[ncur / 5] = dp[temp] + tam + c;
          pq.push(ncur / 5);
        }
      }
    }
    ll ans = INF;
    for (auto x : dp) {
      if (x.fi >= INF / x.fi)
        continue;
      ans = min(ans, x.se + x.fi * d);
    }
    cout << ans << el;
  }

  return 0;
}