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

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

vector<int> x, y, p;
int p3[16];

int main() {
  p3[0] = 1;
  for (int i = 1; i < 16; i++)
    p3[i] = p3[i - 1] * 3;

  int n;
  while (scanf("%d", &n) == 1) {
    x.assign(n, 0);
    y.assign(n, 0);
    p.assign(n, 0);

    for (int i = 0; i < n; i++)
      scanf("%d %d %d", &x[i], &y[i], &p[i]);

    vector<int> lr(n), du(n);
    for (int i = 0; i < n; i++)
      lr[i] = i, du[i] = i;
    sort(lr.begin(), lr.end(), [](const int &i, const int &j) -> bool {
      return x[i] == x[j] ? y[i] < y[j] : x[i] < x[j];
    });
    sort(du.begin(), du.end(), [](const int &i, const int &j) -> bool {
      return y[i] == y[j] ? x[i] < x[j] : y[i] < y[j];
    });

    vector<long long> res(n + 1, 1e18);
    vector<int> vm(n);
    vector<int> _closest(n);
    for (int i = 0; i < n; i++)
      _closest[i] = min(abs(x[i]), abs(y[i]));
    for (int m = 0; m < p3[n]; m++) {
      int tm = m;
      for (int i = 0; i < n; i++) {
        vm[i] = tm % 3;
        tm /= 3;
      }

      int nonZero = 0;
      for (auto &x : vm)
        nonZero += x != 0;

      vector<int> closest = _closest;
      int l = -1e9;
      for (int i = 0; i < n; i++) {
        int id = lr[i];
        if (vm[id] == 1)
          l = x[id];
        closest[id] = min(x[id] - l, closest[id]);
      }

      int d = -1e9;
      for (int i = 0; i < n; i++) {
        int id = du[i];
        if (vm[id] == 2)
          d = y[id];
        closest[id] = min(y[id] - d, closest[id]);
      }

      int r = 1e9;
      for (int i = n - 1; i >= 0; i--) {
        int id = lr[i];
        if (vm[id] == 1)
          r = x[id];
        closest[id] = min(r - x[id], closest[id]);
      }

      int u = 1e9;
      for (int i = n - 1; i >= 0; i--) {
        int id = du[i];
        if (vm[id] == 2)
          u = y[id];
        closest[id] = min(u - y[id], closest[id]);
      }

      long long tres = 0;
      for (int i = 0; i < n; i++)
        tres += 1ll * closest[i] * p[i];
      res[nonZero] = min(res[nonZero], tres);
    }

    for (auto &x : res)
      printf("%lld\\n", x);
  }
}


Error: TLE

Fixed submission:

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

vector<int> x, y, p;
int p3[16];

int main() {
  p3[0] = 1;
  for (int i = 1; i < 16; i++)
    p3[i] = p3[i - 1] * 3;

  int n;
  while (scanf("%d", &n) == 1) {
    x.assign(n, 0);
    y.assign(n, 0);
    p.assign(n, 0);

    for (int i = 0; i < n; i++)
      scanf("%d %d %d", &x[i], &y[i], &p[i]);

    vector<int> lr(n), du(n);
    for (int i = 0; i < n; i++)
      lr[i] = i, du[i] = i;
    sort(lr.begin(), lr.end(), [](const int &i, const int &j) -> bool {
      return x[i] == x[j] ? y[i] < y[j] : x[i] < x[j];
    });
    sort(du.begin(), du.end(), [](const int &i, const int &j) -> bool {
      return y[i] == y[j] ? x[i] < x[j] : y[i] < y[j];
    });

    vector<long long> res(n + 1, 1e18);
    for (int m = 0; m < p3[n]; m++) {
      vector<int> vm;
      int tm = m;
      for (int i = 0; i < n; i++) {
        vm.push_back(tm % 3);
        tm /= 3;
      }

      int nonZero = 0;
      for (auto &x : vm)
        nonZero += x != 0;

      vector<int> closest(n);
      for (int i = 0; i < n; i++)
        closest[i] = min(abs(x[i]), abs(y[i]));

      int l = -1e9;
      for (int i = 0; i < n; i++) {
        int id = lr[i];
        if (vm[id] == 1)
          l = x[id];
        closest[id] = min(x[id] - l, closest[id]);
      }

      int d = -1e9;
      for (int i = 0; i < n; i++) {
        int id = du[i];
        if (vm[id] == 2)
          d = y[id];
        closest[id] = min(y[id] - d, closest[id]);
      }

      int r = 1e9;
      for (int i = n - 1; i >= 0; i--) {
        int id = lr[i];
        if (vm[id] == 1)
          r = x[id];
        closest[id] = min(r - x[id], closest[id]);
      }

      int u = 1e9;
      for (int i = n - 1; i >= 0; i--) {
        int id = du[i];
        if (vm[id] == 2)
          u = y[id];
        closest[id] = min(u - y[id], closest[id]);
      }

      long long tres = 0;
      for (int i = 0; i < n; i++)
        tres += 1ll * closest[i] * p[i];
      res[nonZero] = min(res[nonZero], tres);
    }

    for (auto &x : res)
      printf("%lld\\n", x);
  }
}