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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <complex>
#include <numeric>
#include <utility>

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 1000000007LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t> inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++)
#define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++)
#define REPI(v, vs) for (auto &v : vs)

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << (x) << " (l" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl

#define MAX_VALUE 9223372036854775807

int solve() {
  ll n;
  cin >> n;

  vector<tuple<ll, ll, ll>> xyps;
  ll x, y, p;
  REPD(i, 0, n) {
    cin >> x >> y >> p;
    xyps.emplace_back(x, y, p);
  }

  vector<vector<ll>> min_xs(n, vector<ll>(1LL << n, MAX_VALUE));
  vector<vector<ll>> min_ys(n, vector<ll>(1LL << n, MAX_VALUE));
  ll xk, yk, pk;
  REPD(i, 0, n) {
    REPD(j, 0, 1LL << n) {
      tie(x, y, p) = xyps[i];
      min_xs[i][j] = min(min_xs[i][j], abs(x));
      min_ys[i][j] = min(min_ys[i][j], abs(y));
      REPD(k, 0, n) {
        if ((j >> k) % 2LL == 1LL) {
          tie(xk, yk, pk) = xyps[k];
          min_xs[i][j] = min(min_xs[i][j], abs(x - xk));
          min_ys[i][j] = min(min_ys[i][j], abs(y - yk));
        }
      }
    }
  }

  ll status_count = 1;
  REPD(i, 0, n) status_count *= 3LL;

  vector<ll> results(n + 1, MAX_VALUE);
  REPD(i, 0, status_count) {
    ll status = i;
    ll status_x = 0, status_y = 0, line_count = 0;
    REPD(j, 0, n) {
      status_x <<= 1LL;
      status_y <<= 1LL;
      if (status % 3LL == 1LL) {
        status_x += 1LL;
        ++line_count;
      }
      if (status % 3LL == 2LL) {
        status_y += 1LL;
        ++line_count;
      }
      status /= 3LL;
    }
    ll result = 0;
    REPD(j, 0, n) {
      tie(x, y, p) = xyps[j];
      result += p * min(min_xs[j][status_x], min_ys[j][status_y]);
    }
    results[line_count] = min(results[line_count], result);
  }

  REPD(i, 0, n + 1) cout << results[i] << endl;

  return 0;
}

// main function
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
  // ll t;
  // cin >> t;
  // REPD(i, 0, t) solve();

  return 0;
}


Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <complex>
#include <numeric>
#include <utility>

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 1000000007LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t> inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++)
#define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++)
#define REPI(v, vs) for (auto &v : vs)

// debug
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << (x) << " (l" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl

#define LLONG_MAX 9223372036854775807

vector<vector<ll>> comb(ll n, ll k) {
  if (n == 0)
    return {};
  if (k == 0)
    return {};
  if (n < k)
    return {};

  vector<vector<ll>> res;
  if (k == 1) {
    for (ll i = 0; i < n; ++i) {
      res.emplace_back(vector<ll>(1, i));
    }
    return res;
  }
  if (n == k) {
    vector<ll> v(n);
    for (ll i = 0; i < n; ++i) {
      v[i] = i;
    }
    res.emplace_back(v);
    return res;
  }
  res = comb(n - 1, k);
  vector<vector<ll>> pre = comb(n - 1, k - 1);
  res.reserve(res.size() + pre.size());
  for (ll i = 0; i < pre.size(); ++i) {
    pre[i].emplace_back(n - 1);
  }
  copy(pre.begin(), pre.end(), std::back_inserter(res));

  return res;
}

int solve() {
  ll n;
  cin >> n;

  ll x, y, p;
  vector<tuple<ll, ll, ll>> xyps(n);
  REPD(i, 0, n) {
    cin >> x >> y >> p;
    xyps[i] = {x, y, p};
  }

  REPD(k, 0, n + 1) {
    ll res = LLONG_MAX;

    REPD(row_count, 0, k + 1) {
      ll col_count = k - row_count;
      auto row_combs = comb(n, row_count);
      auto col_combs = comb(n - row_count, col_count);
      row_combs.emplace_back(vector<ll>());
      col_combs.emplace_back(vector<ll>());
      REPI(row_comb, row_combs) {
        REPI(col_comb, col_combs) {
          vector<ll> rows(row_count + 1);
          vector<ll> cols(col_count + 1);
          REPD(i, 0, row_comb.size()) { rows[i] = get<1>(xyps[row_comb[i]]); }

          vector<ll> remains;
          REPD(i, 0, n) {
            if (find(row_comb.begin(), row_comb.end(), i) == row_comb.end()) {
              remains.emplace_back(i);
            }
          }
          REPD(i, 0, col_comb.size()) {
            cols[i] = get<0>(xyps[remains[col_comb[i]]]);
          }
          sort(rows.begin(), rows.end());
          sort(cols.begin(), cols.end());

          ll curr_val = 0;
          REPD(i, 0, n) {
            tie(x, y, p) = xyps[i];
            ll dist = LLONG_MAX;
            REPD(jx, 0, cols.size()) { dist = min(dist, abs(cols[jx] - x)); }
            REPD(jy, 0, rows.size()) { dist = min(dist, abs(rows[jy] - y)); }
            curr_val += dist * p;
          }
          res = min(res, curr_val);
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}

// main function
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}