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

constexpr int N = 15;
int x[N], y[N];
Int p[N];
int min_dist[N], orig_dist[N];
signed main() {
  int n;
  std::cin >> 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]; });
  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];
      REP(_, 2) {
        int l = 1e9;
        for (int i : x_ord) {
          if (T >> i & 1)
            l = x[i];
          chmin(min_dist[i], std::abs(x[i] - l));
        }
        std::reverse(ALL(x_ord));
      }
      REP(_, 2) {
        int l = 1e9;
        for (int i : y_ord) {
          if ((S ^ T) >> i & 1)
            l = y[i];
          chmin(min_dist[i], std::abs(y[i] - l));
        }
        std::reverse(ALL(y_ord));
      }
      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;
}

constexpr int N = 15;
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];
      REP(_, 2) {
        int l = 1e9;
        for (int i : x_ord) {
          if (T >> i & 1)
            l = x[i];
          chmin(min_dist[i], std::abs(x[i] - l));
        }
        std::reverse(ALL(x_ord));
      }
      REP(_, 2) {
        int l = 1e9;
        for (int i : y_ord) {
          if ((S ^ T) >> i & 1)
            l = y[i];
          chmin(min_dist[i], std::abs(y[i] - l));
        }
        std::reverse(ALL(y_ord));
      }
      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";
}