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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2020000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
ll pcount(ll x) { return __builtin_popcount(x); };
const int mod = 1e9 + 7;
// const int mod = 998244353;

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vl ans(n + 1, linf);
  vvl tate(1 << n, vl(n, linf)), yoko(1 << n, vl(n, linf));
  rep(bit, 1 << n) {
    rep(i, n) {
      chmin(tate[bit][i], abs(x[i]));
      chmin(yoko[bit][i], abs(y[i]));
      rep(j, n) {
        if (bit >> j & 1) {
          chmin(tate[bit][i], abs(x[i] - x[j]));
          chmin(yoko[bit][i], abs(y[i] - y[j]));
        }
      }
    }
  }
  rep(bit, 1 << n) {
    int pcnt = pcount(bit);
    int stop = 1;
    for (int sub = bit; stop > 0; sub = (sub - 1) & bit) {
      stop = sub;
      ll sum = 0;
      rep(i, n) { sum += min(tate[sub][i], yoko[bit - sub][i]) * p[i]; }
      chmin(ans[pcnt], sum);
    }
  }
  rep(i, n + 1) cout << ans[i] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2020000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  rep(i, a.size()) cout << a[i] << " ";
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\\n";
}
ll pcount(ll x) { return __builtin_popcount(x); };
const int mod = 1e9 + 7;
// const int mod = 998244353;

int main() {
  int n;
  cin >> n;
  vl x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vl ans(n + 1, linf);
  vector<int> tate, yoko;
  rep(bit, 1 << n) {
    int stop = 1;
    for (int sub = bit; stop > 0; sub = (sub - 1) & bit) {
      stop = sub;
      rep(i, n) {
        if (bit >> i & 1) {
          if (sub >> i & 1) { // 縦方向
            tate.push_back(x[i]);
          } else { // 横方向
            yoko.push_back(y[i]);
          }
        }
      }
      tate.push_back(0);
      yoko.push_back(0);
      sort(all(tate));
      sort(all(yoko));
      ll sum = 0;
      rep(i, n) {
        if (bit >> i & 1)
          continue;
        ll mn = linf;
        auto itr = lower_bound(all(tate), x[i]);
        if (itr != tate.end())
          chmin(mn, *itr - x[i]);
        if (itr != tate.begin()) {
          itr--;
          chmin(mn, x[i] - *itr);
        }
        itr = lower_bound(all(yoko), y[i]);
        if (itr != yoko.end())
          chmin(mn, *itr - y[i]);
        if (itr != yoko.begin()) {
          itr--;
          chmin(mn, y[i] - *itr);
        }
        sum += mn * p[i];
      }
      chmin(ans[pcount(bit)], sum);
      tate.clear();
      yoko.clear();
    }
  }
  rep(i, n + 1) cout << ans[i] << "\\n";
}