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

#include <bits/stdc++.h>

using namespace std;
#define int long long
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<pair<int, int>, int> a[n];
  int x2[n];
  int y2[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first.first >> a[i].first.second >> a[i].second;
    x2[i] = a[i].first.first;
    y2[i] = a[i].first.second;
  }
  int ans[n + 1];
  for (int i = 0; i <= n; ++i)
    ans[i] = 1e18;
  int sz = (1 << n);
  vector<int> x3[sz];
  vector<int> y3[sz];
  for (int mask = 0; mask < sz; ++mask) {
    for (int i = 0; i < n; ++i) {
      if (mask & (1 << i)) {
        x3[mask].push_back(x2[i]);
        y3[mask].push_back(y2[i]);
      }
    }
    x3[mask].push_back(0);
    y3[mask].push_back(0);
    x3[mask].push_back(-1e9);
    x3[mask].push_back(1e9);
    y3[mask].push_back(-1e9);
    y3[mask].push_back(1e9);
    sort(x3[mask].begin(), x3[mask].end());
    sort(y3[mask].begin(), y3[mask].end());
  }
  for (int mask1 = 0; mask1 < sz; ++mask1) {
    for (int mask2 = mask1;; mask2 = (mask2 - 1) & mask1) {
      int mask = (sz - 1 - mask1);
      int sum = 0;
      for (int i = 0; i < n; ++i) {
        int o1 = x2[i];
        int o2 = y2[i];
        vector<int>::iterator it =
            lower_bound(x3[mask].begin(), x3[mask].end(), o1);
        int res1 = (*it - o1);
        it--;
        res1 = min(res1, o1 - *it);
        if (!res1)
          continue;
        it = lower_bound(y3[mask2].begin(), y3[mask2].end(), o2);
        int res2 = (*it - o2);
        it--;
        res2 = min(res2, o2 - *it);
        sum += min(res1, res2) * a[i].second;
      }
      int curr = __builtin_popcount(mask) + __builtin_popcount(mask2);
      ans[curr] = min(ans[curr], sum);
      if (mask2 == 0) {
        break;
      }
    }
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << ' ';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<pair<int, int>, int> a[n];
  int x2[n];
  int y2[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first.first >> a[i].first.second >> a[i].second;
    x2[i] = a[i].first.first;
    y2[i] = a[i].first.second;
  }
  int ans[n + 1];
  for (int i = 0; i <= n; ++i)
    ans[i] = 1e18;
  int sz = (1 << n);
  for (int mask1 = 0; mask1 < sz; ++mask1) {
    for (int mask2 = mask1;; mask2 = (mask2 - 1) & mask1) {
      int mask = (sz - 1 - mask1);
      vector<int> x;
      vector<int> y;
      x.push_back(0);
      y.push_back(0);
      for (int i = 0; i < n; ++i) {
        if (mask & (1 << i)) {
          x.push_back(x2[i]);
        }
        if (mask2 & (1 << i)) {
          y.push_back(y2[i]);
        }
      }
      x.push_back(-1e9);
      x.push_back(1e9);
      y.push_back(-1e9);
      y.push_back(1e9);
      sort(x.begin(), x.end());
      sort(y.begin(), y.end());
      int sum = 0;
      for (int i = 0; i < n; ++i) {
        int o1 = x2[i];
        int o2 = y2[i];
        vector<int>::iterator it = lower_bound(x.begin(), x.end(), o1);
        int res1 = (*it - o1);
        it--;
        res1 = min(res1, o1 - *it);
        it = lower_bound(y.begin(), y.end(), o2);
        int res2 = (*it - o2);
        it--;
        res2 = min(res2, o2 - *it);
        sum += min(res1, res2) * a[i].second;
      }
      int curr = __builtin_popcount(mask) + __builtin_popcount(mask2);
      ans[curr] = min(ans[curr], sum);
      if (mask2 == 0) {
        break;
      }
    }
  }
  for (int i = 0; i <= n; ++i) {
    cout << ans[i] << ' ';
  }
  return 0;
}