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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\\n"
#define int ll

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  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];
  int bit2 = pow(2, N);
  vector<vector<int>> tate(bit2, vector<int>(N));
  vector<vector<int>> yoko(bit2, vector<int>(N));
  for (int b = 0; b < bit2; b++) {
    for (int i = 0; i < N; i++) {
      int res = abs(X[i]);
      for (int j = 0; j < N; j++) {
        if (((b >> j) % 2))
          res = min(res, abs(X[i] - X[j]));
      }
      tate[b][i] = res * P[i];
    }
    for (int i = 0; i < N; i++) {
      int res = abs(Y[i]);
      for (int j = 0; j < N; j++) {
        if (((b >> j) % 2))
          res = min(res, abs(Y[i] - Y[j]));
      }
      yoko[b][i] = res * P[i];
    }
  }
  int bit = pow(3, N);
  vector<int> ANS(N + 1, LONG_MAX);
  for (int b = 0; b < bit; b++) { // 30000
    int s = 0;
    int bbb = b;
    for (int j = 0; j < N; j++) {
      s += (bbb % 3 != 0);
      bbb /= 3;
    }
    int bb = b;
    int t = 0, y = 0, two = 1;
    ;
    for (int j = 0; j < N; j++) { // 10
      if (bb % 3 == 1)
        t += two;
      if (bb % 3 == 2)
        y += two;
      bb /= 3;
      two *= 2;
    }
    int ans = 0;
    for (int i = 0; i < N; i++)
      ans += min(tate[t][i], yoko[y][i]);
    ANS[s] = min(ans, ANS[s]);
  }
  for (auto it : ANS)
    cout << it << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\\n"
#define int ll

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  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];
  int bit2 = pow(2, N);
  vector<vector<int>> tate(bit2, vector<int>(N));
  vector<vector<int>> yoko(bit2, vector<int>(N));
  for (int b = 0; b < bit2; b++) {
    for (int i = 0; i < N; i++) {
      int res = abs(X[i]);
      for (int j = 0; j < N; j++) {
        if (((b >> j) % 2))
          res = min(res, abs(X[i] - X[j]));
      }
      tate[b][i] = res * P[i];
    }
    for (int i = 0; i < N; i++) {
      int res = abs(Y[i]);
      for (int j = 0; j < N; j++) {
        if (((b >> j) % 2))
          res = min(res, abs(Y[i] - Y[j]));
      }
      yoko[b][i] = res * P[i];
    }
  }
  int bit = pow(3, N);
  for (int i = 0; i <= N; i++) { // 10
    int res = LONG_MAX;
    for (int b = 0; b < bit; b++) { // 30000
      int s = 0;
      int bbb = b;
      for (int j = 0; j < N; j++) {
        s += (bbb % 3 != 0);
        bbb /= 3;
      }
      if (s != i)
        continue;
      int bb = b;
      int t = 0, y = 0, two = 1;
      ;
      for (int j = 0; j < N; j++) { // 10
        if (bb % 3 == 1)
          t += two;
        if (bb % 3 == 2)
          y += two;
        bb /= 3;
        two *= 2;
      }
      int ans = 0;
      for (int i = 0; i < N; i++)
        ans += min(tate[t][i], yoko[y][i]);
      res = min(res, ans);
    }
    cout << res << endl;
  }
  return 0;
}