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

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

#define PPC(x) __builtin_popcountll(x)
typedef long long ll;

const int N = 16;

int n;
int x[N], y[N], p[N];
ll ans[N];
int dis[N];
int ordY[N], ordX[N];

// msk: 0(no line), 1(line)
// isx: only if msk bit=1 => 0(horizontal on y_i), 1(vertical on x_i)
void calc(int msk, int isx) {
  for (int i = 0; i < n; i++) {
    if (~msk & (1 << i))
      dis[i] = min(abs(x[i]), abs(y[i]));
    else
      dis[i] = 0;
  }
  int prvI = -1, nxtI = -1;
  for (int k = 0; k < n; k++) {
    int i = ordX[k];
    if (~msk & (1 << i)) {
      if (prvI != -1)
        dis[i] = min(dis[i], x[i] - x[prvI]);
    } else if (isx & (1 << i)) {
      prvI = i;
    }
  }
  for (int k = n - 1; k >= 0; k--) {
    int i = ordX[k];
    if (~msk & (1 << i)) {
      if (nxtI != -1)
        dis[i] = min(dis[i], x[nxtI] - x[i]);
    } else if (isx & (1 << i)) {
      nxtI = i;
    }
  }
  prvI = nxtI = -1;
  for (int k = 0; k < n; k++) {
    int i = ordY[k];
    if (~msk & (1 << i)) {
      if (prvI != -1)
        dis[i] = min(dis[i], y[i] - y[prvI]);
    } else if (~isx & (1 << i)) {
      prvI = i;
    }
  }
  for (int k = n - 1; k >= 0; k--) {
    int i = ordY[k];
    if (~msk & (1 << i)) {
      if (nxtI != -1)
        dis[i] = min(dis[i], y[nxtI] - y[i]);
    } else if (~isx & (1 << i)) {
      nxtI = i;
    }
  }
  ll res = 0;
  for (int i = 0; i < n; i++)
    res += dis[i] * 1LL * p[i];
  ans[PPC(msk)] = min(ans[PPC(msk)], res);
}

void rec(int i, int msk, int isx) {
  if (i == n) {
    calc(msk, isx);
    return;
  }
  rec(i + 1, msk, isx);
  rec(i + 1, msk | (1 << i), isx);
  rec(i + 1, msk | (1 << i), isx | (1 << i));
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
//	freopen("in.txt", "rt", stdin);
//	freopen("out.txt", "wt", stdout);
#endif
  cin >> n;
  ans[n] = LLONG_MAX;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
    ans[i] = LLONG_MAX;
    ordX[i] = i;
    ordY[i] = i;
  }
  sort(ordX, ordX + n, [&](int i, int j) {
    if (x[i] != x[j])
      return x[i] < x[j];
    return y[i] < y[j];
  });
  sort(ordY, ordY + n, [&](int i, int j) {
    if (y[i] != y[j])
      return y[i] < y[j];
    return x[i] < x[j];
  });
  rec(0, 0, 0);
  for (int i = 0; i <= n; i++)
    cout << ans[i] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define REP(i, n) for ((i) = 0; (i) < (n); (i)++)
#define rep(i, x, n) for ((i) = (x); (i) < (n); (i)++)
#define REV(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define FORIT(it, x) for ((it) = (x).begin(); (it) != (x).end(); (it)++)
#define foreach(it, c)                                                         \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define foreach2d(i, j, v)                                                     \\
  foreach (i, v)                                                               \\
    foreach (j, *i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define MMS(x, n) memset(x, n, sizeof(x))
#define mms(x, n, s) memset(x, n, sizeof(x) * s)
#define pb push_back
#define mp make_pair
#define NX next_permutation
#define UN(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define CV(x, n) count(all(x), (n))
#define FIND(x, n) find(all(x), (n)) - (x).begin()
#define ACC(x) accumulate(all(x), 0)
#define PPC(x) __builtin_popcountll(x)
#define LZ(x) __builtin_clz(x)
#define TZ(x) __builtin_ctz(x)
#define mxe(x) *max_element(all(x))
#define mne(x) *min_element(all(x))
#define low(x, i) lower_bound(all(x), i)
#define upp(x, i) upper_bound(all(x), i)
#define NXPOW2(x) (1ll << ((int)log2(x) + 1))
#define PR(x) cout << #x << " = " << (x) << endl;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int OO = (int)2e9;
const double eps = 1e-9;

const int N = 16;

int n;
int x[N], y[N], p[N];
ll ans[N];

ll calc(int valid, int isx) {
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (~valid & (1 << i)) {
      int cur = min(abs(x[i]), abs(y[i]));
      for (int j = 0; j < n; j++) {
        if (j != i && (valid & (1 << j))) {
          if (~isx & (1 << j))
            cur = min(cur, abs(y[i] - y[j]));
          else
            cur = min(cur, abs(x[i] - x[j]));
        }
      }
      res += cur * 1LL * p[i];
    }
  }
  return res;
}

void rec(int i, int valid, int isx) {
  if (i == n) {
    ans[PPC(valid)] = min(ans[PPC(valid)], calc(valid, isx));
    return;
  }
  rec(i + 1, valid, isx);
  rec(i + 1, valid | (1 << i), isx);
  rec(i + 1, valid | (1 << i), isx | (1 << i));
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
//	freopen("in.txt", "rt", stdin);
//	freopen("out.txt", "wt", stdout);
#endif
  cin >> n;
  ans[n] = LLONG_MAX;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i] >> p[i];
    for (int j = 0; j < i; j++) {
      if (x[i] < x[j]) {
        swap(x[i], x[j]);
        swap(y[i], y[j]);
        swap(p[i], p[j]);
      }
    }
    ans[i] = LLONG_MAX;
  }
  rec(0, 0, 0);
  for (int i = 0; i <= n; i++)
    cout << ans[i] << "\\n";
  return 0;
}