Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <math.h>

using namespace std;

int n;
double rad[502], c[502], d[502], dsum[502], ans=0;

double calcD(int i, int j){
	if (rad[i]==rad[j]){
		return rad[i]*2;
	}else if (i==0){
		return rad[j];
	}else{
		return sqrt((rad[i]+rad[j])*(rad[i]+rad[j])-(rad[i]-rad[j])*(rad[i]-rad[j]));
	}
}

int main(){
	cin >> n;
	
	rad[0] = 0;
	for (int i=1; i<=n; i++){
		cin >> rad[i];
	}
	
	c[0] = 0;
	c[1] = rad[1];
	
	for (int i=2; i<=n; i++){
		for (int j=0; j<i; j++){
			c[i] = max(c[i], calcD(j, i)+c[j]);
		}
	}
	
	for (int i=1; i<=n; i++){
		// printf("c[%d] = %f\n", i, c[i] );
		ans = max(ans, c[i] + rad[i]);
	}
	
	printf("%.8f\n", ans);
	
	return 0;
}