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

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
vector<int> f(int x, int n) {
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    r[i] = x % 3;
    x = x / 3;
  }
  return r;
}

int main() {
  int N;
  scanf("%d", &N);
  vector<vector<long long>> L(N, vector<long long>(3));
  for (int i = 0; i < N; i++) {
    scanf("%lld%lld%lld", &L[i][0], &L[i][1], &L[i][2]);
  }
  long long INF = 10000000000000000;
  vector<int> D(21);
  vector<int> T(21);
  D[0] = 1;
  T[0] = 1;
  for (int i = 0; i < 20; i++) {
    D[i + 1] = D[i] * 2;
    T[i + 1] = T[i] * 3;
  }
  vector<vector<long long>> X(D[N], vector<long long>(N, INF));
  vector<vector<long long>> Y(D[N], vector<long long>(N, INF));
  for (int i = 0; i < D[N]; i++) {
    for (int j = 0; j < N; j++) {
      X[i][j] = min(abs(L[j][0]), abs(L[j][1]));
      Y[i][j] = X[i][j];
      for (int k = 0; k < N; k++) {
        if (i & D[k]) {
          X[i][j] = min(X[i][j], abs(L[j][0] - L[k][0]));
          Y[i][j] = min(Y[i][j], abs(L[j][1] - L[k][1]));
        }
      }
      X[i][j] *= L[j][2];
      Y[i][j] *= L[j][2];
    }
  }
  vector<int> s;
  int t;
  long long u;
  vector<long long> P(N + 1, INF);
  int x, y;
  for (int i = 0; i < T[N]; i++) {
    s = f(i, N);
    x = 0;
    y = 0;
    for (int j = 0; j < N; j++) {
      if (s[j] == 1) {
        x += D[j];
      } else if (s[j] == 2) {
        y += D[j];
      }
    }
    t = N - count(s.begin(), s.end(), 0);
    u = 0;
    for (int j = 0; j < N; j++) {
      u += min(X[x][j], Y[y][j]);
    }
    P[t] = min(P[t], u);
  }
  for (int i = 0; i <= N; i++) {
    printf("%lld\\n", P[i]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string f(int x, int n) {
  string r = "";
  for (int i = 0; i < n; i++) {
    r = r + char(48 + (x % 3));
    x = x / 3;
  }
  return r;
}

int main() {
  int N;
  scanf("%d", &N);
  vector<vector<long long>> L(N, vector<long long>(3));
  for (int i = 0; i < N; i++) {
    scanf("%lld%lld%lld", &L[i][0], &L[i][1], &L[i][2]);
  }
  long long INF = 10000000000000000;
  vector<int> D(21);
  vector<int> T(21);
  D[0] = 1;
  T[0] = 1;
  for (int i = 0; i < 20; i++) {
    D[i + 1] = D[i] * 2;
    T[i + 1] = T[i] * 3;
  }
  vector<vector<long long>> X(D[N], vector<long long>(N, INF));
  vector<vector<long long>> Y(D[N], vector<long long>(N, INF));
  for (int i = 0; i < D[N]; i++) {
    for (int j = 0; j < N; j++) {
      X[i][j] = min(abs(L[j][0]), abs(L[j][1]));
      Y[i][j] = X[i][j];
      for (int k = 0; k < N; k++) {
        if (i & D[k]) {
          X[i][j] = min(X[i][j], abs(L[j][0] - L[k][0]));
          Y[i][j] = min(Y[i][j], abs(L[j][1] - L[k][1]));
        }
      }
      X[i][j] *= L[j][2];
      Y[i][j] *= L[j][2];
    }
  }
  string s;
  int t;
  long long u;
  vector<long long> P(N + 1, INF);
  int x, y;
  for (int i = 0; i < T[N]; i++) {
    s = f(i, N);
    x = 0;
    y = 0;
    for (int j = 0; j < N; j++) {
      if (s[j] == '1') {
        x += D[j];
      } else if (s[j] == '2') {
        y += D[j];
      }
    }
    t = N - count(s.begin(), s.end(), '0');
    u = 0;
    for (int j = 0; j < N; j++) {
      u += min(X[x][j], Y[y][j]);
    }
    P[t] = min(P[t], u);
  }
  for (int i = 0; i <= N; i++) {
    printf("%lld\\n", P[i]);
  }
}