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

#pragma GCC optimize("Ofast")

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> X(N), Y(N), P(N);
  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  const int INF = 1e9 + 1;
  int t = (1 << N) - 1;
  vector<long long> ans(N + 1, 1e18);

  vector<vector<long long>> DX(1 << N, vector<long long>(N, INF)), DY = DX;
  for (int i = 0; i < (1 << N); ++i) {
    vector<long long> XX = {-INF, 0, INF};
    for (int l = 0; l < N; ++l) {
      if (i >> l & 1) {
        XX.emplace_back(X[l]);
      }
    }
    sort(XX.begin(), XX.end());
    for (int l = 0; l < N; ++l) {
      auto xlb = lower_bound(XX.begin(), XX.end(), X[l]);
      long long dx = (*xlb) - X[l];
      dx = min(dx, X[l] - (*prev(xlb)));
      DX[i][l] = dx;
    }

    vector<long long> YY = {-INF, 0, INF};
    for (int l = 0; l < N; ++l) {
      if (i >> l & 1) {
        YY.emplace_back(Y[l]);
      }
    }
    sort(YY.begin(), YY.end());
    long long s = 0;
    for (int l = 0; l < N; ++l) {
      auto ylb = lower_bound(YY.begin(), YY.end(), Y[l]);
      long long dy = (*ylb) - Y[l];
      dy = min(dy, Y[l] - (*prev(ylb)));
      DY[i][l] = dy;
    }
  }

  for (int i = 0; i < (1 << N); ++i) {
    int u = t ^ i;
    for (int k = u;; k = (k - 1) & u) {

      int p = __builtin_popcount(i | k);
      long long s = 0;
      for (int l = 0; l < N; ++l) {
        s += min(DX[i][l], DY[k][l]) * P[l];
        if (s >= ans[p])
          break;
      }
      ans[p] = min(ans[p], s);
      if (!k)
        break;
    }
  }
  for (auto e : ans)
    cout << e << "\\n";
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> X(N), Y(N), P(N);
  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  const int INF = 1e9 + 1;
  int t = (1 << N) - 1;
  vector<long long> ans(N + 1, 1e18);
  for (int i = 0; i < (1 << N); ++i) {
    vector<long long> XX = {-INF, 0, INF};
    vector<long long> D_(N, INF);
    for (int l = 0; l < N; ++l) {
      if (i >> l & 1) {
        XX.emplace_back(X[l]);
      }
    }
    sort(XX.begin(), XX.end());
    for (int l = 0; l < N; ++l) {
      auto xlb = lower_bound(XX.begin(), XX.end(), X[l]);
      long long dx = (*xlb) - X[l];
      dx = min(dx, X[l] - (*prev(xlb)));
      D_[l] = dx;
    }
    int u = t ^ i;
    for (int k = u;; k = (k - 1) & u) {
      vector<long long> D = D_;

      vector<long long> YY = {-INF, 0, INF};
      for (int l = 0; l < N; ++l) {
        if (k >> l & 1) {
          YY.emplace_back(Y[l]);
        }
      }
      sort(YY.begin(), YY.end());
      long long s = 0;
      int p = __builtin_popcount(i | k);
      for (int l = 0; l < N; ++l) {
        auto ylb = lower_bound(YY.begin(), YY.end(), Y[l]);
        long long dy = (*ylb) - Y[l];
        dy = min(dy, Y[l] - (*prev(ylb)));
        D[l] = min(D[l], dy);
        s += D[l] * P[l];
        if (s >= ans[p])
          break;
      }
      ans[p] = min(ans[p], s);
      if (!k)
        break;
    }
  }
  for (auto e : ans)
    cout << e << "\\n";
}