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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ull)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int T;
void input() {
  fast_io();
  cin >> T;
}

unordered_map<ull, ull> dp;
ull dfs(ull x, ull a, ull b, ull c, ull d) {
  if (x == 0)
    return 0;
  if (x == 1)
    return d;
  if (x == 2)
    return min(2 * d, a + d);
  if (dp[x])
    return dp[x];
  ull ret = 1e18;
  ull r3 = x % 3;
  ull r5 = x % 5;
  ret = min(ret, d * x);
  if (x % 2 == 0)
    ret = min(ret, a + dfs(x / 2, a, b, c, d));
  else {
    ret = min(ret, a + d + dfs((x + 1) / 2, a, b, c, d));
    ret = min(ret, a + d + dfs((x - 1) / 2, a, b, c, d));
  }
  if (x % 3 == 0)
    ret = min(ret, b + dfs(x / 3, a, b, c, d));
  else {
    ret = min(ret, b + d * (3 - r3) + dfs((x + 3 - r3) / 3, a, b, c, d));
    ret = min(ret, b + d * r3 + dfs((x - r3) / 3, a, b, c, d));
  }
  if (x % 5 == 0)
    ret = min(ret, c + dfs(x / 5, a, b, c, d));
  else {
    ret = min(ret, c + d * (5 - r5) + dfs((x + 5 - r5) / 5, a, b, c, d));
    ret = min(ret, c + d * r5 + dfs((x - r5) / 5, a, b, c, d));
  }
  return dp[x] = ret;
}

int solve() {
  for (int i = 0; i < T; i++) {
    ull n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    cout << dfs(n, a, b, c, d) << endl;
    dp.clear();
  }

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int T;
void input() {
  fast_io();
  cin >> T;
}
unordered_map<ll, ll> dp;
ll rec(ll x, ll a, ll b, ll c, ll d) {
  if (x == 0)
    return 0;
  if (x == 1)
    return d;
  if (x == 2)
    return min(2 * d, a + d);
  if (dp[x])
    return dp[x];
  int r2 = x % 2;
  int r3 = x % 3;
  int r5 = x % 5;
  vector<pll> e;
  e.emplace_back(d, x - 1);
  if (x % 2 == 0)
    e.emplace_back(a, x / 2);
  else {
    e.emplace_back(a + d, (x - 1) / 2);
    e.emplace_back(a + d, (x + 1) / 2);
  }
  if (x % 3 == 0)
    e.emplace_back(b, x / 3);
  else {
    e.emplace_back(b + d * r3, (x - r3) / 3);
    e.emplace_back(b + d * (3 - r3), (x + 3 - r3) / 3);
  }
  if (x % 5 == 0)
    e.emplace_back(c, x / 5);
  else {
    e.emplace_back(c + d * r5, (x - r5) / 5);
    e.emplace_back(c + d * (5 - r5), (x + 5 - r5) / 5);
  }
  // sort(e.begin(),e.end(),[&](pll a, pll b){return
  // a.first*(x-b.second)<b.first*(x-a.second);});
  DBG(e)
  ll ret = 1e18;
  for (auto elem : e)
    ret = min(ret, elem.first + rec(elem.second, a, b, c, d));

  // ret = min(ret,d+rec(x-1,a,b,c,d));
  // if(x%2==0) ret = min(ret,a+rec(x/2,a,b,c,d));
  // if(x%3==0) ret = min(ret,b+rec(x/3,a,b,c,d));
  // if(x%5==0) ret = min(ret,c+rec(x/5,a,b,c,d));
  return dp[x] = ret;
}

ll dijkstra(ll x, ll a, ll b, ll c, ll d) {
  auto cmp = [](pll a, pll b) { return a.first > b.first; };
  priority_queue<pll, vector<pll>, decltype(cmp)> pq(cmp);
  pq.emplace(d, 1);
  unordered_map<ll, ll> dist;
  unordered_map<ll, ll> seen;
  dist[0] = 0;
  dist[1] = d;
  seen[0] = seen[1] = true;
  while (!pq.empty()) {
    ll cost, cur;
    tie(cost, cur) = pq.top();
    pq.pop();
    DBG(make_pair(cost, cur))
    if (cur == x)
      return cost;
    if (dist[cur] < cost)
      continue;
    ll nc, nt;
    if (cur > x) {
      dist[x] = cost + (cur - x) * d;
      pq.emplace(cost + (cur - x) * d, x);
    } else {
      nc = cost + d;
      nt = cur - 1;
      if (dist[nt] > nc || !seen[nt]) {
        seen[nt] = true;
        dist[nt] = nc;
        pq.emplace(nc, nt);
      }
    }
    if (cur > x)
      continue;
    nc = cost + d;
    nt = cur + 1;
    if (dist[nt] > nc || !seen[nt]) {
      seen[nt] = true;
      dist[nt] = nc;
      pq.emplace(nc, nt);
    }
    nc = cost + a;
    nt = cur * 2;
    if (dist[nt] > nc || !seen[nt]) {
      seen[nt] = true;
      dist[nt] = nc;
      pq.emplace(nc, nt);
    }
    nc = cost + b;
    nt = cur * 3;
    if (dist[nt] > nc || !seen[nt]) {
      seen[nt] = true;
      dist[nt] = nc;
      pq.emplace(nc, nt);
    }
    nc = cost + c;
    nt = cur * 5;
    if (dist[nt] > nc || !seen[nt]) {
      seen[nt] = true;
      dist[nt] = nc;
      pq.emplace(nc, nt);
    }
  }
  return 0;
}

int solve() {
  for (int i = 0; i < T; i++) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    cout << rec(n, a, b, c, d) << endl;
  }

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}