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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

ll disX[1 << 16][16];
ll disY[1 << 16][16];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll X[N], Y[N], P[N];
  REP(i, 0, N) { cin >> X[i] >> Y[i] >> P[i]; }
  REP(i, 0, 1 << N) {
    REP(j, 0, N) {
      disX[i][j] = abs(X[j]);
      disY[i][j] = abs(Y[j]);
      REP(k, 0, N) {
        if (i & (1LL << k)) {
          disX[i][j] = min(disX[i][j], abs(X[j] - X[k]));
          disY[i][j] = min(disY[i][j], abs(Y[j] - Y[k]));
        }
      }
    }
  }
  ll ans[N + 1];
  fill(ans, ans + N + 1, inf);
  REP(bit, 0, 1 << N) {
    ll bitnum = 0;
    REP(j, 0, N) {
      if (bit & (1LL << j)) {
        bitnum++;
      }
    }
    for (ll i = bit; i >= 0; i--) {
      i &= bit;
      ll tmp = 0;
      REP(j, 0, N) {
        if (!(i & (1LL << j))) {
          tmp += min(disX[i][j], disY[bit - i][j]) * P[j];
        }
      }
      ans[bitnum] = min(ans[bitnum], tmp);
    }
  }
  REP(i, 0, N + 1) { cout << ans[i] << endl; }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  cin >> N;
  ll X[N], Y[N], P[N];
  REP(i, 0, N) { cin >> X[i] >> Y[i] >> P[i]; }
  ll ans[N + 1];
  fill(ans, ans + N + 1, inf);
  ans[N] = 0;
  REP(bit, 0, (1LL << N)) {
    ll bitnum = 0;
    vll rem;
    vll xs;
    xs.push_back(-25000);
    xs.push_back(0);
    xs.push_back(25000);
    REP(i, 0, N) {
      if (bit & (1LL << i)) {
        bitnum++;
        xs.push_back(X[i]);
      } else {
        rem.push_back(i);
      }
    }
    sort(xs.begin(), xs.end());
    ll remnum = N - bitnum;
    REP(bitb, 0, (1LL << remnum)) {
      vll ys;
      ys.push_back(-25000);
      ys.push_back(0);
      ys.push_back(25000);
      ll bitnumb = 0;
      REP(i, 0, remnum) {
        if (bitb & (1LL << i)) {
          bitnumb++;
          ys.push_back(Y[rem[i]]);
        }
      }
      sort(ys.begin(), ys.end());
      ll tmp = 0;
      REP(i, 0, remnum) {
        if (!(bitb & (1LL << i))) {
          ll xdat = X[rem[i]];
          ll xup = lower_bound(xs.begin(), xs.end(), xdat) - xs.begin();
          ll xdis = min(xs[xup] - xdat, xdat - xs[xup - 1]);
          ll ydat = Y[rem[i]];
          ll yup = lower_bound(ys.begin(), ys.end(), ydat) - ys.begin();
          ll ydis = min(ys[yup] - ydat, ydat - ys[yup - 1]);
          tmp += min(xdis, ydis) * P[rem[i]];
        }
      }
      // if (bitnum + bitnumb == 1) {
      //     cout << bitnum << " " << bitnumb << " ";
      //     cout << tmp << endl;
      //     REP(i, 0, rem.size()) {
      //         cout << rem[i] << " ";
      //     }
      //     cout << endl;
      // }
      ans[bitnum + bitnumb] = min(ans[bitnum + bitnumb], tmp);
    }
  }
  REP(i, 0, N + 1) { cout << ans[i] << endl; }
}