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 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) {
      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;
}


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 set<int> &x, const set<int> &y, const int X,
                const int Y) {
  int distance = INT_MAX;
  auto itr = x.lower_bound(X);
  if (itr != x.end())
    distance = min(distance, abs(*itr - X));
  if (itr != x.begin())
    distance = min(distance, abs(*(--itr) - X));
  itr = y.lower_bound(Y);
  if (itr != y.end())
    distance = min(distance, abs(*itr - Y));
  if (itr != y.begin())
    distance = min(distance, abs(*(--itr) - Y));
  return distance;
}

void dfs(const ll N, const int i, set<int> &x, set<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) {
      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;
  }
  if (x.find(X[i]) == x.end()) {
    x.insert(X[i]);
    z.erase(i);
    dfs(N, i + 1, x, y, z, X, Y, P, answer);
    z.insert(i);
    x.erase(X[i]);
  }
  if (y.find(Y[i]) == y.end()) {
    y.insert(Y[i]);
    z.erase(i);
    dfs(N, i + 1, x, y, z, X, Y, P, answer);
    z.insert(i);
    y.erase(Y[i]);
  }
  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};

  set<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;
}