Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;

double calc(int r1, int r2) {
    if (r1 < r2) {
        swap(r1, r2);
    }

    return sqrt((r1 + r2) * (r1 + r2) - (r2 - r1) * (r2 - r1));
}

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

    vector<int> r(N);
    for (int i = 0; i < N; ++i) {
        cin >> r[i];
    }

    double ans = 0.0;
    vector<double> c;
    for (int i = 0; i < N; ++i) {
        double cc = r[i];
        for (unsigned int j = 0; j < c.size(); ++j) {
            double d = calc(r[i], r[j]);
            cc = max(cc, c[j] + d);
        }
        c.push_back(cc);
        ans = max(ans, cc + r[i]);
    }

    printf("%.10f\n", ans);

    return 0;
}