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

#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \\
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << (first) << ", " << (last) << ")" << endl;                            \\
  for (int _i = (first); _i < (last); ++_i) {                                  \\
    cout << #a << "[" << _i << "] = " << (a)[_i] << endl;                      \\
  }
#define PRINTI(a, i)                                                           \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << #i << "] = " << #a << "[" << (i) << "] = " << (a)[i] << endl;
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#define PRINTI(a, i)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MOD 1000000007
// #define MOD 998244353

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    if (i) {
      if (i % 5 == 0) {
        os << ",\\n ";
      } else {
        os << ", ";
      }
    }
    os << v[i];
  }
  os << "]";
  return os;
}

template <class T> using vec2 = vector<vector<T>>;

template <class T> using vec3 = vector<vector<vector<T>>>;

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int next_combination(int x) {
  if (x == 0) {
    return INT_MAX;
  }
  int y = x & -x;
  int z = x + y;
  return ((x & ~z) / y >> 1) | z;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> X(N), Y(N), P(N);
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];

  ll ans0 = 0;
  REP(i, N) { ans0 += (ll)P[i] * min(abs(X[i]), abs(Y[i])); }
  cout << ans0 << endl;

  vector<vector<int>> d_X(N, vector<int>(1 << N, INT_MAX));
  vector<vector<int>> d_Y(N, vector<int>(1 << N, INT_MAX));

  REP(i, N) {
    d_X[i][0] = abs(X[i]);
    d_Y[i][0] = abs(Y[i]);
  }
  FOR(bit, 1, 1 << N) {
    int mask = bit & -bit;
    int bit_prev = bit ^ mask;
    int j = __builtin_ctz(mask);
    REP(i, N) {
      PRINT(i);
      PRINT(j);
      PRINT(X[i] - X[j]);
      d_X[i][bit] = min(d_X[i][bit_prev], abs(X[i] - X[j]));
      d_Y[i][bit] = min(d_Y[i][bit_prev], abs(Y[i] - Y[j]));
    }
  }

  FOR(K, 1, N + 1) {
    ll result = ans0;

    for (int bit = (1 << K) - 1; bit < 1 << N; bit = next_combination(bit)) {
      vector<int> v;
      REP(i, N) {
        if (bit & (1 << i)) {
          v.pb(i);
        }
      }
      assert(v.size() == K);

      REP(BB, 1 << K) {
        int biti = 0;
        int bitj = 0;
        REP(i, K) {
          if (BB & (1 << i)) {
            biti ^= (1 << v[i]);
          } else {
            bitj ^= (1 << v[i]);
          }
        }
        ll cost = 0;
        REP(n, N) {
          ll d = min(d_X[n][biti], d_Y[n][bitj]);
          cost += (ll)d * P[n];
        }
        amin(result, cost);
      }
    }

    cout << result << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \\
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << (first) << ", " << (last) << ")" << endl;                            \\
  for (int _i = (first); _i < (last); ++_i) {                                  \\
    cout << #a << "[" << _i << "] = " << (a)[_i] << endl;                      \\
  }
#define PRINTI(a, i)                                                           \\
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \\
       << #i << "] = " << #a << "[" << (i) << "] = " << (a)[i] << endl;
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#define PRINTI(a, i)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MOD 1000000007
// #define MOD 998244353

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    if (i) {
      if (i % 5 == 0) {
        os << ",\\n ";
      } else {
        os << ", ";
      }
    }
    os << v[i];
  }
  os << "]";
  return os;
}

template <class T> using vec2 = vector<vector<T>>;

template <class T> using vec3 = vector<vector<vector<T>>>;

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int next_combination(int x) {
  if (x == 0) {
    return INT_MAX;
  }
  int y = x & -x;
  int z = x + y;
  return ((x & ~z) / y >> 1) | z;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> X(N), Y(N), P(N);
  REP(i, N) cin >> X[i] >> Y[i] >> P[i];

  ll ans0 = 0;
  REP(i, N) { ans0 += (ll)P[i] * min(abs(X[i]), abs(Y[i])); }
  cout << ans0 << endl;

  vector<vector<int>> d_X(N, vector<int>(1 << N, INT_MAX));
  vector<vector<int>> d_Y(N, vector<int>(1 << N, INT_MAX));

  REP(i, N) {
    d_X[i][0] = abs(X[i]);
    d_Y[i][0] = abs(Y[i]);
  }
  FOR(bit, 1, 1 << N) {
    int mask = bit & -bit;
    int bit_prev = bit ^ mask;
    int j = __builtin_ctz(mask);
    REP(i, N) {
      PRINT(i);
      PRINT(j);
      PRINT(X[i] - X[j]);
      d_X[i][bit] = min(d_X[i][bit_prev], abs(X[i] - X[j]));
      d_Y[i][bit] = min(d_Y[i][bit_prev], abs(Y[i] - Y[j]));
    }
  }

  FOR(K, 1, N + 1) {
    PRINT(K);
    // 縦i本, 横(K-i)本

    ll result = ans0;

    REP(i, K + 1) {
      int j = K - i;
      for (int bi = (1 << i) - 1; bi < 1 << N; bi = next_combination(bi)) {
        for (int bj = (1 << j) - 1; bj < 1 << N; bj = next_combination(bj)) {
          PRINT(bitset<3>(bi));
          PRINT(bitset<3>(bj));
          ll cost = 0;
          REP(n, N) {
            PRINT(d_X[n][bi]);
            ll d = min(d_X[n][bi], d_Y[n][bj]);
            cost += (ll)d * P[n];
          }

          amin(result, cost);
        }
      }
    }

    cout << result << endl;
  }

  return 0;
}