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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (ll i = (ll)from; i < (ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N;
  cin >> N;
  ll X[N], Y[N], P[N];
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];
  ll xmin[N][1 << N], ymin[N][1 << N];
  rep(i, 1 << N) rep(j, N) {
    xmin[j][i] = abs(X[j]);
    rep(k, N) if (i & 1 << k) xmin[j][i] = min(xmin[j][i], abs(X[k] - X[j]));
    ymin[j][i] = abs(Y[j]);
    rep(k, N) if (i & 1 << k) ymin[j][i] = min(ymin[j][i], abs(Y[k] - Y[j]));
  }
  // rep(i,N) debug(xmin[i],xmin[i]+(1<<N));

  vector<ll> result(N + 1, LLINF);
  rep(i, pow(3, N)) {
    ll ti = i, dx = 0, dy = 0, count = 0;
    rep(j, N) {
      if (ti % 3 == 1) {
        dx += 1 << j;
        count++;
      } else if (ti % 3 == 2) {
        dy += 1 << j;
        count++;
      }
      ti /= 3;
    }
    ll sum = 0;
    rep(j, N) sum += min(xmin[j][dx], ymin[j][dy]) * P[j];
    result[count] = min(result[count], sum);
  }
  rep(i, N + 1) cout << result[i] << endl;
  return 0;
}

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 <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (ll i = (ll)from; i < (ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll N;
vector<ll> dp, X, Y, P, result;

void dfs(vector<ll> mi, ll n = 0, ll d = 0, ll p3d = 1, ll used = 0) {
  // 1つ前について
  if (d) {
    if (n % (p3d / 3) == 1) {
      mi[d - 1] = 0;
      if (d >= 2)
        mi[d] = min(mi[d - 2], abs(X[d - 2] - X[d - 1]) * P[d - 2]);
      else if (d < N)
        mi[d] = min(mi[d], abs(X[d] - X[d - 1]) * P[d]);
    }
    if (n % (p3d / 3) == 2) {
      mi[d - 1] = 0;
      if (d >= 2)
        mi[d] = min(mi[d - 2], abs(Y[d - 2] - Y[d - 1]) * P[d - 2]);
      else if (d < N)
        mi[d] = min(mi[d], abs(Y[d] - Y[d - 1]) * P[d]);
    }
  }

  if (d < N)
    rep(i, 3) dfs(mi, n + p3d * i, d + 1, p3d * 3, used + (i != 0));
  else {
    ll sum = 0;
    rep(i, N) sum += mi[i];
    result[used] = min(result[used], sum);
    // cout << n << " " << used << endl;
  }
}

int main() {
  cin >> N;
  X.resize(N);
  Y.resize(N);
  P.resize(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];
  ll p3n = pow(3, N), nd = 0, np = 1;
  dp.resize(p3n);
  result.resize(N + 1, LLINF);

  vector<ll> mi(N);
  rep(i, N) mi[i] = min(X[i], Y[i]) * P[i];
  dfs(mi);
  // debug(all(dp));
  rep(i, N + 1) cout << result[i] << endl;
  return 0;
}