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

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
ll x[16], y[16], p[16];
vector<int> a;
vector<ll> ans;

void dfs(const vector<vector<ll>> &dx, const vector<vector<ll>> &dy) {
  if (a.size() == n) {
    int cnt = 0;
    ll xmask = 0, ymask = 0;
    rep(i, n) {
      if (a[i] == 1) {
        ++cnt;
        xmask |= (1ll << i);
      }
      if (a[i] == 2) {
        ++cnt;
        ymask |= (1ll << i);
      }
    }

    ll tmp = 0;
    rep(i, n) {
      ll res = LINF;
      if (a[i] == 0) {
        chmin(res, dx[xmask][i]);
        chmin(res, dy[ymask][i]);
        tmp += res * p[i];
      }
    }
    chmin(ans[cnt], tmp);
    return;
  }

  rep(i, 3) {
    a.push_back(i);
    dfs(dx, dy);
    a.pop_back();
  }
  return;
}

int main(int argc, char const *argv[]) {
  scanf("%d", &n);
  ans.assign(n + 1, LINF);
  rep(i, n) scanf("%lld %lld %lld", x + i, y + i, p + i);

  vector<vector<ll>> dx((1 << n), vector<ll>(n, LINF));
  vector<vector<ll>> dy((1 << n), vector<ll>(n, LINF));
  for (int bit = 0; bit < (1 << n); ++bit) {
    vector<int> X{0}, Y{0};
    rep(i, n) if (bit & (1 << i)) {
      X.push_back(x[i]);
      Y.push_back(y[i]);
    }

    vector<ll> p(n);
    rep(i, n) if (!(bit & (1 << i))) {
      ll tmp = LINF;
      for (auto &&e : X)
        chmin(dx[bit][i], abs(e - x[i]));
      for (auto &&e : Y)
        chmin(dy[bit][i], abs(e - y[i]));
    }
  }

  dfs(dx, dy);

  for (auto &&e : ans)
    cout << e << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
ll x[16], y[16], p[16];
vector<int> a;
vector<ll> ans;

void dfs(const vector<vector<ll>> &dx, const vector<vector<ll>> &dy) {
  if (a.size() == n) {
    int cnt = 0;
    set<int> X{0}, Y{0};
    ll xmask = 0, ymask = 0;
    rep(i, n) {
      if (a[i] == 1) {
        X.insert(x[i]);
        ++cnt;
        xmask |= (1ll << i);
      }
      if (a[i] == 2) {
        Y.insert(y[i]);
        ++cnt;
        ymask |= (1ll << i);
      }
    }

    ll tmp = 0;
    rep(i, n) {
      ll res = LINF;
      if (a[i] == 0) {
        chmin(res, dx[xmask][i]);
        chmin(res, dy[ymask][i]);
        tmp += res * p[i];
      }
    }
    chmin(ans[cnt], tmp);
    return;
  }

  rep(i, 3) {
    a.push_back(i);
    dfs(dx, dy);
    a.pop_back();
  }
  return;
}

int main(int argc, char const *argv[]) {
  scanf("%d", &n);
  ans.assign(n + 1, LINF);
  rep(i, n) scanf("%lld %lld %lld", x + i, y + i, p + i);

  vector<vector<ll>> dx((1 << n), vector<ll>(n, LINF));
  vector<vector<ll>> dy((1 << n), vector<ll>(n, LINF));
  for (int bit = 0; bit < (1 << n); ++bit) {
    vector<int> X{0}, Y{0};
    rep(i, n) if (bit & (1 << i)) {
      X.push_back(x[i]);
      Y.push_back(y[i]);
    }

    vector<ll> p(n);
    rep(i, n) if (!(bit & (1 << i))) {
      ll tmp = LINF;
      for (auto &&e : X)
        chmin(dx[bit][i], abs(e - x[i]));
      for (auto &&e : Y)
        chmin(dy[bit][i], abs(e - y[i]));
    }
  }

  dfs(dx, dy);

  for (auto &&e : ans)
    cout << e << '\\n';

  return 0;
}