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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const ll INF = (ll)(1e18);

vector<ll> xx[(1 << 15) + 10];
vector<ll> yy[(1 << 15) + 10];

struct ST {
  ll x, y;
  ll p;
};

void prepare(int S, const vector<ST> &v) {
  int N = v.size();
  {
    rep(i, N) xx[S].push_back(abs(v[i].x));
    rep(i, N) {
      rep(j, N) {
        if ((S >> j) & 1) {
          xx[S][i] = min(xx[S][i], abs(v[i].x - v[j].x));
        }
      }
    }
  }
  {
    rep(i, N) yy[S].push_back(abs(v[i].y));
    rep(i, N) {
      rep(j, N) {
        if ((S >> j) & 1) {
          yy[S][i] = min(yy[S][i], abs(v[i].y - v[j].y));
        }
      }
    }
  }
}

ll calc(const vector<ST> &v, int T, int R) {
  int N = v.size();
  ll ret = 0;
  rep(i, N) { ret += min(xx[T][i], yy[R][i]) * v[i].p; }
  return ret;
}

int main() {
  int N;
  cin >> N;

  vector<ST> v;
  rep(i, N) {
    ST st;
    cin >> st.x >> st.y >> st.p;
    v.push_back(st);
  }

  for (int S = 0; S < (1 << N); S++) {
    prepare(S, v);
  }

  vector<ll> ret(N + 1, INF);

  for (int S = 0; S < (1 << N); S++) {
    int cnt = 0;
    rep(i, N) if ((S >> i) & 1) cnt++;
    for (int T = S; T >= 0; T--) {
      T &= S;
      int R = S ^ T;
      ret[cnt] = min(ret[cnt], calc(v, T, R));
    }
  }

  rep(i, ret.size()) { cout << ret[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const ll INF = (ll)(1e18);

struct ST {
  int x, y;
  int p;
};
vector<pair<int, int>> px, py;
vector<int> xx(20), yy(20);
vector<int> candx(20), candy(20);

ll calc(const vector<ST> &v, int T, int R) {
  int N = v.size();
  int cxi = 0, cyi = 0;
  rep(i, N) {
    {
      int idx = px[i].second;
      if ((T >> idx) & 1)
        candx[cxi++] = px[i].first;
    }
    {
      int idx = py[i].second;
      if ((R >> idx) & 1)
        candy[cyi++] = py[i].first;
    }
  }
  /*
    rep(i,N) if((T>>i)&1) cout << 1; else cout << 0;
    cout << "\\t";
    rep(i,N) if((R>>i)&1) cout << 1; else cout << 0;
    cout << endl;
   */

  rep(i, N) {
    xx[i] = abs(v[i].x);
    yy[i] = abs(v[i].y);
  }

  {
    int j = 0, k = 0;
    while (j < px.size()) {
      while (k < cxi && px[j].first >= candx[k]) {
        int idx = px[j].second;
        xx[idx] = min(xx[idx], abs(v[idx].x - candx[k]));
        k++;
      }
      if (k == cxi)
        break;
      int idx = px[j].second;
      if (k > 0)
        xx[idx] = min(xx[idx], abs(v[idx].x - candx[k - 1]));
      xx[idx] = min(xx[idx], abs(v[idx].x - candx[k]));
      j++;
    }
    if (cxi > 0) {
      while (j < px.size()) {
        int idx = px[j].second;
        xx[idx] = min(xx[idx], abs(v[idx].x - candx[cxi - 1]));
        j++;
      }
    }
  }
  {
    int j = 0, k = 0;
    while (j < py.size()) {
      while (k < cyi && py[j].first >= candy[k]) {
        int idx = py[j].second;
        yy[idx] = min(yy[idx], abs(v[idx].y - candy[k]));
        k++;
      }
      if (k == cyi)
        break;
      int idx = py[j].second;
      if (k > 0)
        yy[idx] = min(yy[idx], abs(v[idx].y - candy[k - 1]));
      yy[idx] = min(yy[idx], abs(v[idx].y - candy[k]));
      j++;
    }
    if (cyi > 0) {
      while (j < py.size()) {
        int idx = py[j].second;
        yy[idx] = min(yy[idx], abs(v[idx].y - candy[cyi - 1]));
        j++;
      }
    }
  }
  ll ret = 0;
  rep(i, N) { ret += min(xx[i], yy[i]) * (ll)v[i].p; }
  return ret;
}

int main() {
  int N;
  cin >> N;

  vector<ST> v;
  rep(i, N) {
    ST st;
    cin >> st.x >> st.y >> st.p;
    v.push_back(st);
  }

  rep(i, N) {
    px.emplace_back(v[i].x, i);
    py.emplace_back(v[i].y, i);
  }
  sort(ALLOF(px));
  sort(ALLOF(py));

  vector<ll> ret(N + 1, INF);

  for (int S = 0; S < (1 << N); S++) {
    int cnt = 0;
    rep(i, N) if ((S >> i) & 1) cnt++;
    for (int T = S; T >= 0; T--) {
      T &= S;
      int R = S ^ T;
      ret[cnt] = min(ret[cnt], calc(v, T, R));
    }
  }

  rep(i, ret.size()) { cout << ret[i] << endl; }

  return 0;
}