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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define itrout(x)                                                              \\
  for (int i = 0; i < x.size(); i++) {                                         \\
    cout << x[i] << (i == x.size() - 1 ? "\\n" : " ");                          \\
  }
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define debugbit(x, n)                                                         \\
  cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")"              \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long
typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long N, std::vector<long long> X, std::vector<long long> Y,
           std::vector<long long> P) {
  // preprocess
  vector<vector<ll>> dx(1 << N, vector<ll>(N, LLONG_MAX)),
      dy(1 << N, vector<ll>(N, LLONG_MAX));
  rep(i, 0, 1 << N) {
    vector<int> x, y;
    rep(j, 0, N) {
      if (1 << j & i)
        x.push_back(j);
      else
        y.push_back(j);
    }
    rep(j, 0, N) {
      ll minCostX = abs(X[j] - 0) * P[j], minCostY = abs(Y[j] - 0) * P[j];
      for (auto xi : x) {
        minCostX = min(minCostX, (abs(X[j] - X[xi]) * P[j]));
      }
      for (auto yi : y) {
        minCostY = min(minCostY, (abs(Y[j] - Y[yi]) * P[j]));
      }
      dx[i][j] = minCostX;
      dy[i][j] = minCostY;
    }
  }

  // solve
  vector<ll> answer(N + 1, LLONG_MAX);
  rep(i, 0, 1 << N) {
    vector<int> used;
    rep(j, 0, N) {
      if (1 << j & i)
        used.push_back(j);
    }
    rep(j, 0, 1 << used.size()) {
      ll x = 0, y = (1 << N) - 1;
      rep(k, 0, used.size()) {
        if (1 << k & j)
          x += 1 << used[k];
        else
          y -= 1 << used[k];
      }
      ll cost = 0;
      rep(j, 0, N) { cost += min(dx[x][j], dy[y][j]); }
      answer[used.size()] = min(answer[used.size()], cost);
    }
  }
  rep(i, 0, N + 1) { cout << answer[i] << endl; }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> X(N);
  std::vector<long long> Y(N);
  std::vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &X[i]);
    scanf("%lld", &Y[i]);
    scanf("%lld", &P[i]);
  }
  solve(N, std::move(X), std::move(Y), std::move(P));
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define itrout(x)                                                              \\
  for (int i = 0; i < x.size(); i++) {                                         \\
    cout << x[i] << (i == x.size() - 1 ? "\\n" : " ");                          \\
  }
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

int getDistance(const vector<int> &x, const vector<int> &y, const int X,
                const int Y) {
  int distance = INT_MAX;
  for (const int v : x)
    distance = min(distance, abs(v - X));
  for (const int v : y)
    distance = min(distance, abs(v - Y));
  return distance;
}

void dfs(const ll N, const int i, vector<int> &x, vector<int> &y,
         unordered_set<int> &z, const std::vector<long long> &X,
         const std::vector<long long> &Y, const std::vector<long long> &P,
         vector<ll> &answer) {
  if (i >= N) {
    ll s = 0;
    //        for (const int k : z) {
    rep(k, 0, N) { s += P[k] * getDistance(x, y, X[k], Y[k]); }
    //        }
    answer[x.size() + y.size() - 2] = min(answer[x.size() + y.size() - 2], s);
    return;
  }
  x.push_back(X[i]);
  z.erase(i);
  dfs(N, i + 1, x, y, z, X, Y, P, answer);
  z.insert(i);
  x.pop_back();

  y.push_back(Y[i]);
  z.erase(i);
  dfs(N, i + 1, x, y, z, X, Y, P, answer);
  z.insert(i);
  y.pop_back();

  dfs(N, i + 1, x, y, z, X, Y, P, answer);
}

void solve(long long N, std::vector<long long> X, std::vector<long long> Y,
           std::vector<long long> P) {
  //    N = 15;
  //    X = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  //    Y = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  //    P = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

  vector<int> x = {0}, y = {0};
  unordered_set<int> z;
  rep(i, 0, N) z.insert(i);
  vector<ll> answer(N + 1, LLONG_MAX);
  dfs(N, 0, x, y, z, X, Y, P, answer);
  ll minS = LLONG_MAX;
  rep(i, 0, N + 1) {
    minS = min(minS, answer[i]);
    cout << minS << endl;
  }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> X(N);
  std::vector<long long> Y(N);
  std::vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &X[i]);
    scanf("%lld", &Y[i]);
    scanf("%lld", &P[i]);
  }
  solve(N, std::move(X), std::move(Y), std::move(P));
  return 0;
}