Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    
    vector<double> b(n);
    b[0] = a[0];
    for (int i = 1; i < n; i++) {
        double m = 0;
        int r = a[i];
        for (int j = 0; j < i; j++) {
            int l = a[j];
            int h = max(l,r) - min(l,r);
            int s = l+r;
            double bot = sqrt(s*s - h*h);
            m = max(m, b[j]+bot);
        }
        b[i] = m;
    }
    double l = 1e9, r = 0;
    for (int i = 0; i < n; i++) {
        l = min(l, b[i]-a[i]);
        r = max(r, b[i]+a[i]);
    }
    printf("%.12lf\n", r-l);
}