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

#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
using namespace std;
const int MAXN = 15;
const ll INF = 1e14;
pii p[MAXN];
ll v[MAXN];
int pow3[MAXN + 1];
ll ans[MAXN + 1], cost[MAXN];
ll disx[1 << MAXN][MAXN], disy[1 << MAXN][MAXN];
int numx[1 << MAXN], numy[1 << MAXN];
void sort_and_unique(vector<int> &vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %lld", &p[i].first, &p[i].second, &v[i]);
  }
  for (int sta = 0; sta < (1 << n); sta++) {
    vector<int> posx, posy;
    for (int i = 0; i < n; i++) {
      if (sta >> i & 1) {
        posx.push_back(p[i].first);
        posy.push_back(p[i].second);
      }
      int MINx = abs(p[i].first), MINy = abs(p[i].second);
      for (int j = 0; j < n; j++)
        if (sta >> j & 1) {
          MINx = min(MINx, abs(p[i].first - p[j].first));
          MINy = min(MINy, abs(p[i].second - p[j].second));
        }
      disx[sta][i] = MINx;
      disy[sta][i] = MINy;
    }
    sort_and_unique(posx);
    numx[sta] = posx.size();
    sort_and_unique(posy);
    numy[sta] = posy.size();
  }

  pow3[0] = 1;
  for (int i = 1; i <= n; i++)
    pow3[i] = pow3[i - 1] * 3;
  for (int i = 0; i <= n; i++)
    ans[i] = INF;
  for (int sta = 0; sta < pow3[n]; sta++) {
    vector<ll> xaxis, yaxis;
    int sx = 0, sy = 0;
    for (int i = 0; i < n; i++) {
      int r = (sta / pow3[i]) % 3;
      if (r == 1)
        sx |= (1 << i);
      else if (r == 2)
        sy |= (1 << i);
    }
    ll res = 0;
    for (int i = 0; i < n; i++)
      res += v[i] * min(disx[sx][i], disy[sy][i]);
    int cnt = numx[sx] + numy[sy];
    ans[cnt] = min(ans[cnt], res);
  }

  for (int i = 1; i <= n; i++)
    ans[i] = min(ans[i - 1], ans[i]);
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
using namespace std;
const int MAXN = 15;
const ll INF = 1e14;
pii p[MAXN];
ll v[MAXN];
ll xid[MAXN], yid[MAXN];
int pow3[MAXN + 1];
ll ans[MAXN + 1], cost[MAXN];
void sort_and_unique(vector<ll> &vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %lld", &p[i].first, &p[i].second, &v[i]);
    xid[i] = yid[i] = i;
  }
  sort(xid, xid + n, [&](int x, int y) { return p[x].first < p[y].first; });
  sort(yid, yid + n, [&](int x, int y) { return p[x].second < p[y].second; });
  pow3[0] = 1;
  for (int i = 1; i <= n; i++)
    pow3[i] = pow3[i - 1] * 3;
  for (int i = 0; i <= n; i++)
    ans[i] = INF;
  for (int sta = 0; sta < pow3[n]; sta++) {
    vector<ll> xaxis, yaxis;
    for (int i = 0; i < n; i++) {
      int r = (sta / pow3[i]) % 3;
      if (r == 1)
        xaxis.push_back(p[i].first);
      else if (r == 2)
        yaxis.push_back(p[i].second);
    }
    xaxis.push_back(-1e9);
    xaxis.push_back(1e9);
    xaxis.push_back(0);
    yaxis.push_back(-1e9);
    yaxis.push_back(1e9);
    yaxis.push_back(0);
    sort_and_unique(xaxis);
    sort_and_unique(yaxis);
    for (int i = 0; i < n; i++)
      cost[i] = INF;
    int now = 0;
    for (int i = 0; i < n; i++) {
      int x = xid[i];
      while (p[x].first >= xaxis[now])
        now++;
      cost[x] = min(cost[x], v[x] * min(abs(p[x].first - xaxis[now - 1]),
                                        abs(p[x].first - xaxis[now])));
    }
    now = 0;
    for (int i = 0; i < n; i++) {
      int y = yid[i];
      while (p[y].second >= yaxis[now])
        now++;
      cost[y] = min(cost[y], v[y] * min(abs(p[y].second - yaxis[now - 1]),
                                        abs(p[y].second - yaxis[now])));
    }
    ll res = 0;
    for (int i = 0; i < n; i++)
      res += cost[i];
    int cnt = xaxis.size() + yaxis.size() - 6;
    ans[cnt] = min(ans[cnt], res);
  }

  for (int i = 1; i <= n; i++)
    ans[i] = min(ans[i - 1], ans[i]);
  for (int i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}