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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<bool> vb;
#define endl '\\n'
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#ifdef LOCAL
#include "../../trace.h"
#else
#define trace(args...)
#endif

ll div_floor(const ll &a, const ll &b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
ll div_ceil(const ll &a, const ll &b) {
  return a / b + (((a ^ b) >= 0) and a % b);
}

#define forn(i, n) for (ll i = 0; i < n; i++)

void solve() {
  ll n;
  cin >> n;
  vl x(n), y(n), p(n);
  forn(i, n) cin >> x[i] >> y[i] >> p[i];

  vvl leastx(n, vl(1 << n, 1e18)); // which x open
  vvl leasty(n, vl(1 << n, 1e18)); // which y open

  forn(i, n) forn(j, 1 << n) {
    leastx[i][j] = abs(x[i]);
    leasty[i][j] = abs(y[i]);
    forn(k, n) if ((j >> k) & 1)
        leastx[i][j] = min(leastx[i][j], abs(x[i] - x[k])),
        leasty[i][j] = min(leasty[i][j], abs(y[i] - y[k]));
  }
  // found all shortest distances
  ll tc = 1;
  for (ll i = 0; i < n; i++)
    tc *= 3;

  vl answer(n + 1, 1e18);

  for (ll i = 0; i < tc; i++) {

    ll temp = i;           // number
    ll cntr = 0;           // cnt
    ll repx = 0, repy = 0; // x, y
    while (temp > 0) {
      ll rem = temp % 3;
      if (rem == 2)
        repy += (1 << cntr);
      else if (rem == 1)
        repx += (1 << cntr);
      cntr++;
      temp = temp / 3;
    }

    cntr = __builtin_popcount(repy) + __builtin_popcount(repx);

    ll curr_sum = 0;
    for (ll j = 0; j < n; j++) // jth house
      curr_sum += p[j] * min(leastx[j][repx], leasty[j][repy]);
    answer[cntr] = min(answer[cntr], curr_sum);
  }
  for (auto &e : answer)
    cout << e << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef LOCAL
  freopen(".inp.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
  freopen(".err.txt", "w", stderr);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<bool> vb;
#define endl '\\n'
#define all(x) x.begin(), x.end()
#define sz(x) (ll) x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#ifdef LOCAL
#include "../../trace.h"
#else
#define trace(args...)
#endif

ll div_floor(const ll &a, const ll &b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
ll div_ceil(const ll &a, const ll &b) {
  return a / b + (((a ^ b) >= 0) and a % b);
}

void solve() {
  ll n;
  cin >> n;
  vl x(n), y(n), p(n);
  for (ll i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  ll tc = 1;
  for (ll i = 0; i < n; i++)
    tc *= 3;

  vl answer(n + 1, 1e18);
  vl curr(n, 0);
  for (ll i = 0; i < tc; i++) {
    // all cases get bitset representation
    ll temp = i;
    ll cntr = 0;
    for (ll j = 0; j < n; j++)
      curr[j] = temp % 3, cntr += (curr[j] > 0), temp = temp / 3;
    if (answer[cntr] == 0)
      continue;
    ll curr_sum = 0;
    for (ll j = 0; j < n; j++) { // jth house
      temp = min(abs(x[j]), abs(y[j]));
      for (ll k = 0; k < n; k++)
        if (curr[k] == 1)
          temp = min(temp, abs(x[j] - x[k]));
        else if (curr[k] == 2)
          temp = min(temp, abs(y[j] - y[k]));
      curr_sum += p[j] * temp;
    }
    answer[cntr] = min(answer[cntr], curr_sum);
  }
  for (auto &e : answer)
    cout << e << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef LOCAL
  freopen(".inp.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
  freopen(".err.txt", "w", stderr);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}