Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

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

using namespace std;

int n;
double r[505];
double p[505];

int main(void)
{
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> r[i];

  p[1] = r[1];
  for(int i = 2; i <= n; i++){
    p[i] = r[i];
    for(int j = 1; j < i; j++){
      double r1 = r[i], r2 = r[j];
      if(r1 > r2) swap(r1, r2);
      double l = sqrt( (r1+r2)*(r1+r2)-(r2-r1)*(r2-r1) );
      p[i] = max(p[i], p[j]+l);
    }
  }
  double ans = 0;
  for(int i = 1; i <= n; i++) ans = max(ans, p[i]+r[i]);
  printf("%.11f\n", ans);

  return 0;
}