Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

double cost(double r1, double r2)
{
	return sqrt((r1 + r2) * (r1 + r2) - (r1 - r2) * (r1 - r2));
}

int main(void)
{
	int N;
	vector<int>r;
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		int rr; cin >> rr; r.push_back(rr);
	}

	vector<double>place(1, 0.);
	for (int i = 1; i < N; ++i)
	{
		double big = 0;
		for (int j = 0; j < i; ++j)
		{
			big = max(big, place[j] + cost(r[j], r[i]));
		}
		place.push_back(big);
	}
	double head = 0, tail = 0;
	for (int i = 0; i < place.size(); ++i)
	{
		head = min(head, place[i] - r[i]);
		tail = max(tail, place[i] + r[i]);
	}
	cout.precision(15);
	cout << tail - head << endl;
	return 0;
}