Problem p02604 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int N;
  cin >> N;
  ll x[N], y[N], p[N];
  pii q1[N], q2[N];
  rep(i, N) { cin >> x[i] >> y[i] >> p[i]; }

  ll dx[1 << N][N], dy[1 << N][N];
  rep(i, 1 << N) {
    rep(j, N) {
      dx[i][j] = abs(x[j]);
      rep(k, N) {
        if (i & (1 << k))
          chmin(dx[i][j], abs(x[j] - x[k]));
      }
    }
  }
  rep(i, 1 << N) {
    rep(j, N) {
      dy[i][j] = abs(y[j]);
      rep(k, N) {
        if (i & (1 << k))
          chmin(dy[i][j], abs(y[j] - y[k]));
      }
    }
  }
  int pw[N + 1];
  pw[0] = 1;
  rep(i, N) pw[i + 1] = pw[i] * 3;

  ll ans[N + 1];
  fill(ans, ans + N + 1, INF);
  rep(i, pw[N]) {
    int cnt = 0, xs = 0, ys = 0;
    rep(j, N) {
      int k = i % pw[j + 1] / pw[j];
      if (k == 1)
        cnt++, xs |= (1 << j);
      if (k == 2)
        cnt++, ys |= (1 << j);
    }
    ll tmp = 0;
    rep(j, N) tmp += p[j] * min(dx[xs][j], dy[ys][j]);
    chmin(ans[cnt], tmp);
  }

  rep(i, N + 1) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

int main() {
  int N;
  cin >> N;
  int x[N], y[N];
  ll p[N];
  pii q1[N], q2[N];
  rep(i, N) {
    cin >> x[i] >> y[i] >> p[i];
    q1[i] = pii(x[i], i), q2[i] = pii(y[i], i);
  }
  sort(q1, q1 + N), sort(q2, q2 + N);
  int px[N], qx[N], py[N], qy[N];
  rep(i, N) {
    int j = q1[i].second, k = q2[i].second;
    px[i] = j, qx[j] = i, py[i] = k, qy[k] = i;
  }
  // rep(i, N) cout << qx[i] << ' '; cout << endl;
  int pw[N + 1];
  pw[0] = 1;
  rep(i, N) pw[i + 1] = pw[i] * 3;

  ll ans[N + 1];
  fill(ans, ans + N + 1, INF);
  bool ux[N], uy[N];
  int dst[N];
  rep(i, pw[N]) {
    int cnt = 0;
    fill(ux, ux + N, false), fill(uy, uy + N, false);
    rep(j, N) {
      int k = i % pw[j + 1] / pw[j];
      if (k == 1)
        cnt++, ux[qx[j]] = true;
      if (k == 2)
        cnt++, uy[qy[j]] = true;
    }
    rep(j, N) dst[j] = min(abs(x[j]), abs(y[j]));
    int l = -inf;
    rep(j, N) {
      int k = px[j];
      if (ux[j])
        l = x[k];
      chmin(dst[k], x[k] - l);
    }
    int r = inf;
    rep3(j, N - 1, 0) {
      int k = px[j];
      if (ux[j])
        r = x[k];
      chmin(dst[k], r - x[k]);
    }
    int d = -inf;
    rep(j, N) {
      int k = py[j];
      if (uy[j])
        d = y[k];
      chmin(dst[k], y[k] - d);
    }
    int u = inf;
    rep3(j, N - 1, 0) {
      int k = py[j];
      if (uy[j])
        u = y[k];
      chmin(dst[k], u - y[k]);
    }
    ll tmp = 0;
    rep(i, N) tmp += p[i] * dst[i];
    chmin(ans[cnt], tmp);
  }
  rep(i, N + 1) cout << ans[i] << endl;
}