Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include<iostream>
#include<iomanip>
using namespace std;
#define MAX_N 100000
long double x[MAX_N];
long double y[MAX_N];
int N;
bool solve(long double a, int b) {
	for (int i = 0; i < b; i++) {
		long double X1 = a, Y1 = x[b];
		long double X2 = y[i], Y2 = x[i];
		long double dist = (X2 - X1)*(X2 - X1) + (Y2 - Y1)*(Y2 - Y1);
		if (a < y[i]) { return false; }
		if (dist < (x[i] + x[b])*(x[i] + x[b])) { return false; }
	}
	if (x[b] > a) { return false; }
	return true;
}
int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> x[i];
		if (i == 0) { y[i] = x[i]; }
		else {
			long double L = 0, R = 1000000000.00, M;
			for (int k = 0; k < 1500; k++) {
				M = (L + R) / 2;
				bool a1 = solve(M, i);
				if (a1 == true) { R = M; }
				else { L = M; }
			}
			y[i] = M;
		}
	}
	long double minx = 0;
	for (int i = 0; i < N; i++) {
		long double dis = y[i] + x[i];
		if (dis > minx) { minx = dis; }
	}
	cout << fixed << setprecision(14) << minx << endl;
	return 0;
}