Problem p02604 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

const ll mod = 1000000007;

template <typename T> static inline void chmin(T &ref, const T value) {
  if (ref > value)
    ref = value;
}

template <typename T> static inline void chmax(T &ref, const T value) {
  if (ref < value)
    ref = value;
}

vector<ll> pow3() {
  vector<ll> ans;
  ll b = 1;
  for (int i = 0; i <= 15; i++) {
    ans.push_back(b);
    b *= 3;
  }
  return ans;
}

inline int pop3(ll j) {
  int a = 0;
  while (j) {
    if (j % 3)
      a++;
    j /= 3;
  }
  return a;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> xs(n), ys(n), ps(n);
  rep(i, n) { cin >> xs[i] >> ys[i] >> ps[i]; }
  vector<vector<ll>> dxs(n, vector<ll>(n));
  vector<vector<ll>> dys(n, vector<ll>(n));
  rep(i, n) {
    rep(j, n) {
      dxs[i][j] = min(abs(xs[i] - xs[j]), abs(xs[i]));
      dys[i][j] = min(abs(ys[i] - ys[j]), abs(ys[i]));
    }
  }
  const auto table = pow3();
  vector<int> popcounts(table[n]);
  for (ll j = 0; j < table[n]; j++) {
    popcounts[j] = pop3(j);
  }
  vector<ll> nxs(table[n], 0);
  vector<ll> nys(table[n], 0);
  for (ll j = 0; j < table[n]; j++) {
    ll c = j;
    for (int a = 0; a < n && c > 0; a++) {
      if (c % 3 == 1) {
        nxs[j] |= (1LL << a);
      } else if (c % 3 == 2) {
        nys[j] |= (1LL << a);
      }
      c /= 3;
    }
  }
  vector<vector<ll>> xss(n, vector<ll>(1LL << n, 0));
  vector<vector<ll>> yss(n, vector<ll>(1LL << n, 0));
  rep(j, 1LL << n) {
    rep(k, n) {
      ll dx = abs(xs[k]);
      ll dy = abs(ys[k]);
      rep(i, n) {
        if (j & (1LL << i)) {
          dx = min(dx, dxs[k][i]);
          dy = min(dy, dys[k][i]);
        }
      }
      xss[k][j] = dx;
      yss[k][j] = dy;
    }
  }
  vector<ll> memo(table[n]);
  for (ll j = 0; j < table[n]; j++) {
    ll d = 0;
    for (int k = 0; k < n; k++) {
      ll dx = xss[j][nxs[j]];
      ll dy = yss[j][nys[j]];
      d += min(dx, dy) * ps[k];
    }
    memo[j] = d;
  }
  vector<ll> ans(n + 1, INF);
  for (ll j = 0; j < table[n]; j++) {
    ans[popcounts[j]] = min(ans[popcounts[j]], memo[j]);
  }
  for (int i = 0; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

const ll mod = 1000000007;

template <typename T> static inline void chmin(T &ref, const T value) {
  if (ref > value)
    ref = value;
}

template <typename T> static inline void chmax(T &ref, const T value) {
  if (ref < value)
    ref = value;
}

vector<ll> pow3() {
  vector<ll> ans;
  ll b = 1;
  for (int i = 0; i <= 15; i++) {
    ans.push_back(b);
    b *= 3;
  }
  return ans;
}

inline int pop3(ll j) {
  int a = 0;
  while (j) {
    if (j % 3)
      a++;
    j /= 3;
  }
  return a;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> xs(n), ys(n), ps(n);
  rep(i, n) { cin >> xs[i] >> ys[i] >> ps[i]; }
  vector<vector<ll>> dxs(n, vector<ll>(n));
  vector<vector<ll>> dys(n, vector<ll>(n));
  rep(i, n) {
    rep(j, n) {
      dxs[i][j] = min(abs(xs[i] - xs[j]), abs(xs[i]));
      dys[i][j] = min(abs(ys[i] - ys[j]), abs(ys[i]));
    }
  }
  const auto table = pow3();
  vector<int> popcounts(table[n]);
  for (ll j = 0; j < table[n]; j++) {
    popcounts[j] = pop3(j);
  }
  vector<ll> nxs(table[n], 0);
  vector<ll> nys(table[n], 0);
  for (ll j = 0; j < table[n]; j++) {
    ll c = j;
    for (int a = 0; a < n && c > 0; a++) {
      if (c % 3 == 1) {
        nxs[j] |= (1LL << a);
      } else if (c % 3 == 2) {
        nys[j] |= (1LL << a);
      }
      c /= 3;
    }
  }
  vector<vector<ll>> xss(n, vector<ll>(1LL << n, 0));
  vector<vector<ll>> yss(n, vector<ll>(1LL << n, 0));
  rep(j, 1LL << n) {
    rep(k, n) {
      ll dx = abs(xs[k]);
      ll dy = abs(ys[k]);
      rep(i, n) {
        if (j & (1LL << i)) {
          dx = min(dx, dxs[k][i]);
          dy = min(dy, dys[k][i]);
        }
      }
      xss[k][j] = dx;
      yss[k][j] = dy;
    }
  }
  vector<ll> memo(table[n]);
  for (ll j = 0; j < table[n]; j++) {
    ll d = 0;
    for (int k = 0; k < n; k++) {
      ll dx = xss[k][nxs[j]];
      ll dy = yss[k][nys[j]];
      d += min(dx, dy) * ps[k];
    }
    memo[j] = d;
  }
  vector<ll> ans(n + 1, INF);
  for (ll j = 0; j < table[n]; j++) {
    ans[popcounts[j]] = min(ans[popcounts[j]], memo[j]);
  }
  for (int i = 0; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}