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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define pb push_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1e18;

ll n, res, xbit, ybit, cnt;
V<> x(15), y(15), p(15), ans(16, INF), xp, yp;
VV<> xans(100000, V<>(15)), yans(100000, V<>(15));

void solve(string s) {
  xp.clear();
  yp.clear();
  res = xbit = ybit = cnt = 0;
  REP(i, n) {
    if (s[i] == '1') {
      xp.pb(x[i]);
      xbit += (1 << i);
      cnt++;
    }
    if (s[i] == '2') {
      yp.pb(y[i]);
      ybit += (1 << i);
      cnt++;
    }
  }
  if (cnt > n)
    return;
  REP(i, n) if (s[i] == '0') res += min(xans[xbit][i], yans[ybit][i]);
  chmin(ans[cnt], res);
}

void dfs(string s) {
  if (SZ(s) == n) {
    solve(s);
    return;
  }
  dfs(s + "0");
  dfs(s + "1");
  dfs(s + "2");
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  REP(i, n) cin >> x[i] >> y[i] >> p[i];

  // 前準備(計算量Ο(2^N * NlogN)なはず？)
  REP(bit, (1 << n)) {
    V<> t, u;
    REP(i, n) if ((bit >> i) & 1) {
      t.pb(x[i]);
      u.pb(y[i]);
    }
    t.pb(INF);
    t.pb(-INF);
    t.pb(0);
    u.pb(INF);
    u.pb(-INF);
    u.pb(0);
    sort(all(t));
    sort(all(u));
    REP(i, n) {
      auto itr0 = lower_bound(all(t), x[i]);
      auto itr1 = itr0;
      itr1--;
      xans[bit][i] = min(*itr0 - x[i], x[i] - *itr1) * p[i];
      itr0 = lower_bound(all(u), y[i]);
      itr1 = itr0;
      itr1--;
      yans[bit][i] = min(*itr0 - y[i], y[i] - *itr1) * p[i];
    }
  }
  // end

  // 全探索(計算量Ο(3^N*N)なはず？)
  dfs("");
  // end

  REP(i, n + 1) cout << ans[i] << '\\n';
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\\n'; }
inline void YAY(bool b = true) { cout << (b ? "Yay!" : ":(") << '\\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\\n';
    exit(0);
  }
}
template <class T> inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\\n';
    exit(0);
  }
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
V<> VEC(ll n) {
  V<> v(n);
  REP(i, n) cin >> v[i];
  return v;
}
template <typename T> T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1)
      ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}

ll n;
V<> x(15), y(15), p(15);
V<> ans(16, INF);
VV<> xans(100000, V<>(15)), yans(100000, V<>(15));

void solve(string s) {
  V<> xp, yp;
  ll res = 0, xbit = 0, ybit = 0, cnt = 0;
  REP(i, n) {
    if (s[i] == '1') {
      xp.pb(x[i]);
      xbit += (1 << i);
      cnt++;
    }
    if (s[i] == '2') {
      yp.pb(y[i]);
      ybit += (1 << i);
      cnt++;
    }
  }
  if (cnt > n)
    return;
  REP(i, n) if (s[i] == '0') res += min(xans[xbit][i], yans[ybit][i]);
  chmin(ans[cnt], res);
}

void dfs(string s) {
  if (SZ(s) == n) {
    solve(s);
    return;
  }
  dfs(s + "0");
  dfs(s + "1");
  dfs(s + "2");
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  REP(i, n) cin >> x[i] >> y[i] >> p[i];

  // 前準備
  REP(bit, (1 << n)) {
    V<> t, u;
    REP(i, n) if ((bit >> i) & 1) {
      t.pb(x[i]);
      u.pb(y[i]);
    }
    t.pb(INF);
    t.pb(-INF);
    t.pb(0);
    u.pb(INF);
    u.pb(-INF);
    u.pb(0);
    sort(all(t));
    sort(all(u));
    REP(i, n) {
      auto itr0 = lower_bound(all(t), x[i]);
      auto itr1 = itr0;
      itr1--;
      xans[bit][i] = min(*itr0 - x[i], x[i] - *itr1) * p[i];
      itr0 = lower_bound(all(u), y[i]);
      itr1 = itr0;
      itr1--;
      yans[bit][i] = min(*itr0 - y[i], y[i] - *itr1) * p[i];
    }
  }
  // end

  // 3^n全探索
  dfs("");
  // end

  REP(i, n + 1) cout << ans[i] << '\\n';
}