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

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \\
  do {                                                                         \\
    cout << "[";                                                               \\
    for (int count = (a); count < (b); ++count)                                \\
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \\
    cout << "]\\n";                                                             \\
  } while (0)
#define p_map(map, it)                                                         \\
  do {                                                                         \\
    cout << "{";                                                               \\
    for (auto(it) = map.begin();; ++(it)) {                                    \\
      if ((it) == map.end()) {                                                 \\
        cout << "}\\n";                                                         \\
        break;                                                                 \\
      } else                                                                   \\
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \\
    }                                                                          \\
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\\n';

int main() {
  int n;
  cin >> n;
  ll x[15], y[15], p[15];
  for (int i = 0; i < n; ++i)
    cin >> x[i] >> y[i] >> p[i];
  vector<vector<ll>> mnx(1 << n, vector<ll>(n)), mny(1 << n, vector<ll>(n));
  vector<ll> dp(1 << n, 1e18);
  for (int s = 0; s < (1 << n); ++s) {
    for (int i = 0; i < n; ++i) {
      mnx[s][i] = abs(x[i]);
      for (int j = 0; j < n; ++j)
        if ((s >> j) & 1)
          mnx[s][i] = min(mnx[s][i], abs(x[i] - x[j]));
    }
  }
  for (int s = 0; s < (1 << n); ++s) {
    for (int i = 0; i < n; ++i) {
      mny[s][i] = abs(y[i]);
      for (int j = 0; j < n; ++j)
        if ((s >> j) & 1)
          mny[s][i] = min(mny[s][i], abs(y[i] - y[j]));
    }
  }
  vector<ll> ans(n + 1, 1e18);
  int t = 1;
  for (int i = 0; i < n; ++i)
    t *= 3;
  for (int s = 0; s < t; ++s) {
    int ss = s, bitx = 0, bity = 0, cnt = 0;
    for (int i = 0; i < n; ++i) {
      if (ss % 3 == 1)
        bitx += (1 << i), cnt++;
      else if (ss % 3 == 2)
        bity += (1 << i), cnt++;
      ss /= 3;
    }
    ll sum = 0;
    for (int i = 0; i < n; ++i)
      sum += min(mnx[bitx][i], mny[bity][i]) * p[i];
    ans[cnt] = min(ans[cnt], sum);
  }
  for (int i = 0; i < n + 1; ++i)
    cout << ans[i] << newl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \\
  do {                                                                         \\
    cout << "[";                                                               \\
    for (int count = (a); count < (b); ++count)                                \\
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \\
    cout << "]\\n";                                                             \\
  } while (0)
#define p_map(map, it)                                                         \\
  do {                                                                         \\
    cout << "{";                                                               \\
    for (auto(it) = map.begin();; ++(it)) {                                    \\
      if ((it) == map.end()) {                                                 \\
        cout << "}\\n";                                                         \\
        break;                                                                 \\
      } else                                                                   \\
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \\
    }                                                                          \\
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\\n';

void next_combination(int n, int k, vector<int> &ret) {
  if (ret[0] == n - k)
    return;
  int cnt = 1, b;
  while (ret.back() == n - cnt)
    ret.pop_back(), cnt++;
  b = ret.back();
  ret.pop_back();
  for (int i = b + 1; i < b + 1 + cnt; ++i)
    ret.push_back(i);
}

int main() {
  int n;
  cin >> n;
  ll x[15], y[15], p[15];
  for (int i = 0; i < n; ++i)
    cin >> x[i] >> y[i] >> p[i];
  for (int k = 0; k < n + 1; ++k) {
    ll ans = 1e18;
    vector<int> c(k);
    for (int i = 0; i < k; ++i)
      c[i] = i;
    ll cnt = 1;
    for (int i = n - k + 1; i <= n; ++i)
      cnt *= i;
    for (int i = 1; i <= k; ++i)
      cnt /= i;
    while (cnt) {
      // p_ary(c,0,k);
      cnt--;
      for (int s = 0; s < (1 << k); ++s) {
        ll sum = 0;
        for (int i = 0; i < n; ++i) {
          ll mn = min(abs(x[i]), abs(y[i]));
          for (int j = 0; j < k; ++j) {
            if ((s >> j) & 1)
              mn = min(abs(x[c[j]] - x[i]), mn);
            else
              mn = min(abs(y[c[j]] - y[i]), mn);
          }
          sum += mn * p[i];
        }
        ans = min(ans, sum);
      }
      if (cnt)
        next_combination(n, k, c);
    }
    cout << ans << newl;
  }
}