Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
#include <complex>
using namespace std;

const int INF = 1<<29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int,int> P;
typedef long long ll;

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

    vec r(N);
    for(int i=0;i<N;i++){
        scanf("%d", &r[i]);
    }

    double left = 0, right = 2*r[0];
    vector<double> x(N, 0);
    x[0] = r[0];
    for(int i=1;i<N;i++){
        for(int j=i-1;j>=0;j--){
            x[i] = max(x[i], x[j] + sqrt((r[i]+r[j])*(r[i]+r[j]) - (r[i]-r[j])*(r[i]-r[j])));
        }
        left = min(left, x[i]-r[i]);
        right = max(right, x[i]+r[i]);
    }

    printf("%.6f\n", right - left);

    return 0;
}