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 rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  const ll inf = 5e18;
  vector<ll> ans(n + 1, inf);

  vector<vector<ll>> xcost(1 << n, vector<ll>(n, inf)),
      ycost(1 << n, vector<ll>(n, inf));
  rep(bit, 1 << n) {
    vector<int> xs, ys;
    xs.push_back(0);
    ys.push_back(0);
    rep(i, n) if (bit >> i & 1) xs.push_back(x[i]), ys.push_back(y[i]);
    rep(i, n) {
      rep(j, xs.size()) chmin(xcost[bit][i], 1LL * p[i] * abs(x[i] - xs[j]));
      rep(j, ys.size()) chmin(ycost[bit][i], 1LL * p[i] * abs(y[i] - ys[j]));
    }
  }

  auto cost = [&](vector<int> &how) {
    int bitx = 0, bity = 0;
    rep(i, n) {
      if (how[i] == 1)
        bitx |= 1 << i;
      if (how[i] == 2)
        bity |= 1 << i;
    }
    ll res = 0;
    rep(i, n) if (!how[i]) { res += min(xcost[bitx][i], ycost[bity][i]); }
    return res;
  };

  for (int bit = 0; bit < pow(3, n); ++bit) {
    int tmp = bit;
    vector<int> how(n);
    rep(i, n) {
      how[i] = tmp % 3;
      tmp /= 3;
    }
    int cnt = 0;
    rep(i, n) if (how[i]) cnt++;

    chmin(ans[cnt], cost(how));
  }
  rep(i, n + 1) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  const ll inf = 5e18;
  vector<ll> ans(n + 1, inf);

  auto cost = [&](vector<int> &how) {
    ll res = 0;
    vector<int> xs, ys;
    xs.push_back(0);
    ys.push_back(0);
    rep(i, n) {
      if (how[i] == 1)
        xs.push_back(x[i]);
      if (how[i] == 2)
        ys.push_back(y[i]);
    }
    rep(i, n) if (how[i] == 0) {
      ll now = inf;
      rep(j, xs.size()) chmin(now, 1LL * p[i] * abs(x[i] - xs[j]));
      rep(j, ys.size()) chmin(now, 1LL * p[i] * abs(y[i] - ys[j]));
      res += now;
    }
    return res;
  };

  for (int bit = 0; bit < pow(3, n); ++bit) {
    int tmp = bit;
    vector<int> how(n);
    rep(i, n) {
      how[i] = tmp % 3;
      tmp /= 3;
    }
    int cnt = 0;
    rep(i, n) if (how[i]) cnt++;

    chmin(ans[cnt], cost(how));
  }
  rep(i, n + 1) cout << ans[i] << endl;
}