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[35];
ll distx[40000][30], disty[40000][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] = min(cdist[i], abs(X[i] - X[cur]));
    chmin(dist[i], abs(Y[i] - Y[cur]));
  }
  dfs(cur + 1, cnt + 2);

  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; }

  for (ll bit = 0; bit < (1 << N); ++bit) {
    rep(i, 0, N) {
      distx[bit][i] = min(abs(X[i]), abs(Y[i]));
      disty[bit][i] = min(abs(X[i]), abs(Y[i]));
    }

    rep(i, 0, N) {
      if (bit & (1 << i)) {
        rep(j, 0, N) {
          chmin(distx[bit][j], abs(X[j] - X[i]));
          chmin(disty[bit][j], abs(Y[j] - Y[i]));
        }
      }
    }
  }

  for (ll bit = 0; bit < pow(3, N) + eps; ++bit) {
    ll b = bit;
    ll xx = 0;
    ll yy = 0;
    ll digit = 1;
    ll cnt = 0;
    rep(i, 0, N) {
      if (b % 3 == 1) {
        xx += digit;
        ++cnt;
      } else if (b % 3 == 2) {
        yy += digit;
        ++cnt;
      }
      digit *= 2;
      b /= 3;
    }

    ll score = 0;
    rep(i, 0, N) { score += min(distx[xx][i], disty[yy][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";
  }
}


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[35];

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] = min(cdist[i], abs(X[i] - X[cur]));
    chmin(dist[i], abs(Y[i] - Y[cur]));
  }
  dfs(cur + 1, cnt + 2);

  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";
  }
}