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

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
struct baseN {
  int base;
  int len;
  string val; // reversed
  string digit = "0123456789ABCDEF";
  bool end = false;
  baseN(int b, int l) : base(b), len(l) { val = string(len, '0'); }
  baseN operator++(int) {
    bool flag = true;
    for (int i = 0; i < len; i++) {
      if (val[i] != digit[base - 1]) {
        val[i] = digit[pos(val[i]) + 1];
        flag = false;
        break;
      } else {
        val[i] = '0';
      }
    }
    if (flag)
      end = true;
    return *this;
  }
  int operator[](int i) { return pos(val[i]); }
  int pos(char s) {
    if (s - '0' < 10)
      return s - '0';
    else
      return s - 'A' + 10;
  }
};
#pragma endregion library
int n;
vector<tuple<ll, ll, ll>> m;
vll ans;
const int MX = 16;
ll dpx[1 << 16][16], dpy[1 << 16][16];
void solve(string &i) {
  int xbit = 0, ybit = 0;
  int cnt = 0;
  rep(j, n) {
    if (i[j] == '1')
      cnt++, ybit += bit(j);
    if (i[j] == '2')
      cnt++, xbit += bit(j);
  }
  ll cost = 0;
  rep(j, n) { cost += min(dpx[xbit][j], dpy[ybit][j]); }
  ans[cnt] = min(ans[cnt], cost);
}
void dfs(string s) {
  if ((int)s.length() == n) {
    solve(s);
    return;
  }
  dfs(s + '0');
  dfs(s + '1');
  dfs(s + '2');
}
int main() {
  cin >> n;
  rep(i, n) {
    ll x, y, p;
    cin >> x >> y >> p;
    m.emplace_back(x, y, p);
  }
  ans.assign(n + 1, LINF);
  rep(i, bit(n)) {
    set<ll> xax = {0ll}, yax = {0ll};
    rep(j, n) {
      if (bit(j) & i)
        yax.emplace(get<0>(m[j])), xax.emplace(get<1>(m[j]));
    }
    rep(j, n) {
      ll x, y, p;
      tie(x, y, p) = m[j];
      ll now = LINF;
      auto it2 = yax.lower_bound(x);
      if (it2 != yax.end())
        now = min(now, (*it2 - x) * p);
      if (it2 != yax.begin())
        now = min(now, (x - *(--it2)) * p);
      dpy[i][j] = now;
      now = LINF;
      auto it = xax.lower_bound(y);
      if (it != xax.end())
        now = min(now, (*it - y) * p);
      if (it != xax.begin())
        now = min(now, (y - *(--it)) * p);
      dpx[i][j] = now;
    }
  }
  dfs("");
  rep(i, n + 1) { cout << ans[i] << '\\n'; }
}

Error: TLE

Fixed submission:

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
struct baseN {
  int base;
  int len;
  string val; // reversed
  string digit = "0123456789ABCDEF";
  bool end = false;
  baseN(int b, int l) : base(b), len(l) { val = string(len, '0'); }
  baseN operator++(int) {
    bool flag = true;
    for (int i = 0; i < len; i++) {
      if (val[i] != digit[base - 1]) {
        val[i] = digit[pos(val[i]) + 1];
        flag = false;
        break;
      } else {
        val[i] = '0';
      }
    }
    if (flag)
      end = true;
    return *this;
  }
  int operator[](int i) { return pos(val[i]); }
  int pos(char s) {
    if (s - '0' < 10)
      return s - '0';
    else
      return s - 'A' + 10;
  }
};
#pragma endregion library
int n;
vector<tuple<ll, ll, ll>> m;
vll ans;
void solve(string &i) {
  set<ll> xax = {0ll}, yax = {0ll};
  int cnt = 0;
  rep(j, n) {
    if (i[j] == '1')
      yax.emplace(get<0>(m[j])), cnt++;
    if (i[j] == '2')
      xax.emplace(get<1>(m[j])), cnt++;
  }
  ll cost = 0;
  rep(j, n) {
    if (i[j] != '0')
      continue;
    ll x, y, p;
    tie(x, y, p) = m[j];
    ll now = LINF;
    auto it = xax.lower_bound(y);
    if (it != xax.end())
      now = min(now, (*it - y) * p);
    if (it != xax.begin())
      now = min(now, (y - *(--it)) * p);
    auto it2 = yax.lower_bound(x);
    if (it2 != yax.end())
      now = min(now, (*it2 - x) * p);
    if (it2 != yax.begin())
      now = min(now, (x - *(--it2)) * p);
    cost += now;
  }
  ans[cnt] = min(ans[cnt], cost);
}
void dfs(string s) {
  if ((int)s.length() == n) {
    solve(s);
    return;
  }
  dfs(s + '0');
  dfs(s + '1');
  dfs(s + '2');
}
int main() {
  cin >> n;
  rep(i, n) {
    ll x, y, p;
    cin >> x >> y >> p;
    m.emplace_back(x, y, p);
  }
  ans.assign(n + 1, LINF);
  dfs("");
  rep(i, n + 1) { cout << ans[i] << '\\n'; }
}