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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  int s = 1 << n;
  vector<vector<ll>> a(s, vector<ll>(n, INF));
  vector<vector<ll>> b(s, vector<ll>(n, INF));
  rep(i, s) rep(j, n) {
    chmin(a[i][j], abs(x[j]));
    chmin(b[i][j], abs(y[j]));
  }
  rep(i, s) {
    rep(j, n) {
      if ((i >> j) & 1) {
        rep(k, n) {
          chmin(a[i][k], abs(x[j] - x[k]));
          chmin(b[i][k], abs(y[j] - y[k]));
        }
      }
    }
  }
  int sz = 1;
  rep(i, n) sz *= 3;
  vector<ll> ans(n + 1, INF);
  rep(i, sz) {
    int tmp = i;
    int xb = 0, yb = 0;
    int cnt = 0;
    rep(j, n) {
      if (tmp % 3 != 0)
        cnt++;
      if (tmp % 3 == 1)
        xb |= 1 << j;
      if (tmp % 3 == 2)
        yb |= 1 << j;
      tmp /= 3;
    }
    ll sum = 0;
    rep(j, n) { sum += min(a[xb][j], b[yb][j]) * p[j]; }
    chmin(ans[cnt], sum);
  }
  rep(i, n) chmin(ans[i + 1], ans[i]);
  rep(i, n + 1) cout << ans[i] << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << x << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  int sz = 1;
  rep(i, n) sz *= 3;
  vector<ll> ans(n + 1, INF);
  rep(i, sz) {
    int tmp = i;
    vector<int> px(n + 3);
    vector<int> py(n + 3);
    vector<int> check(n, -1);
    rep(j, n) {
      if (tmp % 3 == 0)
        check[j] = j;
      if (tmp % 3 == 1)
        px[j] = x[j];
      if (tmp % 3 == 2)
        py[j] = y[j];
      tmp /= 3;
    }
    px[n + 1] = py[n + 1] = -inf;
    px[n + 2] = py[n + 2] = inf;
    sort(all(px));
    px.erase(unique(all(px)), px.end());
    sort(all(py));
    py.erase(unique(all(py)), py.end());
    int s = px.size() + py.size() - 6;
    ll res = 0;
    for (int j : check) {
      if (j == -1)
        continue;
      ll dis = inf;
      auto itx = lower_bound(all(px), x[j]);
      chmin(dis, *itx - x[j]);
      itx--;
      chmin(dis, x[j] - *itx);
      auto ity = lower_bound(all(py), y[j]);
      chmin(dis, *ity - y[j]);
      ity--;
      chmin(dis, y[j] - *ity);
      res += dis * p[j];
    }
    chmin(ans[s], res);
  }
  rep(i, n) chmin(ans[i + 1], ans[i]);
  rep(i, n + 1) cout << ans[i] << '\\n';
}