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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD;

const ll INF = 1e18;
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> ans(n + 1, INF);
  vector<pair<P, ll>> v(n);
  rep(i, n) cin >> v[i].fi.fi >> v[i].fi.se >> v[i].se;
  int loop = pow(3, n);
  ll lenx[n][1 << n], leny[n][1 << n];
  rep(i, n) {
    rep(j, 1 << n) {
      lenx[i][j] = abs(v[i].fi.fi);
      leny[i][j] = abs(v[i].fi.se);
    }
  }
  rep(i, 1 << n) {
    int tmp = i;
    rep(j, n) {
      if (tmp % 2 == 1) {
        rep(k, n) {
          // put j
          chmin(lenx[k][i], abs(v[k].fi.fi - v[j].fi.fi));
          chmin(leny[k][i], abs(v[k].fi.se - v[j].fi.se));
        }
      }
      tmp /= 2;
    }
  }
  rep(mask, loop) {
    int tmp = mask;
    int cnt = 0;
    ll x = 0, y = 0;
    rep(i, n) {
      if (tmp % 3 == 0) { // ver
        x += (1 << i);
        cnt++;
      } else if (tmp % 3 == 1) { // hor
        y += (1 << i);
        cnt++;
      }
      tmp /= 3;
    }
    if (cnt > n)
      continue;
    ll res = 0;
    rep(i, n) res += min(lenx[i][x], leny[i][y]) * v[i].se;
    chmin(ans[cnt], res);
  }
  rep(i, n) { chmin(ans[i + 1], ans[i]); }
  rep(i, n + 1) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD;

const ll INF = 1e18;
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> ans(n + 1, INF);
  vector<pair<P, ll>> v(n);
  rep(i, n) cin >> v[i].fi.fi >> v[i].fi.se >> v[i].se;
  int loop = pow(3, n);
  ll lenx[n][1 << n], leny[n][1 << n];
  rep(i, n) {
    rep(j, 1 << n) {
      lenx[i][j] = abs(v[i].fi.fi);
      leny[i][j] = abs(v[i].fi.se);
    }
  }
  rep(i, 1 << n) {
    int tmp = i;
    rep(j, n) {
      if (tmp % 2 == 1) {
        rep(k, n) {
          // put j
          chmin(lenx[k][i], abs(v[k].fi.fi - v[j].fi.fi));
          chmin(leny[k][i], abs(v[k].fi.se - v[j].fi.se));
        }
      }
      tmp /= 2;
    }
  }
  rep(mask, loop) {
    int tmp = mask;
    map<int, int> mpx;
    map<int, int> mpy;
    int cnt = 0;
    ll x = 0, y = 0;
    rep(i, n) {
      if (tmp % 3 == 0) { // ver
        if (mpx[v[i].fi.fi + 10000] == 0) {
          x += (1 << i);
          cnt++;
          mpx[v[i].fi.fi + 10000] = 1;
        }
      } else if (tmp % 3 == 1) { // sq
        if (mpy[v[i].fi.se + 10000] == 0) {
          y += (1 << i);
          cnt++;
          mpy[v[i].fi.se + 10000] = 1;
        }
      }
      tmp /= 3;
    }
    if (cnt > 15)
      continue;
    ll res = 0;
    rep(i, n) res += min(lenx[i][x], leny[i][y]) * v[i].se;
    chmin(ans[cnt], res);
  }
  rep(i, n) { chmin(ans[i + 1], ans[i]); }
  rep(i, n + 1) { cout << ans[i] << endl; }
}