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

#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define dep(i, a, n) for (int i = (a); i >= (n); --i)
#define eps 1e-8
#define pi 3.1415926535897
#define sqr(x) ((x) * (x))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SZ(x) ((int)(x).size())
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define POSIN(x, y) (1 <= (x) && (x) <= n && 1 <= (y) && (y) <= m)
#define all(x) (x).begin(), (x).end()
#define COUT(S, x) cout << fixed << setprecision(x) << S << endl
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63 - __builtin_clzll(x))
#define pw(x) ((ll(1)) << (x))
#define upmo(a, b) (((a) = ((a) + (b)) % mo) < 0 ? (a) += mo : (a))
#define mmo(a, b) (((a) = 1ll * (a) * (b) % mo) < 0 ? (a) += mo : (a))
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double lf;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<lf, lf> pff;
typedef complex<double> CD;
const int inf = 0x3f3f3f3f;
const int mo = 1000000007;
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9')
    x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gn(long double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
template <class T1, class T2> inline void gn(T1 &r, T2 &s) { gn(r), gn(s); }
template <class T1, class T2, class T3> inline void gn(T1 &r, T2 &s, T3 &t) {
  gn(r), gn(s), gn(t);
}
template <class T1, class T2, class T3, class T4>
inline void gn(T1 &r, T2 &s, T3 &t, T4 &u) {
  gn(r), gn(s), gn(t), gn(u);
}
inline void gs(char *s) { scanf("%s", s); }
inline void gc(char &c) {
  while ((c = getchar()) > 126 || c < 33)
    ;
}
inline void pc(char c) { putchar(c); }
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mo;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
  }
  return res;
}
ll powmod(ll a, ll b, ll mo) {
  ll res = 1;
  a %= mo;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
//*******************************************

const int N = 111111, M = 111111;
int l, m, n, t, C;
pll a[N];
ll v[N];
ll ans[N];
ll px[N][22], py[N][22];
int main() {
#ifdef LOCAL
  freopen("E.in", "r", stdin); // freopen("E.out","w",stdout);
#endif
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld%lld%lld", &a[i].X, &a[i].Y, &v[i]);
  rep(i, 0, n + 1) ans[i] = 1ll << 60;
  rep(msk, 0, (1 << n) - 1) {
    rep(i, 1, n) {
      px[msk][i] = py[msk][i] = 1ll << 60;
      rep(j, 1, n) if (msk >> j - 1 & 1) MIN(px[msk][i], abs(a[i].X - a[j].X));
      rep(j, 1, n) if (msk >> j - 1 & 1) MIN(py[msk][i], abs(a[i].Y - a[j].Y));
    }
  }
  rep(msk, 0, (1 << n) - 1) {                  // all
    for (int m2 = msk;; m2 = msk & (m2 - 1)) { // x axis
      // printf("%d %d\\n",msk,m2);
      ll su = 0;
      rep(i, 1, n) if (!(msk >> i - 1 & 1)) {
        ll mn = min(abs(a[i].X), abs(a[i].Y));
        MIN(mn, px[m2][i]);
        MIN(mn, py[msk ^ m2][i]);
        su += mn * v[i];
        // if(buli(msk)==2)printf(" %d %lld %d\\n",msk,mn,i);
      }
      // if(buli(msk)==2)printf("%d %lld\\n",msk,su);
      MIN(ans[buli(msk)], su);
      if (!m2)
        break;
    }
  }
  rep(i, 0, n) printf("%lld\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define dep(i, a, n) for (int i = (a); i >= (n); --i)
#define eps 1e-8
#define pi 3.1415926535897
#define sqr(x) ((x) * (x))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define SZ(x) ((int)(x).size())
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define POSIN(x, y) (1 <= (x) && (x) <= n && 1 <= (y) && (y) <= m)
#define all(x) (x).begin(), (x).end()
#define COUT(S, x) cout << fixed << setprecision(x) << S << endl
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63 - __builtin_clzll(x))
#define pw(x) ((ll(1)) << (x))
#define upmo(a, b) (((a) = ((a) + (b)) % mo) < 0 ? (a) += mo : (a))
#define mmo(a, b) (((a) = 1ll * (a) * (b) % mo) < 0 ? (a) += mo : (a))
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double lf;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<lf, lf> pff;
typedef complex<double> CD;
const int inf = 0x3f3f3f3f;
const int mo = 1000000007;
inline void gn(long long &x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9')
    x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long &x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gn(long double &x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
template <class T1, class T2> inline void gn(T1 &r, T2 &s) { gn(r), gn(s); }
template <class T1, class T2, class T3> inline void gn(T1 &r, T2 &s, T3 &t) {
  gn(r), gn(s), gn(t);
}
template <class T1, class T2, class T3, class T4>
inline void gn(T1 &r, T2 &s, T3 &t, T4 &u) {
  gn(r), gn(s), gn(t), gn(u);
}
inline void gs(char *s) { scanf("%s", s); }
inline void gc(char &c) {
  while ((c = getchar()) > 126 || c < 33)
    ;
}
inline void pc(char c) { putchar(c); }
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mo;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
  }
  return res;
}
ll powmod(ll a, ll b, ll mo) {
  ll res = 1;
  a %= mo;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mo;
    a = a * a % mo;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
//*******************************************

const int N = 111111, M = 111111;
int l, m, n, t, C;
pll a[N];
ll v[N];
ll ans[N];
int main() {
#ifdef LOCAL
  freopen("E.in", "r", stdin); // freopen("E.out","w",stdout);
#endif
  scanf("%d", &n);
  rep(i, 1, n) scanf("%lld%lld%lld", &a[i].X, &a[i].Y, &v[i]);
  rep(i, 0, n + 1) ans[i] = 1ll << 60;
  rep(msk, 0, (1 << n) - 1) {                  // all
    for (int m2 = msk;; m2 = msk & (m2 - 1)) { // x axis
      // printf("%d %d\\n",msk,m2);
      ll su = 0;
      rep(i, 1, n) if (!(msk >> i - 1 & 1)) {
        ll mn = min(abs(a[i].X), abs(a[i].Y));
        rep(j, 1, n) if (m2 >> j - 1 & 1) MIN(mn, abs(a[i].X - a[j].X));
        else if ((msk >> j - 1 & 1)) MIN(mn, abs(a[i].Y - a[j].Y));
        su += mn * v[i];
        // if(buli(msk)==2)printf(" %d %lld %d\\n",msk,mn,i);
      }
      // if(buli(msk)==2)printf("%d %lld\\n",msk,su);
      MIN(ans[buli(msk)], su);
      if (!m2)
        break;
    }
  }
  rep(i, 0, n) printf("%lld\\n", ans[i]);
  return 0;
}