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

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n;

// bit全探索
vi bt1(16);
vi bt2(16);
ll w1 = 0;
ll w2 = 0;

ll chk(ll j) {
  ll jj = j;
  ll ret = 0;
  for (ll k = 0; k < n; k++) {
    if (jj % 3 != 0)
      ret++;
    jj /= 3;
  }

  w1 = 0;
  w2 = 0;
  jj = j;
  for (ll k = 0; k < n; k++) {
    if (jj % 3 == 1) {
      bt1[w1] = k;
      w1++;
    }

    if (jj % 3 == 2) {
      bt2[w2] = k;
      w2++;
    }
    jj /= 3;
  }
  return ret;
}

int main() {
  // ll ans=0;
  cin >> n;

  vi keta(n * 2 + 1);
  vi x(n), y(n), p(n);
  REP(i, n) { cin >> x[i] >> y[i] >> p[i]; }

  vvi xx(n, vi(1 << n));
  vvi yy(n, vi(1 << n));

  ll mn;
  for (ll kk = 0; kk < n; kk++) {
    for (ll i = 0; i < (1 << n); i++) {
      mn = abs(x[kk]);
      for (ll j = 0; j < n; j++) {
        if (i & (1 << j)) {
          mn = min(mn, abs(x[kk] - x[j]));
        }
      }
      xx[kk][i] = mn * p[kk];
    }
  }

  for (ll kk = 0; kk < n; kk++) {
    for (ll i = 0; i < (1 << n); i++) {
      mn = abs(y[kk]);
      for (ll j = 0; j < n; j++) {
        if (i & (1 << j)) {
          mn = min(mn, abs(y[kk] - y[j]));
        }
      }
      yy[kk][i] = mn * p[kk];
    }
  }

  ll nn = 1;
  for (ll j = 0; j < n; j++)
    nn *= 3;

  vi ans(n + 1, llMAX);

  ans[0] = 0;
  for (ll k = 0; k < n; k++) {
    ans[0] += min(abs(x[k]), abs(y[k])) * p[k];
  }

  ans[n] = 0;

  // 4bit全探索
  /*
  for(int j=0;j<i;j++)
  {
    keta[j]=j;
  }
  */

  ll tmpans = 0;
  ll leng;

  ll c = 0;
  ll idx1 = 0, idx2 = 0;
  int jj;
  ll tmpj;

  for (int j = 0; j < nn; j++) {
    /*
    //数を生成
    j=0;
    for(int jj=0;jj<i;jj++)
      j+=(1<<keta[jj]);
    //keta更新
    int oflow=0;
    int cnt=0;
    for(int jj=i-1;jj>=0;jj--)
    {
      keta[jj]++;
      if (keta[jj]>=n*2-cnt){
        keta[jj]=-1;
      }else{
        break;
      }
      cnt++;
    }
    for(int jj=0;jj<i-1;jj++)
    {
      if (keta[jj]>=keta[jj+1]){
        keta[jj+1]=keta[jj]+1;
      }
    }
    */

    // ll c=chk(j);
    // ll c=j%n;
    // if (c==0)continue;
    c = 0;
    idx1 = 0;
    idx2 = 0;
    jj = j;
    for (ll k = 0; k < n; k++) {
      idx1 *= 2;
      idx2 *= 2;
      tmpj = jj % 3;
      c += (tmpj != 0);
      if (tmpj == 1)
        idx1++;
      if (tmpj == 2)
        idx2++;
      jj /= 3;
    }
    // if (c>=0)continue;

    tmpans = 0;

    // cout<<"j"<<j<<endl;
    for (ll ii = 0; ii < n; ii++) { // 全部の集落
      // ll idx=0;

      /*for(ll kk=0;kk<w1;kk++){
        ll k=bt1[kk];
        idx+=1<<k;
        //leng=min(leng,abs(x[k]-x[ii]));
        //cout<<"?"<<k<<"?";
      }*/
      // leng=min(leng,abs(x[ii]));
      leng = xx[ii][idx1];

      // idx=0;
      /*for(ll kk=0;kk<w2;kk++){
        ll k=bt2[kk];
        idx+=1<<k;
        //leng=min(leng,abs(y[k]-y[ii]));
        //cout<<"?"<<k<<"?";
      }*/
      // leng=min(leng,abs(y[ii]));
      leng = min(yy[ii][idx2], leng);

      tmpans += leng;
    } // tmpansがきまる
    // cout<<"tmpans"<<tmpans<<endl;

    ans[c] = min(ans[c], tmpans);

    /*
    if (keta[0]==-1)break;
    if (keta[i-1]>=n*2)break;
    */

    /*
          for(ll k=0;k<i;k++){
            cout<<keta[k];
          }
          cout<<endl;
          */
    // cout<<"!!!!!!!!!"<<endl;

  } // 4bit全探索のおわり

  for (ll i = 0; i < n + 1; i++) {
    cout << ans[i] << endl;
  }

  // cout<<ans<<endl;
  return 0;
}

/*
15
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903
-23 -18 213815
22 -12 213815
21 -13 224435
-22 -12 136860
-23 -11 239338
-22 12 217647
-21 13 141903
*/

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n;

// bit全探索
vi bt1(16);
vi bt2(16);
ll w1 = 0;
ll w2 = 0;

bool chk(ll j, ll i) {
  ll jj = j;
  ll ret = 0;
  for (ll k = 0; k < n; k++) {
    if (jj % 3 != 0)
      ret++;
    jj /= 3;
  }
  if (ret != i)
    return false;

  w1 = 0;
  w2 = 0;
  jj = j;
  for (ll k = 0; k < n; k++) {
    if (jj % 3 == 1) {
      bt1[w1] = k;
      w1++;
    }

    if (jj % 3 == 2) {
      bt2[w2] = k;
      w2++;
    }
    jj /= 3;
  }
  return true;
}

int main() {
  ll ans = 0;
  cin >> n;

  vi keta(n * 2 + 1);
  vi x(n), y(n), p(n);
  REP(i, n) { cin >> x[i] >> y[i] >> p[i]; }

  ll nn = 1;
  for (ll j = 0; j < n; j++)
    nn *= 3;

  for (ll i = 0; i <= n; i++) {
    if (i == 0) {
      ans = 0;
      for (ll k = 0; k < n; k++) {
        ans += min(abs(x[k]), abs(y[k])) * p[k];
      }
      cout << ans << endl;
      continue;
    }
    if (i == n) {
      cout << 0 << endl;
      continue;
    }

    // 4bit全探索
    /*
    for(int j=0;j<i;j++)
    {
      keta[j]=j;
    }
    */

    ans = llMAX;
    ll tmpans = 0;
    for (ll j = 0; j < nn; j++) {
      /*
      //数を生成
      j=0;
      for(int jj=0;jj<i;jj++)
        j+=(1<<keta[jj]);
      //keta更新
      int oflow=0;
      int cnt=0;
      for(int jj=i-1;jj>=0;jj--)
      {
        keta[jj]++;
        if (keta[jj]>=n*2-cnt){
          keta[jj]=-1;
        }else{
          break;
        }
        cnt++;
      }
      for(int jj=0;jj<i-1;jj++)
      {
        if (keta[jj]>=keta[jj+1]){
          keta[jj+1]=keta[jj]+1;
        }
      }
      */

      if (chk(j, i)) {
        tmpans = 0;
        // cout<<"j"<<j<<endl;
        for (ll ii = 0; ii < n; ii++) { // 全部の集落
          ll leng = 1000000000000;
          for (ll kk = 0; kk < w1; kk++) {
            ll k = bt1[kk];
            leng = min(leng, abs(x[k] - x[ii]));
            // cout<<"?"<<k<<"?";
          }
          leng = min(leng, abs(x[ii]));

          for (ll kk = 0; kk < w2; kk++) {
            ll k = bt2[kk];
            leng = min(leng, abs(y[k] - y[ii]));
            // cout<<"?"<<k<<"?";
          }
          leng = min(leng, abs(y[ii]));

          tmpans += p[ii] * leng;
        } // tmpansがきまる
        // cout<<"tmpans"<<tmpans<<endl;

        ans = min(ans, tmpans);
      }

      /*
      if (keta[0]==-1)break;
      if (keta[i-1]>=n*2)break;
      */

      /*
            for(ll k=0;k<i;k++){
              cout<<keta[k];
            }
            cout<<endl;
            */
      // cout<<"!!!!!!!!!"<<endl;

    } // 4bit全探索のおわり
    cout << ans << endl;
  }

  // cout<<ans<<endl;
  return 0;
}