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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  vector<vl> p(n, vl(2));
  vector<ll> zzz(n);
  rep(i, n) cin >> p[i][0] >> p[i][1] >> zzz[i];
  vector<ll> ans(n + 1, INF);
  vvl dpx(n, vl(1 << n)), dpy(n, vl(1 << n));
  rep(bit, 1 << n) {
    rep(i, n) {
      ll ret = abs(p[i][0]);
      ll rey = abs(p[i][1]);
      rep(j, n) {
        if (bit & (1 << j)) {
          chmin(ret, abs(p[i][0] - p[j][0]));
          chmin(rey, abs(p[i][1] - p[j][1]));
        }
      }
      dpx[i][bit] = ret * zzz[i];
      dpy[i][bit] = rey * zzz[i];
    }
  }
  rep(bit, 1 << n) {
    ll t = __builtin_popcount(bit);
    vector<ll> use;
    rep(i, n) {
      if (bit & (1 << i))
        use.pb(i);
    }
    rep(cit, 1 << t) {
      ll ret = 0;
      ll x = 0, y = 0;
      rep(i, t) {
        if (cit & (1 << i)) {
          x += (1 << use[i]);
        } else {
          y += (1 << use[i]);
        }
      }
      rep(i, n) { ret += min(dpx[i][x], dpy[i][y]); }
      chmin(ans[t], ret);
    }
  }
  rep(i, n + 1) { cout << ans[i] << endl; }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define vtpl(x, y, z) vector<tuple<x, y, z>>
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -1, 0};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  vector<vl> p(n, vl(2));
  vector<ll> zzz(n);
  rep(i, n) cin >> p[i][0] >> p[i][1] >> zzz[i];
  vector<ll> ans(n + 1, INF);
  // vector<vl> line(0,vl(2));
  // vector<ll> x,y;x.pb(0),y.pb(0);
  rep(bit, 1 << n) {
    ll t = __builtin_popcount(bit);
    vector<ll> use, nuse;
    rep(i, n) {
      if (bit & (1 << i))
        use.pb(i);
      else
        nuse.pb(i);
    }
    rep(cit, 1 << t) {
      ll ret = 0;
      vector<ll> x, y;
      x.pb(0), y.pb(0);
      rep(i, t) {
        if (cit & (1 << i))
          x.pb(p[use[i]][0]);
        else
          y.pb(p[use[i]][1]);
      }
      for (auto xxx : nuse) {
        ll z = INF;
        for (auto pp : x) {
          chmin(z, abs(p[xxx][0] - pp));
        }
        for (auto pp : y) {
          chmin(z, abs(p[xxx][1] - pp));
        }
        ret += z * zzz[xxx];
      }
      chmin(ans[t], ret);
    }
  }
  rep(i, n + 1) { cout << ans[i] << endl; }
}