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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

ll N;
ll X[30], Y[30], P[30], dist[30];
long double eps = 1e-9;
ll ans[30];

void init_dist() {
  rep(i, 0, N) {
    ll d = INF;
    chmin(d, abs(X[i]));
    chmin(d, abs(Y[i]));
    dist[i] = d;
  }
}

void dfs(int cur, int cnt) {
  if (cur >= N) {
    ll score = 0;
    rep(i, 0, N) { score += dist[i] * P[i]; }
    chmin(ans[cnt], score);
    return;
  }

  dfs(cur + 1, cnt);

  ll cdist[30];
  rep(i, 0, N) {
    cdist[i] = dist[i];
    chmin(dist[i], abs(Y[i] - Y[cur]));
  }
  dfs(cur + 1, cnt + 1);

  rep(i, 0, N) { dist[i] = min(cdist[i], abs(X[i] - X[cur])); }
  dfs(cur + 1, cnt + 1);

  rep(i, 0, N) { dist[i] = cdist[i]; }
}

int main() {
  //
  cin >> N;
  rep(i, 0, N) { cin >> X[i] >> Y[i] >> P[i]; }

  init_dist();
  repe(i, 0, N) { ans[i] = INF; }

  dfs(0, 0);

  cout << ans[0] << "\\n";
  repe(i, 1, N) {
    chmin(ans[i], ans[i - 1]);
    cout << ans[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

ll N;
ll X[30], Y[30], P[30], dist[30];
long double eps = 1e-9;
ll ans[30];

void init_dist() {
  rep(i, 0, N) {
    ll d = INF;
    chmin(d, abs(X[i]));
    chmin(d, abs(Y[i]));
    dist[i] = d;
  }
}

int main() {
  //
  cin >> N;
  rep(i, 0, N) { cin >> X[i] >> Y[i] >> P[i]; }

  init_dist();
  repe(i, 0, N) { ans[i] = INF; }

  for (ll bit = 0; bit < pow(3, N) + eps; ++bit) {
    init_dist();
    ll b = bit;
    ll cnt = 0;
    rep(i, 0, N) {
      if (b % 3 == 1) {
        ll cy = Y[i];
        rep(j, 0, N) { chmin(dist[j], abs(Y[j] - cy)); }
        ++cnt;
      } else if (b % 3 == 2) {
        ll cx = X[i];
        rep(j, 0, N) { chmin(dist[j], abs(X[j] - cx)); }
        ++cnt;
      }
      b /= 3;
      if (b == 0)
        break;
    }

    ll score = 0;
    rep(i, 0, N) { score += dist[i] * P[i]; }
    chmin(ans[cnt], score);
  }

  cout << ans[0] << "\\n";
  repe(i, 1, N) {
    chmin(ans[i], ans[i - 1]);
    cout << ans[i] << "\\n";
  }
}