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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const ll INF = 1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  vector<vector<ll>> xmi((1 << N), vector<ll>(N, INF));
  rep(bit, (1 << N)) {
    rep(i, N) {
      rep(j, N) {
        if (bit & (1 << j))
          chmin(xmi[bit][i], abs(X[i] - X[j]));
      }
    }
  }
  vector<vector<ll>> ymi((1 << N), vector<ll>(N, INF));
  rep(bit, (1 << N)) {
    rep(i, N) {
      rep(j, N) {
        if (bit & (1 << j))
          chmin(ymi[bit][i], abs(Y[i] - Y[j]));
      }
    }
  }

  vector<ll> ans(31, INF);
  for (int xbit = 0; xbit < (1 << N); xbit++) {
    vector<ll> mires(N, INF);
    rep(i, N) mires[i] = min(abs(X[i]), abs(Y[i]));
    rep(i, N) chmin(mires[i], xmi[xbit][i]);

    int szx = __builtin_popcount(xbit);
    int T = 0;
    rep(i, N) {
      if (xbit & (1 << i))
        ;
      else
        T += (1 << i);
    }
    for (int S = T;; S = (S - 1) & T) {
      vector<ll> tres = mires;
      rep(i, N) chmin(tres[i], ymi[S][i]);
      int szy = __builtin_popcount(S);
      ll res = 0;
      rep(i, N) res += P[i] * tres[i];
      chmin(ans[szx + szy], res);
      if (S == 0)
        break;
    }
  }

  rep(i, N + 1) cout << ans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const ll INF = 1e18;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  vector<ll> ans(31, INF);
  for (int xbit = 0; xbit < (1 << N); xbit++) {
    vector<ll> mires(N, INF);
    rep(i, N) mires[i] = min(abs(X[i]), abs(Y[i]));
    vector<ll> ty;
    rep(i, N) {
      rep(j, N) {
        if (xbit & (1 << j))
          chmin(mires[i], abs(X[i] - X[j]));
      }
      if (xbit & (1 << i))
        ;
      else
        ty.push_back(i);
    }
    {
      ll res = 0;
      rep(i, N) res += P[i] * mires[i];
      chmin(ans[N], res);
    }

    int szx = __builtin_popcount(xbit);
    int M = ty.size();
    for (int ybit = 0; ybit < (1 << M); ybit++) {
      vector<ll> tres = mires;
      rep(i, N) {
        rep(j, M) {
          if (ybit & (1 << j))
            chmin(tres[i], abs(Y[i] - Y[ty[j]]));
        }
      }
      int szy = __builtin_popcount(ybit);
      ll res = 0;
      rep(i, N) res += P[i] * tres[i];
      chmin(ans[szx + szy], res);
    }
  }

  rep(i, N + 1) cout << ans[i] << endl;

  return 0;
}