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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

unsigned next_combination(unsigned sub) {
  unsigned x = sub & -sub, y = sub + x;
  return (((sub & ~y) / x) >> 1) | y;
}

struct city {
  int x;
  int y;
  ll p;
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<city> c(n);
  for (auto &e : c)
    cin >> e.x >> e.y >> e.p;
  ll ans_prev = (ll)1e18;
  rep(k, n + 1) {
    eprintf("k:%d\\n", k);
    ll ans = ans_prev;
    bool ok = false;
    unsigned bit = (1 << k) - 1;
    if (k > 0) {
      for (; bit < (1 << n); bit = next_combination(bit)) {
        rep(dir, (1 << k)) {
          vector<P> l;
          int tmp = 0;
          ll ans_tmp = 0;
          rep(i, n) {
            if (bit & (1 << i))
              l.emplace_back(c[i].x, c[i].y);
          }
          rep(i, n) {
            ll x = c[i].x, y = c[i].y;
            if (bit & (1 << i))
              continue;
            ll dist = min(abs(x), abs(y));
            rep(j, k) {
              chmin(dist, ((dir & (1 << j)) ? abs(x - l[j].first)
                                            : abs(y - l[j].second)));
            }
            ans_tmp += c[i].p * dist;
            if (ans_tmp >= ans)
              break;
          }
          chmin(ans, ans_tmp);
        }
      }
    } else {
      ans = 0;
      rep(i, n) { ans += c[i].p * min(abs(c[i].x), abs(c[i].y)); }
    }
    cout << ans << "\\n";
    ans_prev = ans;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

unsigned next_combination(unsigned sub) {
  unsigned x = sub & -sub, y = sub + x;
  return (((sub & ~y) / x) >> 1) | y;
}

struct city {
  int x;
  int y;
  ll p;
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<city> c(n);
  for (auto &e : c)
    cin >> e.x >> e.y >> e.p;
  ll ans_prev = (ll)1e18;
  rep(k, n + 1) {
    eprintf("k:%d\\n", k);
    ll ans = ans_prev;
    bool ok = false;
    unsigned bit = (1 << k) - 1;
    if (k > 0) {
      for (; bit < (1 << n); bit = next_combination(bit)) {
        rep(dir, (1 << k)) {
          // eprintf("bit:%d dir:%d\\n", bit, dir);
          vector<int> l;
          int tmp = 0;
          ll ans_tmp = 0;
          rep(i, n) {
            if (bit & (1 << i)) {
              // if (dir & (1 << tmp)) v.push_back(c[i].x);
              // else h.push_back(c[i].y);
              l.push_back(i);
              // tmp++;
            }
          }
          rep(i, n) {
            if (bit & (1 << i))
              continue;
            ll dist = min(abs(c[i].x), abs(c[i].y));
            rep(j, k) {
              chmin(dist, ((dir & (1 << j)) ? abs((ll)c[i].x - c[l[j]].x)
                                            : abs((ll)c[i].y - c[l[j]].y)));
            }
            // for (int x : v) chmin(dist, abs((ll)c[i].x - x));
            // for (int y : h) chmin(dist, abs((ll)c[i].y - y));
            ans_tmp += c[i].p * dist;
            if (ans_tmp >= ans)
              break;
          }
          chmin(ans, ans_tmp);
        }
      }
      cout << ans << "\\n";
    } else {
      ans = 0;
      rep(i, n) { ans += c[i].p * min(abs(c[i].x), abs(c[i].y)); }
      cout << ans << "\\n";
    }
    ans_prev = ans;
  }
  return 0;
}