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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
constexpr ll INF = (1LL << 60);

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n);
  vector<ll> p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];

  vector<ll> dp(n + 1, INF);

  vector<vector<ll>> xcost(1 << n, vector<ll>(n));
  vector<vector<ll>> ycost(1 << n, vector<ll>(n));

  for (int bit = 0; bit < (1 << n); bit++) {
    for (int i = 0; i < n; i++) {
      xcost[bit][i] = abs(x[i]);
      ycost[bit][i] = abs(y[i]);

      for (int j = 0; j < n; j++) {
        if (bit & (1 << j)) {
          xcost[bit][i] = min(xcost[bit][i], (ll)abs(x[i] - x[j]));
          ycost[bit][i] = min(ycost[bit][i], (ll)abs(y[i] - y[j]));
        }
      }
    }
  }

  for (int bit = 0; bit < (1 << n); bit++) {
    for (int i = 0; i < n; i++)
      xcost[bit][i] *= p[i], ycost[bit][i] *= p[i];
  }

  for (int bit = 0; bit < (1 << n); bit++) {
    int cnt = __builtin_popcount(bit);

    for (int i = bit; i >= 0; i--) {
      i &= bit;
      ll tmp = 0;

      for (int j = 0; j < n; j++) {
        if (!(bit & (1 << j)))
          tmp += min(xcost[i][j], ycost[bit - i][j]);
      }
      dp[cnt] = min(dp[cnt], tmp);
    }
  }

  for (int i = 0; i <= n; i++)
    cout << dp[i] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
constexpr ll INF = (1LL << 60);

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n);
  vector<ll> p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  auto cul = x, row = y;

  sort(cul.begin(), cul.end());
  cul.erase(unique(cul.begin(), cul.end()), cul.end());
  sort(row.begin(), row.end());
  row.erase(unique(row.begin(), row.end()), row.end());

  int s = cul.size(), t = row.size();

  vector<vector<int>> a(s + 1), b(t + 1);

  for (int bit = 0; bit < (1 << s); bit++) {
    int j = __builtin_popcount(bit);
    a[j].emplace_back(bit);
  }

  for (int bit = 0; bit < (1 << t); bit++) {
    int j = __builtin_popcount(bit);
    b[j].emplace_back(bit);
  }

  vector<ll> res(n + 1, INF);

  auto ok = [&](int i, int f1, int f2) {
    vector<vector<int>> na(2);
    for (int l = 0; l < s; l++)
      if (f1 & (1 << l))
        na[0].emplace_back(cul[l]);
    for (int l = 0; l < t; l++)
      if (f2 & (1 << l))
        na[1].emplace_back(row[l]);

    vector<int> dist(2);

    for (int c = 0; c < 2; c++) {
      int p = (c ? y[i] : x[i]);
      dist[c] = abs(p);
      int r = lower_bound(na[c].begin(), na[c].end(), p) - na[c].begin();
      int l = r - 1;
      if (r < na[c].size())
        dist[c] = min(dist[c], abs(na[c][r] - p));
      if (l >= 0)
        dist[c] = min(dist[c], abs(na[c][l] - p));
    }

    return min(dist[0], dist[1]);
  };

  auto calc = [&](int x, int y) {
    ll res = INF;

    for (int i = 0; i < a[x].size(); i++) {
      for (int j = 0; j < b[y].size(); j++) {
        int f1 = a[x][i], f2 = b[y][j];
        ll tmp = 0;
        for (int k = 0; k < n; k++) {
          ll dist = ok(k, f1, f2);
          tmp += p[k] * dist;
        }
        // cout << tmp << endl;
        res = min(res, tmp);
      }
    }
    return res;
  };

  vector<ll> ans(2 * n + 1, INF);

  for (int i = 0; i <= s; i++) {
    for (int j = 0; j <= t; j++) {
      ans[i + j] = min(ans[i + j], calc(i, j));
    }
  }

  ll cur = INF;

  for (int i = 0; i <= n; i++) {
    cur = min(cur, ans[i]);
    cout << cur << endl;
  }
  return 0;
}