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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll N;
  cin >> N;
  vint X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  ll m = 1;
  rep(i, N) m *= 3;

  vvint D[2];
  rep(i, 2) D[i].resize(N, vint(1ll << N, LLONG_MAX));
  // 前計算
  rep(i, 1ll << N) {
    set<ll> s[2];
    // s[0].insert(-100000); s[1].insert(-100000);
    // s[0].insert(100000); s[1].insert(100000);
    s[0].insert(0);
    s[1].insert(0);
    rep(j, N) {
      if ((i >> j) & 1) {
        s[0].insert(X[j]);
        s[1].insert(Y[j]);
      }
    }
    rep(j, N) {
      ll d = LLONG_MAX;
      for (auto e : s[0]) {
        chmin(d, abs(e - X[j]));
      }
      D[0][j][i] = d;
      d = LLONG_MAX;
      for (auto e : s[1]) {
        chmin(d, abs(e - Y[j]));
      }
      D[1][j][i] = d;
    }
  }

  vint ans(N + 1, LLONG_MAX);

  rep(i, m) {
    ll tate, yoko;
    tate = yoko = 0;
    ll cnt = 0;
    ll fac = 1;
    ll j = i;
    rep(k, N) {
      if (j % 3 == 1) {
        cnt++;
        tate += fac;
      } else if (j % 3 == 2) {
        cnt++;
        yoko += fac;
      }
      fac *= 2;
      j /= 3;
    }
    vint dist(N, LLONG_MAX);
    ll tmp = 0;
    rep(k, N) {
      chmin(dist[k], P[k] * min(D[0][k][tate], D[1][k][yoko]));
      tmp += dist[k];
    }
    chmin(ans[cnt], tmp);
  }

  for (auto e : ans)
    cout << e << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll N;
  cin >> N;
  vint X(N), Y(N), P(N);
  rep(i, N) cin >> X[i] >> Y[i] >> P[i];

  vint ans(N + 1, LLONG_MAX);

  ll m = 1;
  rep(i, N) m *= 3;

  rep(i, m) {
    ll j = i;
    ll cnt = 0;
    set<ll> x, y;
    x.insert(0);
    x.insert(-10000000);
    x.insert(1000000000);
    y.insert(0);
    y.insert(-10000000);
    y.insert(1000000000);
    ll ii = 0;
    while (j > 0) {
      if (j % 3 == 1) {
        // x;
        cnt++;
        x.insert(X[ii]);
      } else if (j % 3 == 2) {
        // y;
        cnt++;
        y.insert(Y[ii]);
      }
      j /= 3;
      ii++;
    }
    vint dist(N, LLONG_MAX);
    rep(k, N) {
      auto it = x.lower_bound(X[k]);
      chmin(dist[k], P[k] * abs(*it - X[k]));
      it--;
      chmin(dist[k], P[k] * abs(*it - X[k]));

      it = y.lower_bound(Y[k]);
      chmin(dist[k], P[k] * abs(*it - Y[k]));
      it--;
      chmin(dist[k], P[k] * abs(*it - Y[k]));
      // for(auto e:x){
      //     chmin(dist[k], P[k]*abs(e-X[k]));
      // }
      // for(auto e:y){
      //     chmin(dist[k], P[k]*abs(e-Y[k]));
      // }
    }
    ll s = 0;
    for (auto e : dist)
      s += e;
    chmin(ans[cnt], s);
    // cout<<cnt<<" ";
  }

  for (auto e : ans)
    cout << e << endl;

  return 0;
}