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

#include <bits/stdc++.h>
using namespace std;
struct fastio {
  fastio() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  }
} _io;
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

constexpr long long INF = (1LL << 60);

int main() {
  int n;
  std::cin >> n;
  const int ALL = (1 << n) - 1;
  std::vector<long long> x(n, 0), y(n, 0), p(n, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> x[i] >> y[i] >> p[i];
  }

  std::vector<long long> ans(n + 1, INF), base(n, INF);
  for (int i = 0; i < n; i++)
    base[i] = std::min(std::abs(x[i]), std::abs(y[i]));
  std::vector<std::vector<long long>> mx((1 << n) + 10), my((1 << n) + 10);

  for (int s = 0; s < (1 << n); s++) {
    int se = (ALL ^ s);
    for (int t = se; t > 0; t = (t - 1) & se) {
      std::vector<long long> cost = base;
      if (mx[s].size()) {
        for (int i = 0; i < n; i++)
          chmin(cost[i], mx[s][i]);
      } else {
        mx[s].resize(n, INF);
        for (int i = 0; i < n; i++)
          if (s >> i & 1) {
            for (int j = 0; j < n; j++) {
              chmin(cost[j], std::abs(x[j] - x[i]));
              chmin(mx[s][j], std::abs(x[j] - x[i]));
            }
          }
      }
      if (my[t].size()) {
        for (int i = 0; i < n; i++)
          chmin(cost[i], my[t][i]);
      } else {
        my[t].resize(n, INF);
        for (int i = 0; i < n; i++)
          if (t >> i & 1) {
            for (int j = 0; j < n; j++) {
              chmin(cost[j], std::abs(y[j] - y[i]));
              chmin(my[t][j], std::abs(y[j] - y[i]));
            }
          }
      }
      int id = __builtin_popcount(s) + __builtin_popcount(t);
      long long value = 0;
      for (int i = 0; i < n; i++)
        value += cost[i] * p[i];
      chmin(ans[id], value);
    }
    std::vector<long long> cost = base;
    if (mx[s].size()) {
      for (int i = 0; i < n; i++)
        chmin(cost[i], mx[s][i]);
    } else {
      mx[s].resize(n, INF);
      for (int i = 0; i < n; i++)
        if (s >> i & 1) {
          for (int j = 0; j < n; j++) {
            chmin(cost[j], std::abs(x[j] - x[i]));
            chmin(mx[s][j], std::abs(x[j] - x[i]));
          }
        }
    }
    int id = __builtin_popcount(s);
    long long value = 0;
    for (int i = 0; i < n; i++)
      value += cost[i] * p[i];
    chmin(ans[id], value);
  }

  for (int i = 0; i <= n; i++) {
    std::cout << ans[i] << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct fastio {
  fastio() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  }
} _io;
template <class T, class U> bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

constexpr long long INF = (1LL << 60);

int main() {
  int n;
  std::cin >> n;
  const int ALL = (1 << n) - 1;
  std::vector<long long> x(n, 0), y(n, 0), p(n, 0);
  for (int i = 0; i < n; i++) {
    std::cin >> x[i] >> y[i] >> p[i];
  }

  std::vector<long long> ans(n + 1, INF);

  for (int s = 0; s < (1 << n); s++) {
    int se = (ALL ^ s);
    for (int t = se; t > 0; t = (t - 1) & se) {
      std::vector<long long> cost(n, INF);
      for (int i = 0; i < n; i++) {
        cost[i] = std::min(std::abs(x[i]), std::abs(y[i]));
      }
      for (int i = 0; i < n; i++) {
        if (s >> i & 1) {
          for (int j = 0; j < n; j++) {
            chmin(cost[j], std::abs(x[j] - x[i]));
          }
        }
        if (t >> i & 1) {
          for (int j = 0; j < n; j++) {
            chmin(cost[j], std::abs(y[j] - y[i]));
          }
        }
      }
      int id = __builtin_popcount(s) + __builtin_popcount(t);
      long long value = 0;
      for (int i = 0; i < n; i++)
        value += cost[i] * p[i];
      chmin(ans[id], value);
    }
    std::vector<long long> cost(n, INF);
    for (int i = 0; i < n; i++) {
      cost[i] = std::min(std::abs(x[i]), std::abs(y[i]));
    }
    for (int i = 0; i < n; i++)
      if (s >> i & 1) {
        for (int j = 0; j < n; j++) {
          chmin(cost[j], std::abs(x[j] - x[i]));
        }
      }
    int id = __builtin_popcount(s);
    long long value = 0;
    for (int i = 0; i < n; i++)
      value += cost[i] * p[i];
    chmin(ans[id], value);
  }

  for (int i = 0; i <= n; i++) {
    std::cout << ans[i] << '\\n';
  }

  return 0;
}