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

#define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), c(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> c[i];
  }
  vector<int64> ret(n + 1, 1LL << 60);
  vector<int> can, v(n);
  for (int S = 0; S < (1 << n); ++S) {
    can.clear();
    for (int i = 0; i < n; ++i) {
      if (!(S & (1 << i)))
        can.push_back(i);
    }
    for (int sX = S;; sX = (sX - 1) & S) {
      for (auto &j : can)
        v[j] = min(abs(x[j]), abs(y[j]));
      int sY = S ^ sX;
      for (int i = 0; i < n; ++i) {
        if (sX & (1 << i)) {
          for (auto &j : can) {
            v[j] = min(v[j], abs(x[i] - x[j]));
          }
        } else if (sY & (1 << i)) {
          for (auto &j : can) {
            v[j] = min(v[j], abs(y[i] - y[j]));
          }
        }
      }
      int64 sum = 0;
      for (auto &j : can)
        sum += 1LL * c[j] * v[j];
      // trace(S, sum, can);
      int k = __builtin_popcount(S);
      ret[k] = min(ret[k], sum);
      if (sX == 0)
        break;
    }
  }
  for (int i = 0; i <= n; ++i) {
    cout << ret[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int N = 15;
int x[N], y[N], c[N];
vector<int> X, Y, pX, pY;

int n, len;
int64 best;
void search(int pos, int cnt) {
  // trace(pos, cnt, len, X.size(), Y.size());
  if (pos == X.size() + Y.size()) {
    // if (len == 0) trace(pX, pY, len);
    int64 sum = 0;
    for (int i = 0; i < n; ++i) {
      int cur = min(abs(x[i]), abs(y[i]));
      auto it = lower_bound(pX.begin(), pX.end(), x[i]);
      if (it != pX.end())
        cur = min(cur, *it - x[i]);
      if (it != pX.begin())
        cur = min(cur, x[i] - *prev(it));
      auto it2 = lower_bound(pY.begin(), pY.end(), y[i]);
      if (it2 != pY.end())
        cur = min(cur, *it2 - y[i]);
      if (it2 != pY.begin())
        cur = min(cur, y[i] - *prev(it2));
      sum += 1LL * cur * c[i];
    }
    best = min(best, sum);
    return;
  }
  if (cnt + 1 <= len) {
    if (pos < X.size()) {
      pX.push_back(X[pos]);
      search(pos + 1, cnt + 1);
      pX.pop_back();
    } else {
      pY.push_back(Y[pos - X.size()]);
      search(pos + 1, cnt + 1);
      pY.pop_back();
    }
  }
  if (X.size() + Y.size() - pos - 1 + cnt >= len) {
    search(pos + 1, cnt);
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> c[i];
    X.push_back(x[i]);
    Y.push_back(y[i]);
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  vector<int64> ret(n + 1);
  for (int k = 0; k <= n; ++k) {
    best = 1LL << 60;
    len = k;
    search(0, 0);
    ret[k] = best;
    if (ret[k] == 0)
      break;
  }
  for (int i = 0; i <= n; ++i)
    cout << ret[i] << endl;
  return 0;
}