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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
#define tr(c, it) for (auto it = c.begin(); it != c.end(); it++)
#define cpresent(c, e) (find(all(c), e) != c.end())
#define present(c, e) (c.find(e) != c.end())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef unsigned long long lu;
const ll MOD = 1e9 + 7;

ll mincx[15][1 << 15];
ll mincy[15][1 << 15];

int main() {
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  vector<ll> ans(n + 1, 1ll * MOD * MOD);
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int j = 0; j < n; j++) {
      ll res = 1e18;
      for (int z = 0; z < n; z++) {
        if (mask & (1 << z))
          res = min(res, p[j] * abs(x[j] - x[z]));
      }
      res = min(res, p[j] * abs(x[j]));
      mincx[j][mask] = res;
    }
  }
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int j = 0; j < n; j++) {
      ll res = 1e18;
      for (int z = 0; z < n; z++) {
        if (mask & (1 << z))
          res = min(res, p[j] * abs(y[j] - y[z]));
      }
      res = min(res, p[j] * abs(y[j]));
      mincy[j][mask] = res;
    }
  }
  for (int mask = 0; mask < (1 << n); mask++) {
    int cnt = __builtin_popcount(mask);
    for (int submask = mask;; submask = (submask - 1) & mask) {
      int other = mask - submask;
      ll res = 0;
      for (int j = 0; j < n; j++) {
        res += min(mincx[j][submask], mincy[j][other]);
      }
      ans[cnt] = min(ans[cnt], res);
      if (submask == 0)
        break;
    }
  }
  for (int i = 0; i < n + 1; i++) {
    cout << ans[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define F first
#define S second
#define pb push_back
#define sz(c) (int)c.size()
#define vint vector<int>
#define vll vector<ll>
#define fr(it, a, b) for (int it = (a); it < (b); it++)
typedef pair<int, int> pi;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
ll cost(ll n, int m, int s, vector<pi> &p, vll &c) {
  ll ans = 0, now = 1;
  for (int i = 0; i < n; i++, now <<= 1) {
    if (m & now) {
      continue;
    }
    pi u = p[i];
    ll mn = 1e18, nw = 1;
    mn = min(llabs(u.S), llabs(u.F));
    fr(j, 0, n) {
      if (m & nw) {
        if (s & nw) {
          mn = min(mn, llabs(p[j].F - u.F));
        } else {
          mn = min(mn, llabs(p[j].S - u.S));
        }
      }
      nw <<= 1;
    }
    ans += mn * c[i];
  }
  return ans;
}
int32_t main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  vector<pi> p(n);
  vector<ll> c(n), val(n + 1, 1e18);
  fr(i, 0, n) { cin >> p[i].F >> p[i].S >> c[i]; }
  ll l = 1 << n;
  for (int m = 0; m < l; ++m) {
    int idx = __builtin_popcount(m);
    for (int s = m; s; s = (s - 1) & m) {
      val[idx] = min(val[idx], cost(n, m, s, p, c));
    }
    val[idx] = min(val[idx], cost(n, m, 0, p, c));
  }
  fr(i, 0, n + 1) { cout << val[i] << "\\n"; }
  return 0;
}