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

/*
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long INF = 1000000000000;
int pow3(int x) {
  int ans = 1;
  for (int i = 0; i < x; i++) {
    ans *= 3;
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  vector<int> X(N), Y(N);
  vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  vector<pair<int, int>> xs;
  xs.reserve(N + 1);
  for (int i = 0; i < N; i++) {
    xs.emplace_back(X[i], i);
  }
  xs.emplace_back(0, N);
  sort(xs.begin(), xs.end());
  vector<pair<int, int>> ys;
  ys.reserve(N + 1);
  for (int i = 0; i < N; i++) {
    ys.emplace_back(Y[i], i);
  }
  ys.emplace_back(0, N);
  sort(ys.begin(), ys.end());
  vector<long long> ans(N + 1, INF);
  vector<int> H, V;
  H.reserve(N + 2);
  V.reserve(N + 2);
  vector<int> a(N + 1, 0);
  vector<int> d(N, 1000000);
  int u = pow3(N);
  int cnt, tmp, A, B;
  for (int i = 0; i < u; i++) {
    cnt = 0;
    tmp = i;
    for (int j = 0; j < N; j++) {
      if ((a[j] = tmp % 3) > 0) {
        cnt++;
      }
      tmp /= 3;
    }
    a[N] = 1;
    H.clear();
    H.emplace_back(-1000000);
    for (auto P : ys) {
      if (a[P.second] == 1) {
        H.emplace_back(P.first);
      }
    }
    H.emplace_back(1000000);
    a[N] = 2;
    V.clear();
    V.emplace_back(-1000000);
    for (auto P : xs) {
      if (a[P.second] == 2) {
        V.emplace_back(P.first);
      }
    }
    V.emplace_back(1000000);
    A = 0;
    int tmp1 = H.size() - 1;
    for (auto p : ys) {
      if (p.second == N) {
        continue;
      }
      while (A < tmp1) {
        if (H[A + 1] < p.first) {
          A++;
        } else {
          break;
        }
      }
      d[p.second] = min(p.first - H[A], H[A + 1] - p.first);
    }
    B = 0;
    int tmp2 = V.size() - 1;
    for (auto p : xs) {
      if (p.second == N) {
        continue;
      }
      while (B < tmp2) {
        if (V[B + 1] < p.first) {
          B++;
        } else {
          break;
        }
      }
      d[p.second] = min(d[p.second], min(p.first - V[B], V[B + 1] - p.first));
    }
    long long S = 0;
    for (int i = 0; i < N; i++) {
      S += d[i] * P[i];
    }
    ans[cnt] = min(ans[cnt], S);
  }
  for (int i = 0; i <= N; i++) {
    cout << ans[i] << "\\n";
  }
}

Error: TLE

Fixed submission:

/*
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long INF = 1000000000000;
int pow3(int x) {
  int ans = 1;
  for (int i = 0; i < x; i++) {
    ans *= 3;
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  vector<int> X(N), Y(N);
  vector<long long> P(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> P[i];
  }
  vector<pair<int, int>> xs;
  xs.reserve(N + 1);
  for (int i = 0; i < N; i++) {
    xs.emplace_back(X[i], i);
  }
  xs.emplace_back(0, N);
  sort(xs.begin(), xs.end());
  vector<pair<int, int>> ys;
  ys.reserve(N + 1);
  for (int i = 0; i < N; i++) {
    ys.emplace_back(Y[i], i);
  }
  ys.emplace_back(0, N);
  sort(ys.begin(), ys.end());
  vector<long long> ans(N + 1, INF);
  vector<int> H, V;
  H.reserve(N + 2);
  V.reserve(N + 2);
  vector<int> a(N + 1, 0);
  vector<int> d(N, 1000000);
  int u = pow3(N);
  int cnt, tmp, A, B;
  for (int i = 0; i < u; i++) {
    cnt = 0;
    tmp = i;
    for (int j = 0; j < N; j++) {
      a[j] = tmp % 3;
      tmp /= 3;
      if (a[j] > 0) {
        cnt++;
      }
    }
    a[N] = 1;
    H.clear();
    H.emplace_back(-1000000);
    for (auto P : ys) {
      if (a[P.second] == 1) {
        H.emplace_back(P.first);
      }
    }
    H.emplace_back(1000000);
    a[N] = 2;
    V.clear();
    V.emplace_back(-1000000);
    for (auto P : xs) {
      if (a[P.second] == 2) {
        V.emplace_back(P.first);
      }
    }
    V.emplace_back(1000000);
    A = 0;
    int tmp1 = H.size() - 1;
    for (auto p : ys) {
      if (p.second == N) {
        continue;
      }
      while (A < tmp1) {
        if (H[A + 1] < p.first) {
          A++;
        } else {
          break;
        }
      }
      d[p.second] = min(p.first - H[A], H[A + 1] - p.first);
    }
    B = 0;
    int tmp2 = V.size() - 1;
    for (auto p : xs) {
      if (p.second == N) {
        continue;
      }
      while (B < tmp2) {
        if (V[B + 1] < p.first) {
          B++;
        } else {
          break;
        }
      }
      d[p.second] = min(d[p.second], min(p.first - V[B], V[B + 1] - p.first));
    }
    long long S = 0;
    for (int i = 0; i < N; i++) {
      S += d[i] * P[i];
    }
    ans[cnt] = min(ans[cnt], S);
  }
  for (int i = 0; i <= N; i++) {
    cout << ans[i] << "\\n";
  }
}