Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <utility>
#include <vector>
#include <vector>
#include <cmath>
#include <cstdio>
using namespace std;

#define fi first
#define se second

typedef pair<double,int> pdi;

template<class T>
T sqr(T x) { return x * x; }

int main() {
	int N;
	cin >> N;
	vector<int> R;
	vector<pdi> X;
	R.resize(N);
	for(int i = 0; i < R.size(); i++) {
		cin >> R[i];
	}
	X.push_back(pdi(0.0,-1));
	for(int i = 0; i < R.size(); i++) {
		double center_x = 0.0;
		for(int j = 0; j < X.size(); j++) {
			if(X[j].se == -1) {
				center_x = max(center_x, X[j].fi + R[i]);
			}
			else{
				// a^2 = b^2 + c^2
				// b = sqrt(sqr(c) - sqr(c))
				center_x = max(center_x,
						sqrt(sqr(R[i]+X[j].se) - sqr(R[i]-X[j].se)) + X[j].fi);
			}
		}
		X.push_back(pdi(center_x, R[i]));
	}

	double res = 0;
	for(int i = 0; i < X.size(); i++)
		res = max(res, X[i].fi + X[i].se);
	
	printf("%.10f\n", res);
}