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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)

#define ri(x)                                                                  \\
  i64 x;                                                                       \\
  cin >> x
#define rii(x, y)                                                              \\
  i64 x, y;                                                                    \\
  cin >> x >> y
#define ri3(x, y, z)                                                           \\
  i64 x, y, z;                                                                 \\
  cin >> x >> y >> z
#define ri4(x, y, z, u)                                                        \\
  i64 x, y, z, u;                                                              \\
  cin >> x >> y >> z >> u
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  cin >> x
#define rvvi(x, n, m)                                                          \\
  vvi x(n, vi(m));                                                             \\
  cin >> x
#define rx(x, t)                                                               \\
  t x;                                                                         \\
  cin >> x

#define ws(x) cout << x << ' '
#define wl(x) cout << x << '\\n'
#define wln cout << '\\n'

#define dbg(x) cerr << '[' << #x << "]: " << x << '\\n'

#define fa(x, v) for (auto &x : v)
#define fi(i, n) for (i32 i = 0; i < n; i++)
#define fp(i, n) for (i32 i = 1; i < n; i++)
#define fr(i, n) for (i32 i = n - 1; i >= 0; i--)
#define fu(x)                                                                  \\
  ri(_x);                                                                      \\
  fi(x, _x)

#define l first
#define r second
#define all(x) x.begin(), x.end()
#define bit(x) ((u64)1 << (u32)(x))

using namespace std;

typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t i32;
typedef int64_t i64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

template <typename T> inline istream &operator>>(istream &in, vector<T> &v) {
  fa(x, v) in >> x;
  return in;
}

template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  fa(x, v) ws(x);
  wln;
  return out;
}

int main() {
  fio;

  ri(n);
  rvvi(a, n, 3);
  vi res(n + 1, numeric_limits<i64>::max());
  u32 state = 0;

  vvi xmin(bit(n), vi(n));
  vvi ymin(bit(n), vi(n));

  fi(t, bit(n)) {
    fi(i, n) {
      xmin[t][i] = abs(a[i][0]);
      ymin[t][i] = abs(a[i][1]);
      fi(j, n) {
        if (t & bit(j))
          xmin[t][i] = min(xmin[t][i], abs(a[i][0] - a[j][0]));
        if (t & bit(j))
          ymin[t][i] = min(ymin[t][i], abs(a[i][1] - a[j][1]));
      }
      xmin[t][i] *= a[i][2];
      ymin[t][i] *= a[i][2];
    }
  }

  fi(i, bit(n)) {
    u32 d = __builtin_popcountll(i);
    fr(j, i + 1) {
      j &= i;
      i64 cost = 0;
      fi(k, n) { cost += min(xmin[j][k], ymin[i - j][k]); }
      res[d] = min(res[d], cost);
    }
  }

  fa(x, res) wl(x);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)

#define ri(x)                                                                  \\
  i64 x;                                                                       \\
  cin >> x
#define rii(x, y)                                                              \\
  i64 x, y;                                                                    \\
  cin >> x >> y
#define ri3(x, y, z)                                                           \\
  i64 x, y, z;                                                                 \\
  cin >> x >> y >> z
#define ri4(x, y, z, u)                                                        \\
  i64 x, y, z, u;                                                              \\
  cin >> x >> y >> z >> u
#define rvi(x, n)                                                              \\
  vi x(n);                                                                     \\
  cin >> x
#define rvvi(x, n, m)                                                          \\
  vvi x(n, vi(m));                                                             \\
  cin >> x
#define rx(x, t)                                                               \\
  t x;                                                                         \\
  cin >> x

#define ws(x) cout << x << ' '
#define wl(x) cout << x << '\\n'
#define wln cout << '\\n'

#define dbg(x) cerr << '[' << #x << "]: " << x << '\\n'

#define fa(x, v) for (auto &x : v)
#define fi(i, n) for (i32 i = 0; i < n; i++)
#define fp(i, n) for (i32 i = 1; i < n; i++)
#define fr(i, n) for (i32 i = n - 1; i >= 0; i--)
#define fu(x)                                                                  \\
  ri(_x);                                                                      \\
  fi(x, _x)

#define l first
#define r second
#define all(x) x.begin(), x.end()
#define bit(x) ((u64)1 << (u32)(x))

using namespace std;

typedef uint32_t u32;
typedef uint64_t u64;
typedef int32_t i32;
typedef int64_t i64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

template <typename T> inline istream &operator>>(istream &in, vector<T> &v) {
  fa(x, v) in >> x;
  return in;
}

template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  fa(x, v) ws(x);
  wln;
  return out;
}

inline i64 cost(const vvi &a, bitset<30> state) {
  i64 cost = 0;
  fi(i, a.size()) {
    i64 d = min(abs(a[i][0]), abs(a[i][1]));
    fi(j, a.size()) {
      if (state.test(j)) {
        d = min(d, abs(a[i][0] - a[j][0]));
      } else if (state.test(15 + j)) {
        d = min(d, abs(a[i][1] - a[j][1]));
      }
    }
    cost += a[i][2] * d;
  }
  return cost;
}

template <int i>
inline void dfs(const vvi &a, vi &res, i32 d, bitset<30> &state) {
  if (i == a.size()) {
    auto k = state.count();
    res[k] = min(res[k], cost(a, state));
  } else {
    dfs<i + 1>(a, res, d, state);
    state.set(i);
    dfs<i + 1>(a, res, d + 1, state);
    state.set(i, false);
    state.set(i + 15);
    dfs<i + 1>(a, res, d + 1, state);
    state.set(i + 15, false);
  }
}

template <>
inline void dfs<16>(const vvi &a, vi &res, i32 d, bitset<30> &state) {}

int main() {
  fio;

  ri(n);
  rvvi(a, n, 3);
  vi res(n, numeric_limits<i64>::max());
  bitset<30> state;

  dfs<0>(a, res, 0, state);
  fa(x, res) wl(x);
  wl(0);

  return 0;
}