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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vector<vector<ll>> dx(1 << n, vector<ll>(n, LINF));
  vector<vector<ll>> dy(1 << n, vector<ll>(n, LINF));
  rep(bit, 1 << n) rep(j, n) {
    dx[bit][j] = abs(x[j]) * p[j];
    dy[bit][j] = abs(y[j]) * p[j];
  }
  rep(bit, 1 << n) {
    rep(i, n) if (bit >> i & 1) {
      rep(j, n) {
        chmin(dx[bit][j], p[j] * abs(x[i] - x[j]));
        chmin(dy[bit][j], p[j] * abs(y[i] - y[j]));
      }
    }
  }

  int MAX = 1;
  rep(i, n) MAX *= 3;
  auto num = [&](int i, int bit) {
    rep(_, i) bit /= 3;
    return bit % 3;
  };

  vector<ll> ans(n + 1, LINF);
  rep(bit, MAX) {
    int xbit = 0, ybit = 0;
    int ppcnt = 0;
    rep(i, n) {
      int kk = num(i, bit);
      if (kk == 1)
        xbit += 1 << i, ppcnt++;
      if (kk == 2)
        ybit += 1 << i, ppcnt++;
    }
    ll now = 0;
    rep(j, n) { now += min(dx[xbit][j], dy[ybit][j]); }
    chmin(ans[ppcnt], now);
  }
  rep(i, n + 1) cout << ans[i] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vector<vector<ll>> dx(1 << n, vector<ll>(n, LINF));
  vector<vector<ll>> dy(1 << n, vector<ll>(n, LINF));
  rep(bit, 1 << n) rep(j, n) {
    dx[bit][j] = abs(x[j]) * p[j];
    dy[bit][j] = abs(y[j]) * p[j];
  }
  rep(bit, 1 << n) {
    rep(i, n) if (bit >> i & 1) {
      rep(j, n) {
        chmin(dx[bit][j], p[j] * abs(x[i] - x[j]));
        chmin(dy[bit][j], p[j] * abs(y[i] - y[j]));
      }
    }
  }

  ll MAX = 1;
  rep(i, n) MAX *= 3;

  vector<vector<int>> memo(1 << n, vector<int>(n, -1));
  auto num = [&](int i, int bit) {
    if (memo[bit][i] != -1)
      return memo[bit][i];
    rep(_, i) bit /= 3;
    return memo[bit][i] = bit % 3;
  };

  vector<ll> ans(n + 1, LINF);
  rep(bit, MAX) {
    ll xbit = 0, ybit = 0;
    int ppcnt = 0;
    rep(i, n) {
      if (num(i, bit) == 1)
        xbit += 1 << i, ppcnt++;
      if (num(i, bit) == 2)
        ybit += 1 << i, ppcnt++;
    }
    ll now = 0;
    rep(j, n) { now += min(dx[xbit][j], dy[ybit][j]); }
    chmin(ans[ppcnt], now);
  }
  rep(i, n + 1) cout << ans[i] << endl;
}