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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

const int INF = 1e9;
const int N_MAX = 20;

int N;
int x[N_MAX], y[N_MAX], p[N_MAX];
ll ans[N_MAX];
int X = 0, Y = 0;
int sx[N_MAX], sy[N_MAX], vx[N_MAX], vy[N_MAX];
bool use[N_MAX];

void dfs(int idx = 0) {
  if (idx == N) {
    ll sum = 0;
    int Min;
    int pcnt = X + Y;
    vx[0] = -INF;
    vx[1] = INF;
    vx[2] = 0;
    rep(i, X) vx[i + 3] = sx[i];
    vy[0] = -INF;
    vy[1] = INF;
    vy[2] = 0;
    rep(i, Y) vy[i + 3] = sy[i];
    sort(vx, vx + (X + 3));
    sort(vy, vy + (Y + 3));
    rep(i, N) {
      if (use[i])
        continue;
      int t = lower_bound(vx, vx + (X + 3), x[i]) - vx;
      Min = min(vx[t] - x[i], x[i] - vx[t - 1]);
      t = lower_bound(vy, vy + (Y + 3), y[i]) - vy;
      chmin(Min, min(vy[t] - y[i], y[i] - vy[t - 1]));
      sum += (ll)Min * p[i];
    }
    chmin(ans[pcnt], sum);
    return;
  }
  use[idx] = true;
  sx[X++] = x[idx];
  dfs(idx + 1);
  X--;
  sy[Y++] = y[idx];
  dfs(idx + 1);
  Y--;
  use[idx] = false;
  dfs(idx + 1);
}

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N) cin >> x[i] >> y[i] >> p[i];
  rep(i, N + 1) ans[i] = (ll)1e18;
  dfs();
  rep(i, N + 1) cout << ans[i] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

/* Some Libraries */

const int INF = 1e9;
const int N_MAX = 20;

int N;
int x[N_MAX], y[N_MAX], p[N_MAX];
ll ans[N_MAX];
int X = 0, Y = 0;
int sx[N_MAX], sy[N_MAX], vx[N_MAX], vy[N_MAX];

void dfs(int idx = 0) {
  if (idx == N) {
    ll sum = 0;
    int Min;
    int pcnt = X + Y;
    vx[0] = -INF;
    vx[1] = INF;
    vx[2] = 0;
    rep(i, X) vx[i + 3] = sx[i];
    vy[0] = -INF;
    vy[1] = INF;
    vy[2] = 0;
    rep(i, Y) vy[i + 3] = sy[i];
    sort(vx, vx + (X + 3));
    sort(vy, vy + (Y + 3));
    rep(i, N) {
      int t = lower_bound(vx, vx + (X + 3), x[i]) - vx;
      Min = min(vx[t] - x[i], x[i] - vx[t - 1]);
      t = lower_bound(vy, vy + (Y + 3), y[i]) - vy;
      chmin(Min, min(vy[t] - y[i], y[i] - vy[t - 1]));
      sum += (ll)Min * p[i];
    }
    chmin(ans[pcnt], sum);
    return;
  }
  sx[X++] = x[idx];
  dfs(idx + 1);
  X--;
  sy[Y++] = y[idx];
  dfs(idx + 1);
  Y--;
  dfs(idx + 1);
}

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N) cin >> x[i] >> y[i] >> p[i];
  rep(i, N + 1) ans[i] = (ll)1e18;
  dfs();
  rep(i, N + 1) cout << ans[i] << "\\n";
  return 0;
}