Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define ll long long
#define vi vector<long long>
#define pi pair<long long, long long>
#define mp make_pair
#define endl '\n'
#define _ << ' '
#define FAST_LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
const ll mod = (ll)1e9 + 7;
const ll INF = (ll)1e9;
const ll N = (1 << 5);
const double PI = 3.1415926535897;

double n, a, b, ans = 0;
vector<pair<double, double>> towns;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("B.txt", "r", stdin);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    towns.pb({a, b});
  }
  sort(all(towns));
  do {
    for (int i = 0; i < sz(towns) - 1; i++) {
      double dx = towns[i + 1].F - towns[i].F, dy = towns[i + 1].S - towns[i].S;
      ans += sqrt(dx * dx + dy * dy);
    }

  } while (next_permutation(all(towns)));
  for (double i = 2; i <= n; i++) {
    ans /= i;
  }
  cout << fixed << setprecision(9) << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define ll long long
#define vi vector<long long>
#define pi pair<long long, long long>
#define mp make_pair
#define endl '\n'
#define _ << ' '
#define FAST_LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
const ll mod = (ll)1e9 + 7;
const ll INF = (ll)1e9;
const ll N = (1 << 5);
const double PI = 3.1415926535897;

double n, a, b, ans = 0;
vector<pair<double, double>> towns;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("B.txt", "r", stdin);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    towns.pb({a, b});
  }
  sort(all(towns));
  do {
    for (int i = 0; i < sz(towns) - 1; i++) {
      double dx = towns[i + 1].F - towns[i].F, dy = towns[i + 1].S - towns[i].S;
      ans += sqrt(dx * dx + dy * dy);
    }

  } while (next_permutation(all(towns)));
  for (double i = 2; i <= n; i++) {
    ans /= i;
  }
  cout << fixed << setprecision(9) << ans;
}