Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
using ld = long double;
constexpr ld EPS = 1e-10;

int main() {
    int n;
    cin >> n;
    vector<ld> r(n);
    vector<ld> x(n, 0);
    for(int i=0; i<n; ++i) {
        cin >> r[i];
    }

    for(int i=0; i<n; ++i) {
        if(i == 0) {
            x[i] = r[i];
        } else {
            ld min_x = -1;
            for(int j=0; j<i; ++j) {
                min_x = max(min_x, x[j] + sqrt((r[i] + r[j])*(r[i]+r[j]) - (r[j]-r[i])*(r[j]-r[i])));
            }
            x[i] = min_x;
        }
    }

    cout << fixed << setprecision(8);
    ld left = 0, right = 0;
    for(int i=0; i<n; ++i) {
        left = min(x[i]-r[i], left);
        right = max(x[i]+r[i], right);
    }
    cout << right - left << endl;
}