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

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, n) for (int i = (int)(n); i >= 0; --i)
#define RFOR(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define ALL(a) (a).begin(), (a).end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;

const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

ll mi[16];
ll x[15], y[15];
ll a[15];
int sel[15];

void dfs(int n, int k, vll &kyori) {
  if (n == k) {
    int cnt = 0;
    ll now = 0;
    REP(i, n) {
      cnt += sel[i] != -1;
      now += kyori[i] * a[i];
    }
    chmin(mi[cnt], now);
  } else {
    vll prev = kyori;
    sel[k] = -1;
    dfs(n, k + 1, kyori);
    sel[k] = 0;
    REP(i, n) { kyori[i] = min(prev[i], abs(x[i] - x[k])); }
    dfs(n, k + 1, kyori);
    sel[k] = 1;
    REP(i, n) { kyori[i] = min(prev[i], abs(y[i] - y[k])); }
    dfs(n, k + 1, kyori);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n;
  cin >> n;
  REP(i, n) { cin >> x[i] >> y[i] >> a[i]; }

  REP(i, n + 1) { mi[i] = LINF; }

  vll kyori(n);
  REP(i, n) { kyori[i] = min(abs(x[i]), abs(y[i])); }
  dfs(n, 0, kyori);

  REP(i, n + 1) { cout << mi[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, n) for (int i = (int)(n); i >= 0; --i)
#define RFOR(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define ALL(a) (a).begin(), (a).end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;

const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

ll mi[16];
ll x[15], y[15], a[15];
int sel[15];

void dfs(int n, int k) {
  if (n == k) {
    int cnt = 0;
    REP(i, n) { cnt += sel[i] != -1; }
    ll now = 0;
    REP(i, n) {
      if (sel[i] != -1)
        continue;
      ll miadd = min(abs(x[i]), abs(y[i])) * a[i];
      REP(j, n) {
        if (sel[j] == 0) {
          chmin(miadd, abs(x[i] - x[j]) * a[i]);
        } else if (sel[j] == 1) {
          chmin(miadd, abs(y[i] - y[j]) * a[i]);
        }
      }
      now += miadd;
    }
    chmin(mi[cnt], now);
  } else {
    sel[k] = -1;
    dfs(n, k + 1);
    sel[k] = 0;
    dfs(n, k + 1);
    sel[k] = 1;
    dfs(n, k + 1);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n;
  cin >> n;
  REP(i, n) { cin >> x[i] >> y[i] >> a[i]; }

  REP(i, n + 1) { mi[i] = LINF; }

  vi sel(n);
  dfs(n, 0);

  REP(i, n + 1) { cout << mi[i] << endl; }
}