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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

const ll INF = (1LL << 61);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

ll divide(ll x, ll y, ll w, ll D) {
  if (x % y != 0) {
    abort();
  }
  if (x / y * (y - 1) >= w / D)
    return w;
  return min(x / y * (y - 1) * D, w);
}
ll dd(ll x, ll y) { return x / y + (x % y > 0 ? 1 : 0); }
ll A, B, C, D;
ll solve(ll N) {
  if (N == 0)
    return 0;
  if (N == 1)
    return D;
  priority_queue<pll, vector<pll>, greater<pll>> q;
  set<ll> visited;
  q.push(pll(0, N));
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    ll cost = cur.first, num = cur.second;
    if (visited.count(num))
      continue;
    visited.insert(num);
    if (num == 0)
      return cost;
    auto push = [&](pll x) {
      if (visited.count(x.second) == 0) {
        q.push(x);
      }
    };
    if (num <= LLONG_MAX / D && num * D <= LLONG_MAX - cost) {
      push(pll(cost + num * D, 0));
    }
    if (num >= 2)
      push(pll(cost + divide(num / 2 * 2, 2, A, D) + num % 2 * D, num / 2));
    if (num % 2)
      push(pll(cost + divide(num / 2 * 2 + 2, 2, A, D) + D * (2 - num % 2),
               num / 2 + 1));
    if (num >= 3)
      push(pll(cost + divide(num / 3 * 3, 3, B, D) + num % 3 * D, num / 3));
    if (num % 3)
      push(pll(cost + divide(num / 3 * 3 + 3, 3, B, D) + D * (3 - num % 3),
               num / 3 + 1));
    if (num >= 5)
      push(pll(cost + divide(num / 5 * 5, 5, C, D) + num % 5 * D, num / 5));
    if (num % 5)
      push(pll(cost + divide(num / 5 * 5 + 5, 5, C, D) + D * (5 - num % 5),
               num / 5 + 1));
  }
}
int main() {
  cout << setprecision(1000);
  ll T;
  cin >> T;
  For(times, T) {
    ll N;
    cin >> N >> A >> B >> C >> D;
    ll ans = solve(N);
    cout << ans << endl;
  }
  return 0;
}


Error: -6

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

const ll INF = (1LL << 61);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]";
  return os;
}

ll divide(ll x, ll y, ll w, ll D) {
  if (x % y != 0) {
    abort();
  }
  if (x / y * (y - 1) >= w / D)
    return w;
  return min(x / y * (y - 1) * D, w);
}
ll dd(ll x, ll y) { return x / y + (x % y > 0 ? 1 : 0); }
ll A, B, C, D;
ll solve(ll N) {
  if (N == 0)
    return 0;
  if (N == 1)
    return D;
  priority_queue<pll, vector<pll>, greater<pll>> q;
  set<ll> visited;
  q.push(pll(0, N));
  while (!q.empty()) {
    auto cur = q.top();
    q.pop();
    ll cost = cur.first, num = cur.second;
    if (visited.count(num))
      continue;
    visited.insert(num);
    if (num == 0)
      return cost;
    auto push = [&](pll x) {
      if (visited.count(x.second) == 0) {
        q.push(x);
      }
    };
    if (num < 5) {
      push(pll(cost + num * D, 0));
    }
    if (num >= 2)
      push(pll(cost + divide(num / 2 * 2, 2, A, D) + num % 2 * D, num / 2));
    if (num % 2)
      push(pll(cost + divide(num / 2 * 2 + 2, 2, A, D) + D, num / 2 + 1));
    if (num >= 3)
      push(pll(cost + divide(num / 3 * 3, 3, B, D) + num % 3 * D, num / 3));
    if (num % 3)
      push(pll(cost + divide(num / 3 * 3 + 3, 3, B, D) + D, num / 3 + 1));
    if (num >= 5)
      push(pll(cost + divide(num / 5 * 5, 5, C, D) + num % 5 * D, num / 5));
    if (num % 5)
      push(pll(cost + divide(num / 5 * 5 + 5, 5, C, D) + D, num / 5 + 1));
  }
}
int main() {
  cout << setprecision(1000);
  ll T;
  cin >> T;
  For(times, T) {
    ll N;
    cin >> N >> A >> B >> C >> D;
    ll ans = solve(N);
    abort();
    if (ans < 0) {
    }
    cout << ans << endl;
  }
  return 0;
}