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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  REP(i, n) { cin >> x[i] >> y[i] >> p[i]; }

  vector<vector<ll>> x2d(1LL << n), y2d(1LL << n);
  REP(i, (1LL << n)) {
    vector<ll> xs, ys;
    REP(j, n) {
      if ((i >> j) & 1) {
        xs.push_back(x.at(j));
        ys.push_back(y.at(j));
      }
    }
    sort(ALL(xs));
    sort(ALL(ys));

    x2d[i].resize(n, numeric_limits<ll>::max());
    y2d[i].resize(n, numeric_limits<ll>::max());

    REP(j, n) {
      {
        ll dx = abs(x.at(j));
        ll idx = lower_bound(ALL(xs), x.at(j)) - xs.begin();
        if (idx < xs.size()) {
          dx = min(dx, xs.at(idx) - x.at(j));
        }
        if (idx > 0) {
          dx = min(dx, x.at(j) - xs.at(idx - 1));
        }
        x2d.at(i).at(j) = dx;
      }

      {
        ll dy = abs(y.at(j));
        ll idx = lower_bound(ALL(ys), y.at(j)) - ys.begin();
        if (idx < ys.size()) {
          dy = min(dy, ys.at(idx) - y.at(j));
        }
        if (idx > 0) {
          dy = min(dy, y.at(j) - ys.at(idx - 1));
        }
        y2d.at(i).at(j) = dy;
      }
    }
  }

  ll ub = 1;
  REP(i, n) { ub *= 3; }

  vector<ll> ans(n + 1, numeric_limits<ll>::max());

  REP(i, ub) {
    ll ix = 0, iy = 0, k = 0;
    {
      ll m = i;
      REP(j, n) {
        const ll l = m % 3;
        if (l == 1) {
          ix += (1LL << j);
          k++;
        } else if (l == 2) {
          iy += (1LL << j);
          k++;
        }
        m /= 3;
      }
    }

    ll s = 0;
    const auto &dx = x2d.at(ix), dy = y2d.at(iy);
    REP(j, n) {
      ll d = min(dx.at(j), dy.at(j));
      s += p.at(j) * d;
    }

    ans.at(k) = min(ans.at(k), s);
  }

  for (auto ai : ans) {
    cout << ai << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main() {
  ll n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  REP(i, n) { cin >> x[i] >> y[i] >> p[i]; }

  map<ll, vector<ll>> x2d, y2d;
  REP(i, (1LL << n)) {
    vector<ll> xs, ys;
    REP(j, n) {
      if ((i >> j) & 1) {
        xs.push_back(x.at(j));
        ys.push_back(y.at(j));
      }
    }
    sort(ALL(xs));
    sort(ALL(ys));

    x2d[i].resize(n, numeric_limits<ll>::max());
    y2d[i].resize(n, numeric_limits<ll>::max());

    REP(j, n) {
      {
        ll dx = abs(x.at(j));
        ll idx = lower_bound(ALL(xs), x.at(j)) - xs.begin();
        if (idx < xs.size()) {
          dx = min(dx, xs.at(idx) - x.at(j));
        }
        if (idx > 0) {
          dx = min(dx, x.at(j) - xs.at(idx - 1));
        }
        x2d.at(i).at(j) = dx;
      }

      {
        ll dy = abs(y.at(j));
        ll idx = lower_bound(ALL(ys), y.at(j)) - ys.begin();
        if (idx < ys.size()) {
          dy = min(dy, ys.at(idx) - y.at(j));
        }
        if (idx > 0) {
          dy = min(dy, y.at(j) - ys.at(idx - 1));
        }
        y2d.at(i).at(j) = dy;
      }
    }
  }

  ll ub = 1;
  REP(i, n) { ub *= 3; }

  vector<ll> ans(n + 1, numeric_limits<ll>::max());

  REP(i, ub) {
    ll ix = 0, iy = 0, k = 0;
    {
      ll m = i;
      REP(j, n) {
        const ll l = m % 3;
        if (l == 1) {
          ix += (1LL << j);
          k++;
        } else if (l == 2) {
          iy += (1LL << j);
          k++;
        }
        m /= 3;
      }
    }

    ll s = 0;
    const auto &dx = x2d.at(ix), dy = y2d.at(iy);
    REP(j, n) {
      ll d = min(dx.at(j), dy.at(j));
      s += p.at(j) * d;
    }

    ans.at(k) = min(ans.at(k), s);
  }

  for (auto ai : ans) {
    cout << ai << endl;
  }
  return 0;
}