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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

const ll INF = 1LL << 50;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<ll> X(N), Y(N), P(N);
  rep(i, 0, N) { cin >> X[i] >> Y[i] >> P[i]; }
  vector<ll> x, y;
  rep(i, 0, N) {
    x.push_back(X[i]);
    y.push_back(Y[i]);
  }
  sort(all(x));
  sort(all(y));
  x.erase(unique(all(x)), x.end());
  y.erase(unique(all(y)), y.end());
  int xn = (int)x.size();
  int yn = (int)y.size();
  vector<int> yoko_idx(N);
  vector<int> tate_idx(N);
  rep(i, 0, N) {
    rep(j, 0, xn) {
      if (X[i] == x[j]) {
        yoko_idx[i] = j;
        break;
      }
    }
    rep(j, 0, yn) {
      if (Y[i] == y[j]) {
        tate_idx[i] = j;
        break;
      }
    }
  }
  vector<vector<ll>> yoko_dist(1 << xn, vector<ll>(N, INF));
  vector<vector<ll>> tate_dist(1 << yn, vector<ll>(N, INF));
  rep(mask, 0, 1 << xn) {
    rep(i, 0, xn) {
      if (!((mask >> i) % 2)) {
        continue;
      }
      rep(j, 0, N) { chmin(yoko_dist[mask][j], llabs(X[j] - x[i])); }
    }
  }
  rep(mask, 0, 1 << yn) {
    rep(i, 0, yn) {
      if (!((mask >> i) % 2)) {
        continue;
      }
      rep(j, 0, N) { chmin(tate_dist[mask][j], llabs(Y[j] - y[i])); }
    }
  }
  rep(i, 0, N) {
    rep(mask, 0, 1 << xn) { chmin(yoko_dist[mask][i], llabs(X[i])); }
    rep(mask, 0, 1 << yn) { chmin(tate_dist[mask][i], llabs(Y[i])); }
  }
  int max_mask = 1;
  rep(i, 0, N) { max_mask *= 3; }
  vector<ll> ans(N + 1, INF);
  rep(i, 0, max_mask) {
    int mask = i;
    int yoko_mask = 0;
    int tate_mask = 0;
    rep(j, 0, N) {
      if (mask % 3 == 1) {
        yoko_mask |= 1 << yoko_idx[j];
      } else if (mask % 3 == 2) {
        tate_mask |= 1 << tate_idx[j];
      }
      mask /= 3;
    }
    ll now_ans = 0;
    rep(j, 0, N) {
      now_ans += P[j] * min(yoko_dist[yoko_mask][j], tate_dist[tate_mask][j]);
    }
    mask = i;
    int cnt = 0;
    rep(j, 0, N) {
      if (mask % 3) {
        ++cnt;
      }
      mask /= 3;
    }
    chmin(ans[cnt], now_ans);
  }
  rep(i, 0, N + 1) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

ll INF = 1LL << 50;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> X(N), Y(N);
  vector<ll> P(N);
  rep(i, 0, N) { cin >> X[i] >> Y[i] >> P[i]; }
  int max_mask = 1;
  rep(i, 0, N) { max_mask *= 3; }
  vector<ll> ans(N + 1, INF);
  rep(i, 0, max_mask) {
    vector<int> sx, sy;
    sx.push_back(0);
    sy.push_back(0);
    int mask = i;
    rep(j, 0, N) {
      // 0:使わない,1:縦,2:横
      if (mask % 3 == 1) {
        sx.push_back(X[j]);
      } else if (mask % 3 == 2) {
        sy.push_back(Y[j]);
      }
      mask /= 3;
    }
    sort(all(sx));
    sx.erase(unique(all(sx)), sx.end());
    sort(all(sy));
    sy.erase(unique(all(sy)), sy.end());
    ll now_ans = 0;
    rep(j, 0, N) {
      ll min_dist = INF;
      auto x_itr = lower_bound(all(sx), X[j]);
      if (x_itr != sx.end()) {
        chmin(min_dist, ll(*x_itr - X[j]));
      }
      if (x_itr != sx.begin()) {
        chmin(min_dist, ll(X[j] - *(x_itr - 1)));
      }
      auto y_itr = lower_bound(all(sy), Y[j]);
      if (y_itr != sy.end()) {
        chmin(min_dist, ll(*y_itr - Y[j]));
      }
      if (y_itr != sy.begin()) {
        chmin(min_dist, ll(Y[j] - *(y_itr - 1)));
      }
      now_ans += min_dist * P[j];
    }
    mask = i;
    int cnt = 0;
    rep(j, 0, N) {
      if (mask % 3) {
        ++cnt;
      }
      mask /= 3;
    }
    chmin(ans[cnt], now_ans);
  }
  rep(i, 0, N + 1) { cout << ans[i] << endl; }
  return 0;
}