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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  V<P<ll, P<ll, ll>>> xyp(n);
  for (int i = 0; i < n; i++)
    cin >> xyp[i].first >> xyp[i].second.first >> xyp[i].second.second;
  vll ans(n + 1, HIGHINF);
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    ans[0] +=
        min(abs(xyp[i].first), abs(xyp[i].second.first)) * xyp[i].second.second;
  }

  V<V<ll>> xxx(1 << n, vll(n)), yyy(1 << n, vll(n));
  for (int state = 0; state < (1 << n); state++) {
    set<ll> x = {0};
    for (int i = 0; i < n; i++) {
      if (state >> i & 1)
        x.insert(xyp[i].first);
    }
    for (int i = 0; i < n; i++) {
      if (state >> i & 1) {
        xxx[state][i] = 0;
      } else {
        ll diff = HIGHINF;
        auto itx = x.lower_bound(xyp[i].first);
        if (itx != x.end())
          chmin(diff, abs(*itx - xyp[i].first));
        if (itx != x.begin())
          chmin(diff, abs(*(prev(itx)) - xyp[i].first));
        xxx[state][i] = diff;
      }
    }
    set<ll> y = {0};
    for (int i = 0; i < n; i++) {
      if (state >> i & 1)
        y.insert(xyp[i].second.first);
    }
    for (int i = 0; i < n; i++) {
      if (state >> i & 1) {
        yyy[state][i] = 0;
      } else {
        ll diff = HIGHINF;
        auto itx = y.lower_bound(xyp[i].second.first);
        if (itx != y.end())
          chmin(diff, abs(*itx - xyp[i].second.first));
        if (itx != y.begin())
          chmin(diff, abs(*(prev(itx)) - xyp[i].second.first));
        yyy[state][i] = diff;
      }
    }
  }

  for (int state = 1; state < (1 << n); state++) {
    for (int subs = state; subs >= 0; subs = (subs - 1) & state) {
      ll tmp = 0;
      for (int i = 0; i < n; i++) {
        tmp += min(xxx[subs][i], yyy[state ^ subs][i]) * xyp[i].second.second;
      }
      chmin(ans[__builtin_popcount(state)], tmp);
      if (subs == 0)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \\
  REP(sz, vec.size())                                                          \\
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  V<P<ll, P<ll, ll>>> xyp(n);
  for (int i = 0; i < n; i++)
    cin >> xyp[i].first >> xyp[i].second.first >> xyp[i].second.second;
  vll ans(n + 1, HIGHINF);
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    ans[0] +=
        min(abs(xyp[i].first), abs(xyp[i].second.first)) * xyp[i].second.second;
  }

  for (int state = 1; state < (1 << n); state++) {
    for (int subs = state; subs >= 0; subs = (subs - 1) & state) {
      set<ll> x = {0}, y = {0};
      // subs が y 軸
      for (int i = 0; i < n; i++) {
        if (state >> i & 1) {
          if (subs >> i & 1)
            y.insert(xyp[i].second.first);
          else
            x.insert(xyp[i].first);
        }
      }

      ll tmp = 0;
      for (int i = 0; i < n; i++) {
        if (state >> i & 1)
          continue;
        ll dist = HIGHINF;
        auto itx = x.lower_bound(xyp[i].first);
        if (itx != x.end())
          chmin(dist, abs(*itx - xyp[i].first) * xyp[i].second.second);
        if (itx != x.begin())
          chmin(dist, abs(*prev(itx) - xyp[i].first) * xyp[i].second.second);
        auto ity = y.lower_bound(xyp[i].second.first);
        if (ity != y.end())
          chmin(dist, abs(*ity - xyp[i].second.first) * xyp[i].second.second);
        if (ity != y.begin())
          chmin(dist,
                abs(*prev(ity) - xyp[i].second.first) * xyp[i].second.second);
        tmp += dist;
      }
      chmin(ans[int(x.size() + y.size()) - 2], tmp);
      if (subs == 0)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}