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

#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

const int N = 15;

int n;

int x[N];
int y[N];
int a[N];

double mst[1 << N];
double sum[1 << N];
double dist[N][N];
int dp[1 << N];
int ok[1 << N];

VPII E;

bool check(double k) {
  for (int i = 0; i < 1 << n; ++i) {
    dp[i] = 0;

    if (sum[i] >= __builtin_popcount(i) * k + mst[i])
      ok[i] = 1;
    else
      ok[i] = 0;
  }

  dp[0] = 1;

  for (int i = 0; i < 1 << n; ++i) {
    for (int j = i;; j = (j - 1) & i) {
      if (ok[j] && dp[i ^ j]) {
        dp[i] = 1;
        break;
      }

      if (!j)
        break;
    }
  }

  return dp[(1 << n) - 1];
}

int p[N];

int Find(int a) {
  if (p[a] != a)
    p[a] = Find(p[a]);

  return p[a];
}

double licz(int m) {
  for (int i = 0; i < n; ++i) {
    p[i] = i;
  }

  double res = 0;

  for (auto &it : E) {
    if ((m & (1 << it.st)) && (m & (1 << it.nd))) {
      int a = Find(it.st);
      int b = Find(it.nd);

      if (a != b) {
        res += dist[it.st][it.nd];
        p[a] = b;
      }
    }
  }

  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> a[i];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      dist[i][j] = sqrt((ll)(x[i] - x[j]) * (x[i] - x[j]) +
                        (ll)(y[i] - y[j]) * (y[i] - y[j]));
      E.eb(i, j);
    }
  }

  sort(ALL(E),
       [](PII a, PII b) { return dist[a.st][a.nd] < dist[b.st][b.nd]; });

  for (int i = 0; i < 1 << n; ++i) {
    mst[i] = licz(i);
  }

  for (int i = 0; i < 1 << n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j))
        sum[i] += a[j];
    }
  }

  double l = 0, r = 1e9;

  for (int i = 0; i < 70; ++i) {
    double m = (l + r) / 2;

    if (check(m))
      l = m;
    else
      r = m;
  }

  cout << fixed << setprecision(20) << l << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

const int N = 15;

int n;

int x[N];
int y[N];
int a[N];

double mst[1 << N];
double sum[1 << N];
double dist[N][N];
int dp[1 << N];
int ok[1 << N];

VPII E;

bool check(double k) {
  for (int i = 0; i < 1 << n; ++i) {
    dp[i] = 0;

    if (sum[i] >= __builtin_popcount(i) * k + mst[i])
      ok[i] = 1;
    else
      ok[i] = 0;
  }

  dp[0] = 1;

  for (int i = 0; i < 1 << n; ++i) {
    for (int j = i;; j = (j - 1) & i) {
      if (ok[j] && dp[i ^ j])
        dp[i] = 1;

      if (!j)
        break;
    }
  }

  return dp[(1 << n) - 1];
}

int p[N];

int Find(int a) {
  if (p[a] != a)
    p[a] = Find(p[a]);

  return p[a];
}

double licz(int m) {
  for (int i = 0; i < n; ++i) {
    p[i] = i;
  }

  double res = 0;

  for (auto &it : E) {
    if ((m & (1 << it.st)) && (m & (1 << it.nd))) {
      int a = Find(it.st);
      int b = Find(it.nd);

      if (a != b) {
        res += dist[it.st][it.nd];
        p[a] = b;
      }
    }
  }

  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  cin >> n;

  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> a[i];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      dist[i][j] = sqrt((ll)(x[i] - x[j]) * (x[i] - x[j]) +
                        (ll)(y[i] - y[j]) * (y[i] - y[j]));
      E.eb(i, j);
    }
  }

  sort(ALL(E),
       [](PII a, PII b) { return dist[a.st][a.nd] < dist[b.st][b.nd]; });

  for (int i = 0; i < 1 << n; ++i) {
    mst[i] = licz(i);
  }

  for (int i = 0; i < 1 << n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j))
        sum[i] += a[j];
    }
  }

  double l = 0, r = 1e9;

  for (int i = 0; i < 70; ++i) {
    double m = (l + r) / 2;

    if (check(m))
      l = m;
    else
      r = m;
  }

  cout << fixed << setprecision(20) << l << endl;
}