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

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jul 25 22:14:29 JST 2020
 **/

template <class T> bool chmin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

signed main() {
  int n;
  std::cin >> n;
  int x[n], y[n];
  Int p[n];
  REP(i, n) std::cin >> x[i] >> y[i] >> p[i];
  std::vector<int> x_ord(n), y_ord(n);
  std::iota(ALL(x_ord), 0),
      std::sort(ALL(x_ord), [&](int i, int j) { return x[i] < x[j]; });
  std::iota(ALL(y_ord), 0),
      std::sort(ALL(y_ord), [&](int i, int j) { return y[i] < y[j]; });
  int min_dist[n], orig_dist[n];
  REP(i, n) orig_dist[i] = std::min(std::abs(x[i]), std::abs(y[i]));
  std::vector<Int> ans(n + 1, 1e18);
  REP(S, 1 << n) {
    Int cnt = __builtin_popcount(S);
    // Sの部分集合の列挙
    for (int T = S;; T = (T - 1) & S) {
      // 操作
      REP(i, n) min_dist[i] = orig_dist[i];
      int l = 1e9;
      REP(k, n) {
        int i = x_ord[k];
        if (T >> i & 1)
          l = x[i];
        chmin(min_dist[i], std::abs(x[i] - l));
      }
      l = 1e9;
      for (int k = n - 1; k >= 0; k--) {
        int i = x_ord[k];
        if (T >> i & 1)
          l = x[i];
        chmin(min_dist[i], std::abs(x[i] - l));
      }
      l = 1e9;
      REP(k, n) {
        int i = y_ord[k];
        if ((S ^ T) >> i & 1)
          l = y[i];
        chmin(min_dist[i], std::abs(y[i] - l));
      }
      l = 1e9;
      for (int k = n - 1; k >= 0; k--) {
        int i = y_ord[k];
        if ((S ^ T) >> i & 1)
          l = y[i];
        chmin(min_dist[i], std::abs(y[i] - l));
      }
      Int acc = 0;
      REP(i, n) acc += p[i] * min_dist[i];
      chmin(ans[cnt], acc);
      if (T == 0)
        break;
    }
  }
  for (Int z : ans)
    std::cout << z << "\\n";
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Jul 25 22:14:29 JST 2020
 **/

template <class T> bool chmin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool chmax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

signed main() {
  int n;
  std::cin >> n;
  std::vector<int> x(n), y(n);
  std::vector<Int> p(n);
  REP(i, n) std::cin >> x[i] >> y[i] >> p[i];
  std::vector<int> x_ord(n), y_ord(n);
  std::iota(ALL(x_ord), 0),
      std::sort(ALL(x_ord), [&](int i, int j) { return x[i] < x[j]; });
  std::iota(ALL(y_ord), 0),
      std::sort(ALL(y_ord), [&](int i, int j) { return y[i] < y[j]; });
  std::vector<int> min_dist(n), orig_dist(n);
  REP(i, n) orig_dist[i] = std::min(std::abs(x[i]), std::abs(y[i]));
  std::vector<Int> ans(n + 1, 1e18);
  REP(S, 1 << n) {
    Int cnt = __builtin_popcount(S);
    // Sの部分集合の列挙
    for (int T = S;; T = (T - 1) & S) {
      // 操作
      REP(i, n) min_dist[i] = orig_dist[i];
      int l = 1e9;
      REP(k, n) {
        int i = x_ord[k];
        if (T >> i & 1)
          l = x[i];
        chmin(min_dist[i], std::abs(x[i] - l));
      }
      l = 1e9;
      for (int k = n - 1; k >= 0; k--) {
        int i = x_ord[k];
        if (T >> i & 1)
          l = x[i];
        chmin(min_dist[i], std::abs(x[i] - l));
      }
      l = 1e9;
      REP(k, n) {
        int i = y_ord[k];
        if ((S ^ T) >> i & 1)
          l = y[i];
        chmin(min_dist[i], std::abs(y[i] - l));
      }
      l = 1e9;
      for (int k = n - 1; k >= 0; k--) {
        int i = y_ord[k];
        if ((S ^ T) >> i & 1)
          l = y[i];
        chmin(min_dist[i], std::abs(y[i] - l));
      }
      Int acc = 0;
      REP(i, n) acc += p[i] * min_dist[i];
      chmin(ans[cnt], acc);
      if (T == 0)
        break;
    }
  }
  for (Int z : ans)
    std::cout << z << "\\n";
}