Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;


using Real = double;
using Point = complex<Real>;
const Real EPS = 1e-8;

struct Circle {
    Point p;
    Real r;
    Circle() :p(Point(0, 0)), r(0) {}
    Circle(Point _p, Real _r) :p(_p), r(_r) {}
};

using Points = vector<Point>;
using Circles = vector<Circle>;

bool eq(Real a, Real b) { return abs(a - b) < EPS; }

int intersect(Circle c1, Circle c2) {
    if (c1.r < c2.r) swap(c1, c2);
    Real d = abs(c1.p - c2.p);
    if (c1.r + c2.r < d) return 4;
    if (eq(c1.r + c2.r, d)) return 3;
    if (c1.r - c2.r < d) return 2;
    if (eq(c1.r - c2.r, d)) return 1;
    return 0;
}

pair<Point, Point> crosspoint(Circle& c1, Circle& c2) {
    Real d = abs(c1.p - c2.p);
    Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));
    Real t = arg(c2.p - c1.p);
    Point p1 = c1.p + polar(c1.r, t + a);
    Point p2 = c1.p + polar(c1.r, t - a);
    return { p1, p2 };
}


Real scan() {
    int x; scanf("%d", &x);
    return (Real)x;
}
void solve() {
    int n; scanf("%d", &n);
    vector<Real> r(n); for (int i = 0; i < n; i++) r[i] = scan();
    Circles c(n); c[0] = Circle(Point(r[0], r[0]), r[0]);
    Real mx = r[0];
    for (int i = 1; i < n; i++) {
        Real lf = max(r[i], mx);
        Real rg = inf;
        for (int kkt = 0; kkt <= 89; kkt++) {
            Real mid = (lf + rg) / 2.0;
            bool ok = true;
            Circle t(Point(mid, r[i]), r[i]);
            for (int j = 0; j < i; j++) {
                int f = intersect(c[j], t);
                if (f < 3) { ok = false; break; }
            }
            if (ok) rg = mid;
            else lf = mid;
        }
        chmax(mx, lf);
        c[i] = Circle(Point(lf, r[i]), r[i]);
    }
    Real res = 0;
    for (int i = 0; i < n; i++) chmax(res, c[i].p.real() + c[i].r);
    cout << fixed << setprecision(12) << res << "\n";
}

int main()
{
    /*
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    */

	int kkt = 89;
	while (kkt) {
		solve();
        break;
	}
    return 0;
}