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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

using VLL = vector<long long>;
using VPILL = vector<pair<long long, int>>;
using VB = vector<bool>;
int N;
VLL X;
VLL Y;
VLL P;
VLL ans;
VPILL PX;
VPILL PY;
VB xcho;
VB ycho;

const long long INF = 1000000000LL * 1000000000LL;

void solve(int now, int chosen) {
  if (now == N) {
    VLL minDist(N, INF);

    long long res = 0;
    long long lastX = -INF;
    long long lastY = -INF;

    for (int i = 0; i < N; ++i) {
      // default
      minDist[i] = min(minDist[i], min(abs(X[i]), abs(Y[i])));

      // x axis
      if (xcho[PX[i].second]) {
        lastX = PX[i].first;
      }

      minDist[PX[i].second] = min(minDist[PX[i].second], PX[i].first - lastX);

      // y axis
      if (ycho[PY[i].second]) {
        lastY = PY[i].first;
      }

      minDist[PY[i].second] = min(minDist[PY[i].second], PY[i].first - lastY);
    }

    // backward
    lastX = INF;
    lastY = INF;

    for (int i = N - 1; i >= 0; --i) {
      // x axis
      if (xcho[PX[i].second]) {
        lastX = PX[i].first;
      }

      minDist[PX[i].second] = min(minDist[PX[i].second], lastX - PX[i].first);

      // y axis
      if (ycho[PY[i].second]) {
        lastY = PY[i].first;
      }

      minDist[PY[i].second] = min(minDist[PY[i].second], lastY - PY[i].first);
    }

    for (int i = 0; i < N; ++i) {
      res += P[i] * minDist[i];
    }

    ans[chosen] = min(ans[chosen], res);
    return;
  }

  solve(now + 1, chosen);

  xcho[now] = true;
  solve(now + 1, chosen + 1);
  xcho[now] = false;

  ycho[now] = true;
  solve(now + 1, chosen + 1);
  ycho[now] = false;
}

int main() {
  cin >> N;

  X = VLL(N);
  Y = VLL(N);
  P = VLL(N);

  PX = VPILL(N);
  PY = VPILL(N);

  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> P[i];
    PX[i] = make_pair(X[i], i);
    PY[i] = make_pair(Y[i], i);
  }

  ans = VLL(N + 1, INF);
  xcho = VB(N);
  ycho = VB(N);

  sort(PX.begin(), PX.end());
  sort(PY.begin(), PY.end());
  solve(0, 0);

  for (int k = 0; k <= N; ++k) {
    cout << ans[k] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

using VI = vector<int>;
using VLL = vector<long long>;
using VPII = vector<pair<int, int>>;

int mypow(int n, int p) {
  if (p == 0) {
    return 1;
  }

  int half = mypow(n, p / 2);
  return half * half * (1 + (p & 1) * (n - 1));
}

int main() {
  const int IINF = 1000000000;
  const long long INF = 1000000000LL * 1000000000LL;

  int N;
  cin >> N;

  VI X(N);
  VI Y(N);
  VI P(N);
  VPII PX(N);
  VPII PY(N);
  VPII RPX(N);
  VPII RPY(N);
  VI defaultMinDist(N, IINF);

  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> P[i];
    PX[i] = make_pair(X[i], i);
    PY[i] = make_pair(Y[i], i);

    defaultMinDist[i] = min(defaultMinDist[i], min(abs(X[i]), abs(Y[i])));
  }

  int superset = mypow(3, N);
  VLL ans(N + 1, INF);

  sort(PX.begin(), PX.end());
  sort(PY.begin(), PY.end());

  RPX = PX;
  RPY = PY;

  reverse(RPX.begin(), RPX.end());
  reverse(RPY.begin(), RPY.end());

  vector<int> powPrecal(N);
  powPrecal[0] = 1;
  for (int i = 1; i < N; ++i) {
    powPrecal[i] = powPrecal[i - 1] * 3;
  }

  for (int sub = 0; sub < superset; ++sub) {
    long long res = 0;

    VI minDist(defaultMinDist);

    // forward
    int cho = 0;
    int lastX = -IINF;
    int lastY = -IINF;
    int rlastX = IINF;
    int rlastY = IINF;

    for (int i = 0; i < N; ++i) {
      // x axis
      if (sub / powPrecal[PX[i].second] % 3 == 1) {
        lastX = PX[i].first;
        ++cho;
      }

      minDist[PX[i].second] = min(minDist[PX[i].second], PX[i].first - lastX);

      // y axis
      if (sub / powPrecal[PY[i].second] % 3 == 2) {
        lastY = PY[i].first;
        ++cho;
      }

      minDist[PY[i].second] = min(minDist[PY[i].second], PY[i].first - lastY);

      // reverse x axis
      if (sub / powPrecal[RPX[i].second] % 3 == 1) {
        rlastX = RPX[i].first;
      }

      minDist[RPX[i].second] =
          min(minDist[RPX[i].second], rlastX - RPX[i].first);

      // reverse y axis
      if (sub / powPrecal[RPY[i].second] % 3 == 2) {
        rlastY = RPY[i].first;
      }

      minDist[RPY[i].second] =
          min(minDist[RPY[i].second], rlastY - RPY[i].first);
    }

    for (int i = 0; i < N; ++i) {
      res += P[i] * (long long)minDist[i];
    }

    ans[cho] = min(ans[cho], res);
  }

  for (int k = 0; k <= N; ++k) {
    cout << ans[k] << endl;
  }

  return 0;
}