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

#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int64_t> X(N), Y(N), P(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  vector<vector<int64_t>> SX(1 << N, vector<int64_t>(N, INF)),
      SY(1 << N, vector<int64_t>(N, INF));
  for (int bit = 0; bit < (1 << N); bit++) {
    for (int i = 0; i < N; i++) {
      SX[bit][i] = min(SX[bit][i], abs(X[i]));
      SY[bit][i] = min(SY[bit][i], abs(Y[i]));
      if ((bit >> i) & 1) {
        for (int j = 0; j < N; j++) {
          SX[bit][j] = min(SX[bit][j], abs(X[i] - X[j]));
          SY[bit][j] = min(SY[bit][j], abs(Y[i] - Y[j]));
        }
      }
    }
  }
  vector<int64_t> res(N + 1, INF);
  for (int bitx = 0; bitx < (1 << N); bitx++) {
    int m = ((1 << N) - 1) ^ bitx;
    for (int bity = m;; bity = (bity - 1) & m) {
      int64_t sum = 0;
      for (int i = 0; i < N; i++) {
        sum += P[i] * min(SX[bitx][i], SY[bity][i]);
      }
      int b = __builtin_popcount(bitx) + __builtin_popcount(bity);
      res[b] = min(res[b], sum);
      if (bity == 0)
        break;
    }
  }
  for (auto r : res) {
    cout << r << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

template <typename T>
int pow(int x, T n, int mod = std::numeric_limits<int>::max()) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<int64_t>(res) * static_cast<int64_t>(x) % mod;
    x = static_cast<int64_t>(x) * static_cast<int64_t>(x) % mod;
    n >>= 1;
  }
  return res;
}

template <typename T>
int64_t pow(int64_t x, T n, int64_t mod = std::numeric_limits<int64_t>::max()) {
  int64_t res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<__int128>(res) * static_cast<__int128>(x) % mod;
    x = static_cast<__int128>(x) * static_cast<__int128>(x) % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int64_t> X(N), Y(N), P(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  vector<int64_t> res(N + 1, INF);
  for (int bit = 0; bit < pow(3, N); bit++) {
    vector<int64_t> cost(N, INF);
    int x = bit, cnt = 0;
    vector<int64_t> XX, YY;
    for (int i = 0; i < N; i++) {
      if (x % 3 == 1) {
        XX.emplace_back(X[i]);
        cnt++;
      } else if (x % 3 == 2) {
        YY.emplace_back(Y[i]);
        cnt++;
      }
      x /= 3;
    }
    XX.emplace_back(-INF);
    XX.emplace_back(INF);
    YY.emplace_back(-INF);
    YY.emplace_back(INF);
    sort(XX.begin(), XX.end());
    sort(YY.begin(), YY.end());
    for (int i = 0; i < N; i++) {
      int idx = lower_bound(XX.begin(), XX.end(), X[i]) - XX.begin();
      cost[i] = min(cost[i], abs(XX[idx] - X[i]));
      cost[i] = min(cost[i], abs(XX[idx - 1] - X[i]));
    }
    for (int i = 0; i < N; i++) {
      int idx = lower_bound(YY.begin(), YY.end(), Y[i]) - YY.begin();
      cost[i] = min(cost[i], abs(YY[idx] - Y[i]));
      cost[i] = min(cost[i], abs(YY[idx - 1] - Y[i]));
    }
    int64_t tot = 0;
    for (int i = 0; i < N; i++) {
      cost[i] = min(cost[i], min(abs(X[i]), abs(Y[i])));
      tot += P[i] * cost[i];
    }
    res[cnt] = min(res[cnt], tot);
  }
  for (int i = 0; i < N + 1; i++) {
    cout << res[i] << '\\n';
  }
  return 0;
}