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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).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;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  REP(i, n) { cin >> x[i] >> y[i] >> p[i]; }
  vector<vector<ll>> hp(1 << n, vector<ll>(n)), wp(1 << n, vector<ll>(n));
  REP(t, 1 << n) {
    vector<int> h;
    h.push_back(0);
    REP(i, n) {
      if (t & (1 << i)) {
        h.push_back(x[i]);
      }
    }
    REP(i, n) {
      ll res = INF;
      REP(j, h.size()) { chmin(res, abs(h[j] - x[i])); }
      hp[t][i] = res;
    }
  }
  REP(t, 1 << n) {
    vector<int> w;
    w.push_back(0);
    REP(i, n) {
      if (t & (1 << i)) {
        w.push_back(y[i]);
      }
    }
    REP(i, n) {
      ll res = INF;
      REP(j, w.size()) { chmin(res, abs(w[j] - y[i])); }
      wp[t][i] = res;
    }
  }
  vector<ll> ans(n + 1, INF);
  ll T = 1;
  REP(i, n) { T *= 3; }
  REP(t, T) {
    int u = 0, v = 0;
    int cnt = 0;
    int num = t;
    REP(i, n) {
      if (num % 3 == 2) {
        cnt++;
        u += 1 << i;
      } else if (num % 3 == 1) {
        cnt++;
        v += 1 << i;
      }
      num /= 3;
    }
    ll res = 0;
    REP(i, n) { res += min(hp[u][i], wp[v][i]) * p[i]; }
    chmin(ans[cnt], res);
  }
  REP(i, n + 1) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).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;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> x(n), y(n), p(n);
  REP(i, n) { cin >> x[i] >> y[i] >> p[i]; }
  vector<ll> ans(n + 1, INF);
  ll T = 1;
  REP(i, n) { T *= 3; }
  REP(t, T) {
    int cnt = 0;
    vector<int> h, w;
    h.push_back(0);
    w.push_back(0);
    ll num = t;
    REP(i, n) {
      if (num % 3 == 2) {
        cnt++;
        h.push_back(x[i]);
      } else if (num % 3 == 1) {
        cnt++;
        w.push_back(y[i]);
      }
      num /= 3;
    }
    h.push_back(-inf), h.push_back(inf);
    w.push_back(-inf), w.push_back(inf);
    sort(ALL(h));
    sort(ALL(w));
    ll res = 0;
    REP(i, n) {
      int u = lower_bound(ALL(h), x[i]) - h.begin();
      int v = lower_bound(ALL(w), y[i]) - w.begin();
      ll dx = min(abs(x[i] - h[u]), abs(x[i] - h[u - 1]));
      ll dy = min(abs(y[i] - w[v]), abs(y[i] - w[v - 1]));
      res += min(dx, dy) * p[i];
    }
    chmin(ans[cnt], res);
  }
  REP(i, n + 1) { cout << ans[i] << endl; }
}