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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int n, x[15], y[15], p[15];
ll ans[16];
int main() {
  scanf("%d", &n);
  rep(i, n) { scanf("%d%d%d", &x[i], &y[i], &p[i]); }
  rep(i, 16) ans[i] = 1002003004005006001LL;
  int bitmx = 1;
  rep(i, n) bitmx *= 3;
  vi xordered(n), yordered(n);
  rep(i, n) xordered[i] = yordered[i] = i;
  sort(all(xordered), [](int a, int b) { return x[a] < x[b]; });
  sort(all(yordered), [](int a, int b) { return y[a] < y[b]; });
  rep(b, bitmx) { // 3 ^ 15
    int xs[15], ys[15];
    int xssize = 0, yssize = 0;
    int cnt = 0;
    int ybucket[15] = {};
    {
      int t = b;
      rep(i, n) {
        if (t % 3 == 1) {
          xs[xssize++] = x[xordered[i]];
          cnt++;
        } else if (t % 3 == 2) {
          ybucket[xordered[i]] = 1;
          cnt++;
        }
        t /= 3;
      }
    }
    rep(i, n) {
      int yi = yordered[i];
      if (ybucket[yi])
        ys[yssize++] = y[yi];
    }

    int d[15];
    rep(i, n) d[i] = min(abs(x[i]), abs(y[i]));
    if (xssize > 0) {
      int j = 0;
      for (int i : xordered) {
        while (j < xssize - 1 && xs[j + 1] < x[i])
          j++;
        if (xs[j] >= x[i])
          chmin(d[i], xs[j] - x[i]) else {
            chmin(d[i], x[i] - xs[j]);
            if (j < xssize - 1)
              chmin(d[i], xs[j + 1] - x[i])
          }
      }
    }
    if (yssize > 0) {
      int j = 0;
      for (int i : yordered) {
        while (j < yssize - 1 && ys[j + 1] < y[i])
          j++;
        if (ys[j] >= y[i])
          chmin(d[i], ys[j] - y[i]) else {
            chmin(d[i], y[i] - ys[j]) if (j < yssize - 1)
                chmin(d[i], ys[j + 1] - y[i])
          }
      }
    }
    ll s = 0;
    rep(i, n) s += (ll)d[i] * p[i];
    chmin(ans[cnt], s);
  }
  rep(i, n + 1) printf("%lld\\n", ans[i]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int n, x[15], y[15], p[15];
ll ans[16];
int main() {
  scanf("%d", &n);
  rep(i, n) { scanf("%d%d%d", &x[i], &y[i], &p[i]); }
  rep(i, 16) ans[i] = 1002003004005006001LL;
  int bitmx = 1;
  rep(i, n) bitmx *= 3;
  vi xordered(n), yordered(n);
  rep(i, n) xordered[i] = yordered[i] = i;
  sort(all(xordered), [](int a, int b) { return x[a] < x[b]; });
  sort(all(yordered), [](int a, int b) { return y[a] < y[b]; });
  rep(b, bitmx) { // 3 ^ 15
    int xs[15], ys[15];
    int xssize = 0, yssize = 0;
    int cnt = 0;
    int ybucket[15] = {};
    {
      int t = b;
      rep(i, n) {
        if (t % 3 == 1) {
          xs[xssize++] = x[xordered[i]];
          cnt++;
        } else if (t % 3 == 2) {
          ybucket[xordered[i]] = 1;
          cnt++;
        }
        t /= 3;
      }
    }
    rep(i, n) {
      int yi = yordered[i];
      if (ybucket[yi])
        ys[yssize++] = y[yi];
    }

    int d[15];
    rep(i, n) d[i] = min(abs(x[i]), abs(y[i]));
    if (xssize > 0) {
      int j = 0;
      for (int i : xordered) {
        while (j < xssize - 1 && xs[j + 1] < x[i])
          j++;
        if (xs[j] >= x[i])
          chmin(d[i], xs[j] - x[i]) else {
            chmin(d[i], x[i] - xs[j]);
            if (j < xssize - 1)
              chmin(d[i], xs[j + 1] - x[i])
          }
      }
    }
    if (yssize > 0) {
      int j = 0;
      for (int i : yordered) {
        while (j < yssize - 1 && ys[j + 1] < y[i])
          j++;
        if (ys[j] >= y[i])
          chmin(d[i], ys[j] - y[i]) else {
            chmin(d[i], y[i] - ys[j]) if (j < yssize - 1)
                chmin(d[i], ys[j + 1] - y[i])
          }
      }
    }
    ll s = 0;
    rep(i, n) s += (ll)d[i] * p[i];
    chmin(ans[cnt], s);
  }
  rep(i, n + 1) printf("%lld\\n", ans[i]);
}