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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

vector<ll> calc(vector<Pll> a) {
  ll n = a.size();
  sort(a.begin(), a.end());
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 1e18));
  vector<ll> res(n + 1, 1e18);
  dp[0][0] = 0;
  rep(l, n) {
    REP(r, l + 1, n + 1) {
      ll now = 1e18;
      REP(k, l, r) {
        ll s = 0;
        REP(i, l, r) { s += abs(a[i].first - a[k].first) * a[i].second; }
        cmin(now, s);
      }
      // cmin(dp[i+1][j], dp[i][j]);
      rep(j, n) cmin(dp[r][j + 1], dp[l][j] + now);

      now = 0;
      REP(i, l, r) { now += abs(a[i].first) * a[i].second; }
      rep(j, n + 1) cmin(dp[r][j], dp[l][j] + now);
    }
  }
  return dp[n];
  /*
  rep(s,(1LL << n)){
      vector<ll> p(1,0);
      rep(i,n){
          if((s>>i)&1LL){
              p.push_back(a[i].first);
          }
      }
      ll now = 0;
      rep(i,n){
          ll m = 1e18;
          rep(j,p.size()) cmin(m, abs(a[i].first - p[j]));
          now += m*a[i].second;
      }
      ll b = p.size() - 1;
      cmin(res[b], now);
  }*/
  // return res;
}

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<ll> ans(N + 1, 1e18);
  rep(s, (1LL << N)) {
    vector<Pll> ax, ay;
    rep(i, N) {
      if ((s >> i) & 1LL) {
        ax.push_back(Pll(X[i], P[i]));
      } else {
        ay.push_back(Pll(Y[i], P[i]));
      }
    }
    vector<ll> dx = calc(ax);
    vector<ll> dy = calc(ay);
    rep(i, dx.size()) {
      rep(j, dy.size()) { cmin(ans[i + j], dx[i] + dy[j]); }
    }
  }
  rep(i, N + 1) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

vector<ll> calc(vector<Pll> a) {
  ll n = a.size();
  vector<ll> res(n + 1, 1e18);
  rep(s, (1LL << n)) {
    vector<ll> p(1, 0);
    rep(i, n) {
      if ((s >> i) & 1LL) {
        p.push_back(a[i].first);
      }
    }
    ll now = 0;
    rep(i, n) {
      ll m = 1e18;
      rep(j, p.size()) cmin(m, abs(a[i].first - p[j]));
      now += m * a[i].second;
    }
    ll b = p.size() - 1;
    cmin(res[b], now);
  }
  return res;
}

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<ll> ans(N + 1, 1e18);
  rep(s, (1LL << N)) {
    vector<Pll> ax, ay;
    rep(i, N) {
      if ((s >> i) & 1LL) {
        ax.push_back(Pll(X[i], P[i]));
      } else {
        ay.push_back(Pll(Y[i], P[i]));
      }
    }
    vector<ll> dx = calc(ax);
    vector<ll> dy = calc(ay);
    rep(i, dx.size()) {
      rep(j, dy.size()) { cmin(ans[i + j], dx[i] + dy[j]); }
    }
  }
  rep(i, N + 1) { cout << ans[i] << endl; }
}