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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<ll, ll>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const ll INF = 1E+18;

int main() {
  ll n;
  cin >> n;
  vector<pair<P, ll>> a(n);
  rep(i, n) cin >> a[i].first.first >> a[i].first.second >> a[i].second;
  sort(whole(a));
  vector<ll> ans(n + 1, INF);
  ll all = 1;
  rep(i, n) { all *= 3; }
  vector<vector<ll>> distx(1LL << n, vector<ll>(n));
  vector<vector<ll>> disty(1LL << n, vector<ll>(n));

  rep(i, 1LL << n) {
    rep(j, n) {
      distx[i][j] = abs(a[j].first.first);
      disty[i][j] = abs(a[j].first.second);
      rep(k, n) {
        if (i >> k & 1) {
          chmin(distx[i][j], abs(a[j].first.first - a[k].first.first));
          chmin(disty[i][j], abs(a[j].first.second - a[k].first.second));
        }
      }
    }
  }
  rep(xi, all) {
    vector<ll> v(n);
    ll x = xi;
    ll lx = 0;
    ll ly = 0;
    ll num = 0;
    rep(i, n) {
      v[i] = x % 3;
      x /= 3;
      if (v[i] == 1) {
        num++;
        lx += 1LL << i;
      }
      if (v[i] == 2) {
        num++;
        ly += 1LL << i;
      }
    }
    ll sum = 0;
    rep(i, n) {
      ll res = min(distx[lx][i], disty[ly][i]);
      res *= a[i].second;
      sum += res;
    }
    chmin(ans[num], sum);
  }
  rep(i, n + 1) { cout << ans[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(), (x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<ll, ll>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const ll INF = 1E+18;

int main() {
  ll n;
  cin >> n;
  vector<pair<P, ll>> a(n);
  rep(i, n) cin >> a[i].first.first >> a[i].first.second >> a[i].second;
  sort(whole(a));
  vector<ll> ans(n + 1, INF);
  ll all = 1;
  rep(i, n) { all *= 3; }
  rep(xi, all) {
    vector<ll> v(n);
    ll x = xi;
    rep(i, n) {
      v[i] = x % 3;
      x /= 3;
    }
    vector<ll> lx;
    vector<ll> ly;
    lx.push_back(0);
    lx.push_back(INF);
    lx.push_back(-INF);
    ly.push_back(0);
    ly.push_back(INF);
    ly.push_back(-INF);
    ll num = 0;
    rep(i, n) {
      if (v[i] == 1) {
        lx.push_back(a[i].first.first);
        num++;
      }
      if (v[i] == 2) {
        ly.push_back(a[i].first.second);
        num++;
      }
    }
    sort(whole(lx));
    sort(whole(ly));
    ll sum = 0;
    rep(i, n) {
      if (v[i] != 0)
        continue;
      auto it1 = lower_bound(whole(lx), a[i].first.first);
      ll res = *it1 - a[i].first.first;
      it1--;
      chmin(res, a[i].first.first - *it1);
      auto it2 = lower_bound(whole(ly), a[i].first.second);
      chmin(res, *it2 - a[i].first.second);
      it2--;
      chmin(res, a[i].first.second - *it2);
      res *= a[i].second;
      sum += res;
    }
    chmin(ans[num], sum);
  }
  rep(i, n + 1) { cout << ans[i] << endl; }
  return 0;
}