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

#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e6 * 2 + 10, INF = 1e9 * 1e9 + 10;

ll dp[100], bstX[35000][20], bstY[35000][20];

int main() {
  fastInp;

  ll n;
  cin >> n;

  vector<tuple<ll, ll, ll>> hs;
  for (int i = 0; i < n; i++) {
    ll x, y, p;
    cin >> x >> y >> p;
    hs.push_back({x, y, p});
  }

  ll pw = (1ll << n);
  for (int m = 0; m < pw; m++) {
    for (int i = 0; i < n; i++) {
      ll bst = abs(get<0>(hs[i]));
      for (int j = 0; j < n; j++) {
        if ((m >> j) & 1) {
          bst = min(bst, abs(get<0>(hs[i]) - get<0>(hs[j])));
        }
      }
      bstX[m][i] = bst;
    }
  }

  for (int m = 0; m < pw; m++) {
    for (int i = 0; i < n; i++) {
      ll bst = abs(get<1>(hs[i]));
      for (int j = 0; j < n; j++) {
        if ((m >> j) & 1) {
          bst = min(bst, abs(get<1>(hs[i]) - get<1>(hs[j])));
        }
      }
      bstY[m][i] = bst;
    }
  }

  vector<ll> bst(n + 1);
  for (int i = 0; i <= n; i++)
    bst[i] = INF;
  for (int m = 0; m < pw; m++) {
    ll vl = INF, cnt = 0;
    for (int s = m;; s = (s - 1) & m) {
      ll cur = 0;
      for (int i = 0; i < n; i++) {
        if ((m >> i) & 1)
          continue;

        ll d = min(abs(get<0>(hs[i])), abs(get<1>(hs[i])));

        ll xr = (m ^ s);
        d = min(bstX[xr][i], bstY[s][i]);

        cur += d * get<2>(hs[i]);
      }
      vl = min(vl, cur);
      if (s == 0)
        break;
    }

    for (int i = 0; i < n; i++) {
      if ((m >> i) & 1ll)
        cnt++;
    }

    bst[cnt] = min(bst[cnt], vl);
  }

  bst[n] = 0;
  for (auto cur : bst) {
    cout << cur << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e6 * 2 + 10, INF = 1e9 * 1e9 + 10;

ll dp[100];

int main() {
  fastInp;

  ll n;
  cin >> n;

  vector<tuple<ll, ll, ll>> hs;
  for (int i = 0; i < n; i++) {
    ll x, y, p;
    cin >> x >> y >> p;
    hs.push_back({x, y, p});
  }

  ll pw = (1ll << n);
  vector<ll> bst(n + 1);
  for (int i = 0; i <= n; i++)
    bst[i] = INF;
  for (int m = 0; m < pw; m++) {
    ll vl = INF, cnt = 0;
    for (int s = m;; s = (s - 1) & m) {
      ll cur = 0;
      for (int i = 0; i < n; i++) {
        if ((m >> i) & 1)
          continue;

        ll d = min(abs(get<0>(hs[i])), abs(get<1>(hs[i])));

        for (int j = 0; j < n; j++) {
          if ((m >> j) & 1) {
            if ((s >> j) & 1) {
              d = min(d, abs(get<0>(hs[j]) - get<0>(hs[i])));
            } else {
              d = min(d, abs(get<1>(hs[j]) - get<1>(hs[i])));
            }
          }
        }

        cur += d * get<2>(hs[i]);
      }
      vl = min(vl, cur);
      if (s == 0)
        break;
    }

    for (int i = 0; i < n; i++) {
      if ((m >> i) & 1ll)
        cnt++;
    }

    bst[cnt] = min(bst[cnt], vl);
  }

  for (auto cur : bst) {
    cout << cur << "\\n";
  }
  return 0;
}