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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll INF = 1e15;

struct city {
  ll x, y, p, ind;
};

bool xa(city x, city y) { return x.x < y.x; }

bool ya(city x, city y) { return x.y < y.y; }

int main() {
  vi pw3(16);
  pw3[0] = 1;
  FOR(i, 1, 15)
  pw3[i] = pw3[i - 1] * 3;
  int n;
  cin >> n;
  vector<city> ct(n);
  FOR(i, 0, n - 1) {
    cin >> ct[i].x >> ct[i].y >> ct[i].p;
    ct[i].ind = i;
  }
  vector<city> sx(ct), sy(ct);
  sort(all(sx), xa);
  sort(all(sy), ya);

  vvll xp(1 << n, vll(n)), yp(1 << n, vll(n));
  FOR(i, 0, (1 << n) - 1) {
    vi tx, ty;
    FOR(j, 0, n - 1) if (1 << j & i) {
      tx.pb(ct[j].x);
      ty.pb(ct[j].y);
    }

    tx.pb(0);
    ty.pb(0);
    // for (int k : tx) cout << k << ' ';
    // cout << endl;
    // for (int k : ty) cout << k << ' ';
    // cout << endl;
    FOR(j, 0, n - 1) {
      ll xmn = INF, ymn = INF;
      for (int k : tx)
        xmn = min(xmn, abs(k - ct[j].x));
      for (int k : ty)
        ymn = min(ymn, abs(k - ct[j].y));
      xp[i][j] = xmn, yp[i][j] = ymn;
      // cout << i << ' ' << j << ' ' << xp[i][j] << ' ' << yp[i][j] << endl;
    }
  }

  vll pc(n + 1, INF);
  FOR(j, 0, pw3[n] - 1) {
    int t = j;
    int xc = 0, yc = 0;
    int x_msk = 0, y_msk = 0;
    ROF(k, n - 1, 0) {
      if (t >= 2 * pw3[k]) {
        y_msk |= (1 << k);
        t -= 2 * pw3[k], yc++;
      } else if (t >= pw3[k]) {
        x_msk |= (1 << k);
        t -= pw3[k], xc++;
      }
    }
    ll sum = 0;
    FOR(i, 0, n - 1)
    sum += ct[i].p * min(xp[x_msk][i], yp[y_msk][i]);
    // cout << j << ' ' << sum << endl;
    int popcnt = xc + yc;
    // cout << popcnt << endl;
    pc[popcnt] = min(pc[popcnt], sum);
  }
  FOR(i, 0, n)
  cout << pc[i] << endl;

  // double tt = clock();
  // printf("%.3lf\\n", tt / CLOCKS_PER_SEC);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll INF = 1e15;

struct city {
  ll x, y, p, ind;
};

bool xa(city x, city y) { return x.x < y.x; }

bool ya(city x, city y) { return x.y < y.y; }

int main() {
  vi pw3(16);
  pw3[0] = 1;
  FOR(i, 1, 15)
  pw3[i] = pw3[i - 1] * 3;
  int n;
  cin >> n;
  vector<city> ct(n);
  FOR(i, 0, n - 1) {
    cin >> ct[i].x >> ct[i].y >> ct[i].p;
    ct[i].ind = i;
  }
  vector<city> sx(ct), sy(ct);
  sort(all(sx), xa);
  sort(all(sy), ya);
  vvi pos(n + 1);
  FOR(i, 0, pw3[n] - 1) {
    int cnt = 0, cur = i;
    ROF(j, n - 1, 0) {
      if (cur >= pw3[j])
        cur -= pw3[j], cnt++;
      if (cur >= pw3[j])
        cur -= pw3[j];
    }
    pos[cnt].pb(i);
  }
  FOR(e, 0, n) {
    ll ans = INF;
    for (int j : pos[e]) {
      int t = j;
      vi tx, ty;
      ROF(k, n - 1, 0) {
        if (t >= 2 * pw3[k]) {
          ty.pb(ct[k].x);
          t -= 2 * pw3[k];
        } else if (t >= pw3[k]) {
          tx.pb(ct[k].y);
          t -= pw3[k];
        }
      }
      tx.pb(0), ty.pb(0);
      sort(all(tx));
      sort(all(ty));
      // for (int i : tx) cout << i << ' ';
      // cout << '\\n';
      // for (int i : ty) cout << i << ' ';
      // cout << '\\n';
      vll mn(n, INF);
      int xp = 0, yp = 0;
      FOR(i, 0, n - 1) {
        while (yp + 1 < ty.size() &&
               abs(ty[yp + 1] - sx[i].x) <= abs(ty[yp] - sx[i].x))
          yp++;
        mn[sx[i].ind] = min(mn[sx[i].ind], abs(ty[yp] - sx[i].x));

        while (xp + 1 < tx.size() &&
               abs(tx[xp + 1] - sy[i].y) <= abs(tx[xp] - sy[i].y))
          xp++;
        mn[sy[i].ind] = min(mn[sy[i].ind], abs(tx[xp] - sy[i].y));
      }
      ll sum = 0;
      FOR(i, 0, n - 1)
      sum += ct[i].p * mn[i];
      ans = min(ans, sum);
    }
    cout << ans << endl;
  }
}