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

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define EPS (1e-7)
#define INF (1e11)
#define PI (acos(-1))

typedef long long unsigned int ull;
typedef long long int ll;
typedef pair<ll, ll> P;

ll T, N, A, B, C, D;

map<ll, ll> cost;
map<ll, bool> seen;

ll dfs(ll num) {
  if (seen[num])
    return cost[num];
  seen[num] = true;
  cost[num] = INF;

  int by[3] = {2, 3, 5};
  ll ABC[3] = {A, B, C};
  ll dD[6];
  ll next[6];
  REP(i, 3) {
    next[2 * i] = num / by[i];
    next[2 * i + 1] = num / by[i] + 1;
    dD[2 * i] = (num - by[i] * next[2 * i]) * D;
    dD[2 * i + 1] = (next[2 * i + 1] * by[i] - num) * D;
  }
  REP(i, 6) {
    if ((ABC[i / 2] + dD[i]) / D < num - next[i]) {
      cost[num] = min(cost[num], dfs(next[i]) + ABC[i / 2] + dD[i]);
    } else {
      cost[num] = min(cost[num], dfs(next[i]) + (num - next[i]) * D);
    }
  }
  // cout << "dfs(" << num << ")=" << cost[num] <<endl;
  return cost[num];
}
int main() {
  cin >> T;
  ll n[T], a[T], b[T], c[T], d[T];
  REP(t, T) { cin >> n[t] >> a[t] >> b[t] >> c[t] >> d[t]; }
  REP(t, T) {
    N = n[t];
    A = a[t];
    B = b[t];
    C = c[t];
    D = d[t];
    cost.clear();
    seen.clear();
    cost[0] = 0;
    seen[0] = true;
    cost[1] = D;
    seen[1] = true;
    cout << dfs(N) << endl;
  }
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ull;
typedef long long int ll;
typedef pair<ll, ll> P;

ll T, N, A, B, C, D;

vector<ll> cost;
vector<bool> seen;

ll dfs(ll num) {
  if (seen[num])
    return cost[num];
  seen[num] = true;
  ll a = INF;

  int by[3] = {2, 3, 5};
  ll ABC[3] = {A, B, C};
  ll dD[6];
  ll next[6];
  REP(i, 3) {
    next[2 * i] = floor(num / by[i]);
    next[2 * i + 1] = ceil(num / by[i]);
    dD[2 * i] = (num - by[i] * floor((double)num / by[i])) * D;
    dD[2 * i + 1] = (by[i] * ceil((double)num / by[i]) - num) * D;
  }
  REP(i, 6) { cost[num] = min(cost[num], dfs(next[i]) + ABC[i / 2] + dD[i]); }
  // cout << "dfs(" << num << ")=" << cost[num] <<endl;
  return cost[num];
}
int main() {
  cin >> T;
  ll n[T], a[T], b[T], c[T], d[T];
  REP(t, T) { cin >> n[t] >> a[t] >> b[t] >> c[t] >> d[t]; }
  REP(t, T) {
    N = n[t];
    A = a[t];
    B = b[t];
    C = c[t];
    D = d[t];
    seen.assign(N, false);
    cost.assign(N, INF);
    cost[0] = 0;
    seen[0] = true;
    cost[1] = D;
    seen[1] = true;
    cout << dfs(N) << endl;
  }
}