Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>

using namespace std;

double get_x2(double x, int r, int r2) {
	/*
	float temp = r + (r2 * 2);
	if (temp < sqrt2 * r) {
		return x;
	} else {
	*/
		return x + sqrt(pow(r2 + r, 2) - pow(r - r2, 2));
	//}
}

int main() {
	int n;
	cin >> n;

	vector<int> r(n);
	vector<double> pos(n);

	for (int i = 0; i < n; i++) {
		cin >> r[i];
	}

	pos[0] = 0;
	for (int i = 1; i < n; i++) {
		double temp = 0;
		for (int j = 0; j < i; j++) {
			temp = max(temp, get_x2(pos[j], r[j], r[i]));
		}
		pos[i] = temp;
	}

	double max_r1 = pos[n - 1] + r[n -1];
	double min_r1 = pos[0] - r[0];

	for (int i = 0; i < n; i++) {
		max_r1 = max(max_r1, pos[i] + r[i]);
		min_r1 = min(min_r1, pos[i] - r[i]);
	}

	printf("%.8lf\n", max_r1 - min_r1);
}