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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

bool next_one(int *s, int *t, int k) {
  while (s < t) {
    if (++*s < k)
      return true;
    *(s++) = 0;
  }
  return false;
}

constexpr int N = 32;
int x[2][N];
ll z[N];

int p[N] = {};
int m[2][1 << 15][N];
int q[N] = {};
ll ans[N];

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d%d%lld", x[0] + i, x[1] + i, z + i);

  REP(h, 2) {
    REP(t, 1 << n) {
      REP(i, n) {
        int &y = m[h][t][i];
        y = abs(x[h][i]);
        REP(j, n) {
          if (t & (1 << j)) {
            SML(y, abs(x[h][i] - x[h][j]));
          }
        }
      }
    }
  }

  fill(ans, ans + N, LLONG_MAX);
  do {
    fill(q, q + n, INT_MAX);
    REP(h, 2) {
      int t = 0;
      REP(i, n) if (p[i] == h) t |= 1 << i;
      REP(i, n) SML(q[i], m[h][t][i]);
    }
    ll sm = 0;
    REP(i, n) sm += z[i] * q[i];
    int b = 0;
    REP(i, n) if (p[i] < 2) b++;
    SML(ans[b], sm);
  } while (next_one(p, p + n, 3));

  REP(i, n + 1) printf("%lld\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

bool next_one(int *s, int *t, int k) {
  while (s < t) {
    if (++*s < k)
      return true;
    *(s++) = 0;
  }
  return false;
}

constexpr int N = 32;
int x[2][N], y[2][N];
ll z[N];

map<int, int> xs;

int p[N] = {};
int m[N];
vector<int> v[2][N];
bool u[2][N];
int w[2][N];
ll ans[N];

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%d%d%lld", x[0] + i, x[1] + i, z + i);
  int f[2], g[2];
  REP(h, 2) {
    xs.clear();
    xs[0] = xs[-30000] = xs[30000] = 0;
    REP(i, n) xs[x[h][i]] = 0;
    {
      int i = 0;
      for (auto &p : xs) {
        w[h][i] = p.first;
        p.second = i++;
      }
    }
    REP(i, n) {
      int xx = xs[x[h][i]];
      v[h][xx].EB(i);
      y[h][i] = xx;
    }
    f[h] = xs[0];
    g[h] = xs[30000];

    for (auto p : xs)
      DEBUG("%d: %d\\n", p.first, p.second);
  }

  fill(ans, ans + N, LLONG_MAX);
  do {
    fill(m, m + N, INT_MAX);

    int b = 0;
    REP(h, 2) REP(i, N) u[h][i] = false;
    REP(h, 2) u[h][0] = u[h][f[h]] = u[h][g[h]] = true;
    REP(i, n) if (p[i] < 2) b += u[p[i]][y[p[i]][i]] = true;

    REP(i, n) DEBUG("%d", p[i]);
    DEBUG("\\n");
    REP(h, 2) {
      DEBUG("  - ");
      REP(i, g[h] + 1) DEBUG("%d", u[h][i]);
      DEBUG("\\n");
    }

    REP(h, 2) {
      DEBUG(" h=%d\\n", h);
      for (int s = 0, t = 1; t <= g[h]; t++) {
        while (!u[h][t])
          t++;
        int c = w[h][s], d = w[h][t];
        DEBUG("  [%d, %d] => [%d, %d]\\n", s, t, c, d);
        FOR(k, s, t) for (int i : v[h][k]) {
          DEBUG("    %d: %d\\n", i, x[h][i]);
          DEBUG("     => m[i]=%d\\n", m[i]);
          SML(m[i], min(abs(x[h][i] - c), abs(x[h][i] - d)));
          DEBUG("     => m[i]=%d = min(%d, %d)\\n", m[i], abs(x[h][i] - c),
                abs(x[h][i] - d));
        }
        s = t;
      }
    }

    ll sm = 0;
    REP(i, n) {
      DEBUG("  %d: %lld*%d\\n", i, z[i], m[i]);
      sm += z[i] * m[i];
    }
    SML(ans[b], sm);
    DEBUG(" %d => %lld\\n", b, sm);
  } while (next_one(p, p + n, 3));

  REP(i, n + 1) printf("%lld\\n", ans[i]);
}