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

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

//.define
// #define FILE_IN_OUT
#define RET(_x) return cout << (_x) << '\\n', 0;
#define all(_obj) (_obj).begin(), (_obj).end()
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\\n' << std::flush;
#else
#define debug(_x)                                                              \\
  {}
#endif
#define endl "\\n"
template <typename P, typename Q> P CeilDiv(P _dividend, Q _divider) {
  return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char *_delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\\n';
}

//.constant
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
struct City {
  int X;
  int Y;
  int P;
};

bool HasBit(int x, int b) { return (x >> b) & 1; }

int best_x[1 << 15][15];
int best_y[1 << 15][15];
int kbit[1 << 15];

//.code
int main() {
#ifdef FILE_IN_OUT
  std::ifstream cin("input.txt", std::ios::in);
  std::ofstream cout("output.txt", std::ios::out);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n;
  cin >> n;
  vector<City> arr(n);
  loop(n) { cin >> arr[i].X >> arr[i].Y >> arr[i].P; }
  const int full_mask = 1 << n;
  for (int mask = 0; mask < full_mask; ++mask) {
    for (int i = 0; i < n; ++i) {
      if (!HasBit(mask, i)) {
        best_x[mask][i] = abs(arr[i].X);
        best_y[mask][i] = abs(arr[i].Y);
        for (int j = 0; j < n; ++j) {
          if (HasBit(mask, j)) {
            best_x[mask][i] = min(best_x[mask][i], abs(arr[i].X - arr[j].X));
            best_y[mask][i] = min(best_y[mask][i], abs(arr[i].Y - arr[j].Y));
          }
        }
      }
    }
  }
  vector<ll> ans(n + 1, LLONG_MAX);
  for (int mask = 0; mask < full_mask; ++mask) {
    kbit[mask] = kbit[mask >> 1] + (mask & 1);
    if (ans[kbit[mask]] == 0) {
      continue;
    }
    for (int sub_mask = mask; sub_mask > 0; sub_mask = (sub_mask - 1) & mask) {
      ll val = 0;
      for (int i = 0; i < n; ++i) {
        if (!HasBit(mask, i)) {
          val += 1LL * min(best_x[sub_mask][i], best_y[mask ^ sub_mask][i]) *
                 arr[i].P;
        }
      }
      ans[kbit[mask]] = min(ans[kbit[mask]], val);
    }
    ll val = 0;
    for (int i = 0; i < n; ++i) {
      if (!HasBit(mask, i)) {
        val += 1LL * min(best_x[0][i], best_y[mask][i]) * arr[i].P;
      }
    }
    ans[kbit[mask]] = min(ans[kbit[mask]], val);
  }
  for (ll x : ans) {
    cout << x << endl;
  }

#ifdef FILE_IN_OUT
  cin.close();
  cout.close();
#endif
  return 0;
}

Error: TLE

Fixed submission:

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

//.define
// #define FILE_IN_OUT
#define RET(_x) return cout << (_x) << '\\n', 0;
#define all(_obj) (_obj).begin(), (_obj).end()
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\\n' << std::flush;
#else
#define debug(_x)                                                              \\
  {}
#endif
#define endl "\\n"
template <typename Tp> Tp CeilDiv(Tp _dividend, long long _divider) {
  return static_cast<Tp>((_dividend - 1LL + _divider) / _divider);
}
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char *_delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\\n';
}

//.constant
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
struct City {
  int X;
  int Y;
  int P;
};

int minx[15];

bool HasBit(int x, int b) { return (x >> b) & 1; }

//.code
int main() {
#ifdef FILE_IN_OUT
  std::ifstream cin("input.txt", std::ios::in);
  std::ofstream cout("output.txt", std::ios::out);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n;
  cin >> n;
  vector<City> arr(n);
  loop(n) { cin >> arr[i].X >> arr[i].Y >> arr[i].P; }
  vector<int> xs(n);
  iota(all(xs), 0);
  sort(all(xs), [&arr](int i, int j) { return arr[i].X < arr[j].X; });
  vector<int> ys(n);
  iota(all(ys), 0);
  sort(all(ys), [&arr](int i, int j) { return arr[i].Y < arr[j].Y; });
  const int full_mask = 1 << n;
  vector<int> kbit(full_mask, 0);
  vector<ll> ans(n + 1, LLONG_MAX);
  for (int mask = 0; mask < full_mask; ++mask) {
    kbit[mask] = kbit[mask >> 1] + (mask & 1);
    if (ans[kbit[mask]] == 0) {
      continue;
    }
    for (int smask = mask; smask > 0; smask = (smask - 1) & mask) {
      memset(minx, INF, sizeof(minx));
      int prex = INF;
      for (int j = 0; j < n; ++j) {
        int i = xs[j];
        if (!HasBit(mask, i)) {
          minx[i] = min(abs(arr[i].X), abs(arr[i].Y));
          if (prex != INF) {
            minx[i] = min(minx[i], abs(prex - arr[i].X));
          }
        } else if (HasBit(smask, i)) {
          prex = arr[i].X;
        }
      }
      prex = INF;
      for (int j = n - 1; j >= 0; --j) {
        int i = xs[j];
        if (!HasBit(mask, i)) {
          if (prex != INF) {
            minx[i] = min(minx[i], abs(prex - arr[i].X));
          }
        } else if (HasBit(smask, i)) {
          prex = arr[i].X;
        }
      }
      prex = INF;
      for (int j = 0; j < n; ++j) {
        int i = ys[j];
        if (!HasBit(mask, i)) {
          if (prex != INF) {
            minx[i] = min(minx[i], abs(prex - arr[i].Y));
          }
        } else if (!HasBit(smask, i)) {
          prex = arr[i].Y;
        }
      }
      prex = INF;
      ll cur = 0;
      for (int j = n - 1; j >= 0; --j) {
        int i = ys[j];
        if (!HasBit(mask, i)) {
          if (prex != INF) {
            minx[i] = min(minx[i], abs(prex - arr[i].Y));
          }
          cur += 1LL * arr[i].P * minx[i];
        } else if (!HasBit(smask, i)) {
          prex = arr[i].Y;
        }
      }
      ans[kbit[mask]] = min(ans[kbit[mask]], cur);
    }
    int prex = INF;
    for (int j = 0; j < n; ++j) {
      int i = ys[j];
      if (!HasBit(mask, i)) {
        minx[i] = min(abs(arr[i].Y), abs(arr[i].X));
        if (prex != INF) {
          minx[i] = min(minx[i], abs(prex - arr[i].Y));
        }
      } else {
        prex = arr[i].Y;
      }
    }
    prex = INF;
    ll cur = 0;
    for (int j = n - 1; j >= 0; --j) {
      int i = ys[j];
      if (!HasBit(mask, i)) {
        if (prex != INF) {
          minx[i] = min(minx[i], abs(prex - arr[i].Y));
        }
        cur += 1LL * arr[i].P * minx[i];
      } else {
        prex = arr[i].Y;
      }
    }
    ans[kbit[mask]] = min(ans[kbit[mask]], cur);
  }
  for (ll x : ans) {
    cout << x << endl;
  }

#ifdef FILE_IN_OUT
  cin.close();
  cout.close();
#endif
  return 0;
}