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

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint N;
  cin >> N;
  vector<lint> X(N);
  vector<lint> Y(N);
  vector<lint> P(N);
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];
  vector<vector<lint>> xd(N, vector<lint>(1 << N));
  REP(i, N) REP(bit, 1 << N) {
    xd[i][bit] = abs(X[i]) * P[i];
    REP(j, N)
    if (bit >> j & 1) xd[i][bit] = min(xd[i][bit], abs(X[i] - X[j]) * P[i]);
  }
  vector<vector<lint>> yd(N, vector<lint>(1 << N));
  REP(i, N) REP(bit, 1 << N) {
    yd[i][bit] = abs(Y[i]) * P[i];
    REP(j, N)
    if (bit >> j & 1) yd[i][bit] = min(yd[i][bit], abs(Y[i] - Y[j]) * P[i]);
  }
  vector<lint> ans(N + 1, 1e18);
  vector<lint> pow3(N + 1);
  REP(i, N + 1) pow3[i] = pow(3LL, i);
  REP(bit, pow3[N]) {
    lint cnt = 0;
    lint xbit = 0;
    lint ybit = 0;
    lint bittmp = bit;
    REP(i, N) {
      lint t = bittmp % 3;
      if (t == 1) {
        cnt++;
        xbit += 1 << i;
      } else if (t == 2) {
        cnt++;
        ybit += 1 << i;
      }
      bittmp /= 3;
    }
    lint sum = 0;
    REP(i, N) sum += min(xd[i][xbit], yd[i][ybit]);
    ans[cnt] = min(ans[cnt], sum);
  }
  REP(i, N + 1) cout << ans[i] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\\n"; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint N;
  cin >> N;
  vector<lint> X(N);
  vector<lint> Y(N);
  vector<lint> P(N);
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];
  vector<vector<lint>> xd(N, vector<lint>(1 << N));
  REP(i, N) REP(bit, 1 << N) {
    xd[i][bit] = abs(X[i]) * P[i];
    REP(j, N)
    if (bit >> j & 1) xd[i][bit] = min(xd[i][bit], abs(X[i] - X[j]) * P[i]);
  }
  vector<vector<lint>> yd(N, vector<lint>(1 << N));
  REP(i, N) REP(bit, 1 << N) {
    yd[i][bit] = abs(Y[i]) * P[i];
    REP(j, N)
    if (bit >> j & 1) yd[i][bit] = min(yd[i][bit], abs(Y[i] - Y[j]) * P[i]);
  }
  vector<lint> ans(N + 1, 1e18);
  REP(bit, pow(3LL, N)) {
    lint cnt = 0;
    lint xbit = 0;
    lint ybit = 0;
    REP(i, N) {
      if ((bit / (lint)pow(3LL, i)) % 3 == 1) {
        cnt++;
        xbit += 1 << i;
      } else if ((bit / (lint)pow(3LL, i)) % 3 == 2) {
        cnt++;
        ybit += 1 << i;
      }
    }
    lint sum = 0;
    REP(i, N) sum += min(xd[i][xbit], yd[i][ybit]);
    ans[cnt] = min(ans[cnt], sum);
  }
  REP(i, N + 1) cout << ans[i] << "\\n";
}