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

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for (i = (s); i < (e); ++i)

int main(int argc, char *argv[]) {
  i64 N, i, j;
  cin >> N;
  vi X(N), Y(N), P(N);
  REP(0, N, i) { cin >> X[i] >> Y[i] >> P[i]; }

  i64 S = 1, s;
  REP(0, N, i) { S *= 3; }

  vi DD0(N);
  i64 ans0 = 0;
  REP(0, N, i) {
    DD0[i] = min(abs(X[i]), abs(Y[i])) * P[i];
    ans0 += DD0[i];
  }

  if (N == 1) {
    cout << ans0 << endl << 0 << endl;
    return 0;
  }

  i64 N0 = N / 2;
  i64 S0 = 1;
  REP(0, N0, i) { S0 *= 3; }
  i64 N1 = N - N0;
  i64 S1 = S / S0;
  vvi DD_S0(S0, DD0);
  vvi DD_S1(S1, DD0);
  vi K_S0(S0, 0);
  vi K_S1(S1, 0);

  REP(0, S0, s) {
    i64 ss = s;
    REP(0, N0, i) {
      i64 R = ss % 3;
      ss /= 3;
      if (R == 1) {
        REP(0, N, j) {
          DD_S0[s][j] = min(DD_S0[s][j], abs(X[j] - X[i]) * P[j]);
        }
        K_S0[s]++;
      } else if (R == 2) {
        REP(0, N, j) {
          DD_S0[s][j] = min(DD_S0[s][j], abs(Y[j] - Y[i]) * P[j]);
        }
        K_S0[s]++;
      }
    }
  }

  REP(0, S1, s) {
    i64 ss = s;
    REP(N0, N, i) {
      i64 R = ss % 3;
      ss /= 3;
      if (R == 1) {
        REP(0, N, j) {
          DD_S1[s][j] = min(DD_S1[s][j], abs(X[j] - X[i]) * P[j]);
        }
        K_S1[s]++;
      } else if (R == 2) {
        REP(0, N, j) {
          DD_S1[s][j] = min(DD_S1[s][j], abs(Y[j] - Y[i]) * P[j]);
        }
        K_S1[s]++;
      }
    }
  }

  vi ans(N + 1, ans0);
  REP(0, S, s) {
    i64 ss = s;
    vi DD = DD0;

    i64 ss0 = ss % S0;
    i64 ss1 = ss / S0;
    REP(0, N, j) { DD[j] = min(DD_S0[ss0][j], DD_S1[ss1][j]); }

    i64 K = K_S0[ss0] + K_S1[ss1];
    i64 candidate = accumulate(all(DD), 0LL);
    // cout << candidate << endl;

    ans[K] = min(ans[K], candidate);
  }

  REP(0, N + 1, i) { cout << ans[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for (i = (s); i < (e); ++i)

int main(int argc, char *argv[]) {
  i64 N, i, j;
  cin >> N;
  vi X(N), Y(N), P(N);
  REP(0, N, i) { cin >> X[i] >> Y[i] >> P[i]; }

  i64 S = 1, s;
  REP(0, N, i) { S *= 3; }

  vi DD0(N);
  i64 ans0 = 0;
  REP(0, N, i) {
    DD0[i] = min(abs(X[i]), abs(Y[i])) * P[i];
    ans0 += DD0[i];
  }

  vi ans(N + 1, ans0);
  REP(0, S, s) {
    i64 ss = s;
    vi DD = DD0;
    i64 K = 0;
    // cout << "R ";
    REP(0, N, i) {
      i64 R = ss % 3;
      ss /= 3;
      if (R == 1) {
        REP(0, N, j) { DD[j] = min(DD[j], abs(X[j] - X[i]) * P[j]); }
        K++;
      } else if (R == 2) {
        REP(0, N, j) { DD[j] = min(DD[j], abs(Y[j] - Y[i]) * P[j]); }
        K++;
      }
      // cout << R << " ";
    }
    // cout << endl;

    /*
    cout << "DD ";
    REP(0, N, i) {
      cout << DD[i] << " ";
    }
    cout << endl;
    */

    i64 candidate = accumulate(all(DD), 0LL);
    // cout << candidate << endl;

    ans[K] = min(ans[K], candidate);
  }

  REP(0, N + 1, i) { cout << ans[i] << endl; }

  return 0;
}