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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;

#define int long long
#define rep(i, n) for (long long i = 0; i < (n); i++)

const int INF = 2000000000;                 // 2e9
const long long INF2 = 1000000000000000000; // 1e18
const long double pi = acos(-1);

const int MOD = 1000000007; // 1e9 + 7
// const int MOD = 1000000009; //1e9 + 9
// const int MOD = 998244353;

// 繰り返し2乗法
// N^aの、Mで割った余りを求める。
ll my_pow(ll N, ll a, ll M) {
  ll tempo;
  if (a == 0) {
    return 1;
  } else {
    if (a % 2 == 0) {
      tempo = my_pow(N, a / 2, M);
      return (tempo * tempo) % M;
    } else {
      tempo = my_pow(N, a - 1, M);
      return (tempo * N) % M;
    }
  }
}

ll my_pow(ll N, ll a) {
  ll tempo;
  if (a == 0) {
    return 1;
  } else {
    if (a % 2 == 0) {
      tempo = my_pow(N, a / 2);
      return (tempo * tempo);
    } else {
      tempo = my_pow(N, a - 1);
      return (tempo * N);
    }
  }
}

signed main() {
  int N;
  cin >> N;

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

  vector<ll> res(N + 1, INF2);
  res[N] = 0;

  int M = my_pow(2, N);
  vector<vector<int>> memox(M, vector<int>(N, -1));
  vector<vector<int>> memoy(M, vector<int>(N, -1));

  // 前計算
  rep(j, M) {
    vector<int> Ys, Xs;
    Xs.push_back(0);
    Ys.push_back(0);
    rep(k, N) {
      if (j & (1LL << k))
        Xs.push_back(X[k]);
      if (j & (1LL << k))
        Ys.push_back(Y[k]);
    }

    rep(k, N) {
      int tempo1 = INF2;
      rep(l, (int)Xs.size()) { tempo1 = min(tempo1, abs(X[k] - Xs[l])); }
      memox[j][k] = tempo1;

      tempo1 = INF2;
      rep(l, (int)Ys.size()) { tempo1 = min(tempo1, abs(Y[k] - Ys[l])); }
      memoy[j][k] = tempo1;
    }
  }
  // 前計算終わり

  rep(j, M) {
    rep(j2, M) {
      if (j & j2)
        continue;
      int i = __builtin_popcountll(j) + __builtin_popcountll(j2);
      ll tempo0 = 0;
      rep(k, N) tempo0 += (ll)min(memox[j][k], memoy[j2][k]) * (ll)P[k];
      res[i] = min(res[i], tempo0);
    }
  }

  rep(i, N + 1) { cout << res[i] << endl; }
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef long double ld;

// #define int long long
#define rep(i, n) for (long long i = 0; i < (n); i++)

const int INF = 2000000000;                 // 2e9
const long long INF2 = 1000000000000000000; // 1e18
const long double pi = acos(-1);

const int MOD = 1000000007; // 1e9 + 7
// const int MOD = 1000000009; //1e9 + 9
// const int MOD = 998244353;

// 繰り返し2乗法
// N^aの、Mで割った余りを求める。
ll my_pow(ll N, ll a, ll M) {
  ll tempo;
  if (a == 0) {
    return 1;
  } else {
    if (a % 2 == 0) {
      tempo = my_pow(N, a / 2, M);
      return (tempo * tempo) % M;
    } else {
      tempo = my_pow(N, a - 1, M);
      return (tempo * N) % M;
    }
  }
}

ll my_pow(ll N, ll a) {
  ll tempo;
  if (a == 0) {
    return 1;
  } else {
    if (a % 2 == 0) {
      tempo = my_pow(N, a / 2);
      return (tempo * tempo);
    } else {
      tempo = my_pow(N, a - 1);
      return (tempo * N);
    }
  }
}

signed main() {
  int N;
  cin >> N;

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

  vector<ll> res(N + 1, -1);
  res[N] = 0;

  int M = my_pow(2, N);
  vector<vector<int>> memox(M, vector<int>(N, -1));
  vector<vector<int>> memoy(M, vector<int>(N, -1));

  rep(j, M) {
    vector<int> Ys, Xs;
    Xs.push_back(0);
    Ys.push_back(0);
    rep(k, N) {
      if (j & (1LL << k))
        Xs.push_back(X[k]);
      if (j & (1LL << k))
        Ys.push_back(Y[k]);
    }

    rep(k, N) {
      int tempo1 = INF;
      rep(l, (int)Xs.size()) { tempo1 = min(tempo1, abs(X[k] - Xs[l])); }
      memox[j][k] = tempo1;

      tempo1 = INF;
      rep(l, (int)Ys.size()) { tempo1 = min(tempo1, abs(Y[k] - Ys[l])); }
      memoy[j][k] = tempo1;
    }
  }

  for (int i = 0; i <= N; i++) {
    if (res[i] != -1) {
      cout << res[i] << endl;
      continue;
    }
    if (i != 0 && res[i - 1] == 0) {
      res[i] = 0;
      cout << res[i] << endl;
      continue;
    }

    if (i == 0) {
      ll tempo = 0;
      rep(j, N) { tempo += (ll)min(memox[0][j], memoy[0][j]) * (ll)P[j]; }
      res[i] = tempo;
      cout << res[i] << endl;
      continue;
    }

    res[i] = res[i - 1];

    rep(j, M) {
      int bit1 = __builtin_popcountll(j);
      if (bit1 > i)
        continue;
      rep(j2, M) {
        int bit2 = __builtin_popcountll(j2);
        if (bit2 > i)
          continue;
        if ((j & j2) || bit1 + bit2 != i)
          continue;

        ll tempo0 = 0;
        rep(k, N) tempo0 += (ll)min(memox[j][k], memoy[j2][k]) * (ll)P[k];
        res[i] = min(res[i], tempo0);
      }
    }

    cout << res[i] << endl;
    continue;
  }
}