Problem p01490 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

typedef long double D;

const int LOOP = 200;
const D PI = acos(-1);
const D EPS = 1e-8;
D calc(vint r) {
  int n = r.size();

  D ret = 0.0;
  do {

    vint rr(n);
    rep(i, n) rr[i] = r[i] * r[(i + 1) % n];

    int ei = LLONG_MAX;
    rep(i, n) chmin(ei, rr[i]);
    D lb = -ei;
    D ub = ei;

    rep(i, LOOP) {
      D mid = (lb + ub) / 2.0;
      D sum = 0.0;
      rep(j, n) sum += acos(mid / rr[j]);
      if (sum > 2 * PI)
        lb = mid;
      else
        ub = mid;
    }
    D sum = 0.0;
    rep(j, n) sum += acos(lb / rr[j]);
    if (abs(sum - 2 * PI) > EPS) {
      continue; //???????
    }
    sum = 0.0;
    rep(i, n) sum += rr[i] * sin(acos(lb / rr[i]));
    chmax(ret, sum);
  } while (next_permutation(all(r)));
  return ret;
}

signed main() {
  int N;
  cin >> N;
  vint R(N);
  rep(i, N) cin >> R[i];

  sort(all(R));

  D ans = 0.0;

  rep(i, 1 << N) {
    int num = __builtin_popcount(i);
    if (num <= 2)
      continue;
    vint r;
    rep(j, N) if (i >> j & 1) r.pb(R[j]);
    chmax(ans, calc(r));
  }

  printf("%.20Lf\n", ans / 2.0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

typedef long double D;

const int LOOP = 100;
const D PI = acos(-1);
const D EPS = 1e-8;
D calc(vint r) {
  int n = r.size();

  D ret = 0.0;
  do {

    vint rr(n);
    rep(i, n) rr[i] = r[i] * r[(i + 1) % n];

    int ei = LLONG_MAX;
    rep(i, n) chmin(ei, rr[i]);
    D lb = -ei;
    D ub = ei;

    rep(i, LOOP) {
      D mid = (lb + ub) / 2.0;
      D sum = 0.0;
      rep(j, n) sum += acos(mid / rr[j]);
      if (sum > 2 * PI)
        lb = mid;
      else
        ub = mid;
    }
    D sum = 0.0;
    rep(j, n) sum += acos(lb / rr[j]);
    if (abs(sum - 2 * PI) > EPS) {
      continue; //???????
    }
    sum = 0.0;
    rep(i, n) sum += rr[i] * sin(acos(lb / rr[i]));
    chmax(ret, sum);
  } while (next_permutation(all(r)));
  return ret;
}

signed main() {
  int N;
  cin >> N;
  vint R(N);
  rep(i, N) cin >> R[i];

  sort(all(R));

  D ans = 0.0;

  rep(i, 1 << N) {
    int num = __builtin_popcount(i);
    if (num <= 2)
      continue;
    vint r;
    rep(j, N) if (i >> j & 1) r.pb(R[j]);
    chmax(ans, calc(r));
  }

  printf("%.20Lf\n", ans / 2.0);
  return 0;
}