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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr ll INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
const int MAXD = 200010;

vector<vector<ll>> precompute(vector<ll> &A, int N) {
  vector<vector<ll>> ans(1 << N, vector<ll>(N, HINF));
  rep(bit, 1 << N) {
    vector<ll> chose(1, 0);
    rep(i, N) {
      if ((bit >> i) & 1) {
        chose.push_back(A[i]);
      }
    }
    rep(i, N) {
      ll diff = HINF;
      rep(j, chose.size()) { diff = min(diff, abs(A[i] - chose[j])); }
      ans[bit][i] = diff;
    }
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  vector<vector<ll>> chosex = precompute(X, N), chosey = precompute(Y, N);

  vector<ll> ans(N + 1, HINF);
  rep(bit, 1 << N) { // どの街を選ぶか
    int cnt = 0;
    rep(i, N) {
      if ((bit >> i) & 1)
        ++cnt;
    }
    irep(i, bit + 1) { // 選んだ街の中でどの街に縦線を引くか
      i &= bit;
      ll ret = 0;
      rep(j, N) {
        if (!(bit >> j & 1)) {
          ret += min(chosex[i][j], chosey[bit - i][j]) * P[j];
        }
      }
      ans[cnt] = min(ans[cnt], ret);
    }
  }
  rep(i, N + 1) cout << ans[i] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr ll INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
const int MAXD = 200010;

vector<vector<ll>> precompute(vector<ll> &A, int N) {
  vector<vector<ll>> ans(1 << N, vector<ll>(N, HINF));
  rep(bit, 1 << N) {
    vector<ll> chose(1, 0);
    rep(i, N) {
      if ((bit >> i) & 1) {
        chose.push_back(A[i]);
      }
    }
    rep(i, N) {
      ll diff = HINF;
      rep(j, chose.size()) { diff = min(diff, abs(A[i] - chose[j])); }
      ans[bit][i] = diff;
    }
  }
  return ans;
}

int main() {
  int N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  vector<vector<ll>> chosex = precompute(X, N), chosey = precompute(Y, N);

  vector<ll> ans(N + 1, HINF);
  vector<ll> power3(N + 1);
  power3[0] = 1;
  rep2(i, 1, N + 1) power3[i] = power3[i - 1] * 3LL;
  rep(bit, power3.back()) {
    ll x = 0, y = 0;
    int cnt = N;
    rep(i, N) {
      ll m = (bit % power3[i + 1]) / power3[i];
      if (m == 2)
        x += 1 << i;
      else if (m == 1)
        y += 1 << i;
      else
        --cnt;
    }
    ll ret = 0;
    rep(i, N) ret += min(chosex[x][i], chosey[y][i]) * P[i];
    ans[cnt] = min(ans[cnt], ret);
  }
  rep(i, N + 1) cout << ans[i] << '\\n';
  return 0;
}