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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int N = 16;
const ll INF = 1e18;
int n;
ll x[N], y[N], p[N], ans[N];

void dfs(int i, int c, VL d) {
  ll s = 0;
  REP(i, n) s += d[i] * p[i];
  // cout << i << " " << c << endl;
  // REP(i,n) cout << d[i] << " ";
  // cout << endl;
  chmin(ans[c], s);
  if (i == n)
    return;
  dfs(i + 1, c, d);
  VL dx(d), dy(d);
  REP(j, n) {
    chmin(dx[j], abs(x[j] - x[i]));
    chmin(dy[j], abs(y[j] - y[i]));
  }
  dfs(i + 1, c + 1, dx);
  dfs(i + 1, c + 1, dy);
}

int main(void) {
  cin >> n;
  REP(i, n) {
    cin >> x[i] >> y[i] >> p[i];
    // x[i] = rand() % 10000;
    // y[i] = rand() % 10000;
    // p[i] = rand() % 10000;
  }
  REP(i, n + 1) ans[i] = INF;
  VL d(n);
  REP(i, n) d[i] = min(abs(x[i]), abs(y[i]));
  dfs(0, 0, d);
  REP(i, n + 1) cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

ll calc(VL &xs, ll x) {
  ll ret = 1e18;
  int n = xs.size();
  auto itr = lower_bound(ALL(xs), x);
  if (itr != xs.end())
    chmin(ret, abs(*itr - x));
  if (itr != xs.begin()) {
    itr--;
    chmin(ret, abs(*itr - x));
  }
  return ret;
}

int main(void) {
  int n;
  cin >> n;
  VL x(n), y(n), p(n);
  REP(i, n) {
    cin >> x[i] >> y[i] >> p[i];
    // x[i] = rand() % 10000;
    // y[i] = rand() % 10000;
    // p[i] = rand() % 10000;
  }
  int all = 1;
  REP(i, n) all *= 3;
  VL ans(n + 1, 1e18);
  REP(mask, all) {
    VI d(n);
    int tmp = mask;
    REP(i, n) {
      d[i] = tmp % 3;
      tmp /= 3;
    }
    int cnt = 0;
    VL xs({0}), ys({0});
    REP(i, n) {
      if (d[i] == 0) {
        cnt++;
        xs.push_back(x[i]);
      } else if (d[i] == 1) {
        cnt++;
        ys.push_back(y[i]);
      }
    }
    sort(ALL(xs));
    sort(ALL(ys));
    ll sum = 0;
    REP(i, n) {
      ll mi = 1e18;
      chmin(mi, calc(xs, x[i]));
      chmin(mi, calc(ys, y[i]));
      sum += mi * p[i];
      if (sum >= ans[cnt])
        break;
    }
    chmin(ans[cnt], sum);
  }
  REP(i, n + 1) cout << ans[i] << endl;
  return 0;
}