Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

double calcDist(double, double);

int main()
{
	int N;
	scanf("%d", &N);
	std::vector<double> r(N);
	int max_i{};
	for (int i{}; i < N; i++)
	{
		scanf("%lf", &r[i]);
		if (r[i] > r[max_i])
			max_i = i;
	}
	std::vector<double> distance(N);
	double max_l{r[max_i]}, max_r{r[max_i]};
	for (int i{max_i - 1}; i >= 0; i--)
	{
		for (int j{max_i}; j > i; j--)
			distance[i] = std::max(distance[i], distance[j] + calcDist(r[i], r[j]));
		max_l = std::max(distance[i] + r[i], max_l);
	}
	for (int i{max_i + 1}; i < N; i++)
	{
		for (int j{max_i}; j < i; j++)
			distance[i] = std::max(distance[i], distance[j] + calcDist(r[i], r[j]));
		max_r = std::max(distance[i] + r[i], max_r);
	}

	std::cout << std::fixed << std::setprecision(10) << max_l + max_r << std::endl;
	return 0;
}

double calcDist(double a, double b)
{
	return sqrt((a + b) * (a + b) - (a - b) * (a - b));
}