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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define bit(n, k) (((ll)n >> (ll)k) & 1) /*nのk bit目*/
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
// ll MOD=1000000007LL;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;
// const ll MAX_V=114514LL;
// const ll MAX = 500010LL;
const ll mod = MOD;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {0, 0, 1, -1};
vl dy = {1, -1, 0, 0};

// 素因数分解O(√n)
map<ll, ll> prime_factor(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

const ll MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
// finvが階乗の逆元

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modpow(ll a, ll n, ll mod) {

  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/
  ll n;
  cin >> n;
  vl x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vvl bitx((1 << n), vl(n)), bity((1 << n), vl(n));
  rep(bit, 1 << n) {
    rep(i, n) {
      bitx[bit][i] = min(abs(x[i]), abs(y[i]));
      bity[bit][i] = min(abs(x[i]), abs(y[i]));
    }
  }
  vl ans(n + 1, INF);
  vl pow(16);
  vl three(16);
  pow[0] = 1;
  three[0] = 1;
  rep(i, 15) pow[i + 1] = pow[i] * 2;
  rep(i, 15) three[i + 1] = three[i] * 3;
  rep(bit, (1 << n)) {
    rep(i, n) {
      rep(j, n) {
        if (bit(bit, j))
          bitx[bit][i] = min(bitx[bit][i], abs(x[i] - x[j]));
      }
    }
  }
  rep(bit, (1 << n)) {
    rep(i, n) {
      rep(j, n) {
        if (bit(bit, j))
          bity[bit][i] = min(bity[bit][i], abs(y[i] - y[j]));
      }
    }
  }
  // rep(bit,1<<n){
  //   rep(i,n)cout<<bitx[bit][i]<<" ";
  //   cout<<endl;
  // }

  rep(bit, three[n]) {

    ll sumx = 0, sumy = 0;
    ll cnt = 0;
    ll tmp = bit;

    rep(i, n) {
      cnt++;
      if (tmp % 3 == 1)
        sumx += pow[i];
      else if (tmp % 3 == 2)
        sumy += pow[i];
      else
        cnt--;
      tmp /= 3;
      if (tmp == 0)
        break;
    }

    // cout<<bit<<" "<<sumx<<" "<<sumy<<endl;
    ll part = 0;

    rep(i, n) { part += p[i] * min(bitx[sumx][i], bity[sumy][i]); }
    ans[cnt] = min(ans[cnt], part);
    // cout<<bit<<" "<<part<<" "<<cnt<<endl;
  }
  rep(i, n + 1) cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define bit(n, k) (((ll)n >> (ll)k) & 1) /*nのk bit目*/
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const long long INF = 1e+18 + 1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
const ll MOD = 1000000007LL;
// ll MOD=1000000007LL;
// const ll MOD=998244353LL;
// const ll MOD=1777777777LL;
// const ll MAX_V=114514LL;
// const ll MAX = 500010LL;
const ll mod = MOD;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx = {0, 0, 1, -1};
vl dy = {1, -1, 0, 0};

// 素因数分解O(√n)
map<ll, ll> prime_factor(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

const ll MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
// finvが階乗の逆元

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modpow(ll a, ll n, ll mod) {

  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/
  ll n;
  cin >> n;
  vl x(n), y(n), p(n);
  rep(i, n) cin >> x[i] >> y[i] >> p[i];
  vvl bitx((1 << n), vl(n)), bity((1 << n), vl(n));
  rep(bit, 1 << n) {
    rep(i, n) {
      bitx[bit][i] = min(abs(x[i]), abs(y[i]));
      bity[bit][i] = min(abs(x[i]), abs(y[i]));
    }
  }
  vl ans(n + 1, INF);
  vl pow(16);
  vl three(16);
  pow[0] = 1;
  three[0] = 1;
  rep(i, 15) pow[i + 1] = pow[i] * 2;
  rep(i, 15) three[i + 1] = three[i] * 3;
  rep(bit, (1 << n)) {
    rep(i, n) {
      rep(j, n) {
        if (bit(bit, j))
          bitx[bit][i] = min(bitx[bit][i], abs(x[i] - x[j]));
      }
    }
  }
  rep(bit, (1 << n)) {
    rep(i, n) {
      rep(j, n) {
        if (bit(bit, j))
          bity[bit][i] = min(bity[bit][i], abs(y[i] - y[j]));
      }
    }
  }
  // rep(bit,1<<n){
  //   rep(i,n)cout<<bitx[bit][i]<<" ";
  //   cout<<endl;
  // }

  rep(bit, three[n]) {

    ll sumx = 0, sumy = 0;
    ll cnt = 0;
    ll tmp = bit;

    rep(i, n) {
      cnt++;
      if (tmp % 3 == 1)
        sumx += pow[i];
      else if (tmp % 3 == 2)
        sumy += pow[i];
      else
        cnt--;
      tmp /= 3;
      if (tmp == 0)
        break;
    }

    // cout<<bit<<" "<<sumx<<" "<<sumy<<endl;
    ll part = 0;

    rep(i, n) { part += p[i] * min(bitx[sumx][i], bity[sumy][i]); }
    ans[cnt] = min(ans[cnt], part);
    // cout<<bit<<" "<<part<<" "<<cnt<<endl;
  }
  rep(i, n + 1) cout << ans[i] << '\\n';
}