Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        v.push_back(x);
    }

    // x coordinates delta between collided cylinder which radius are a and b
    auto delta = [](double a, double b) {
        // d^2 + (b  - a)^2 = (b + a)^2
        // d = sqrt((b + a)^2 - (b - a)^2)
        // d = sqrt(4 * a * b)
        return 2 * sqrt(a * b);
    };

    double sum_ = 0;
    vector<double> cylinderXCoordinates;
    for (int i = 0; i < v.size(); ++i) {
        // first, collided with left wall
        double max_ = v[i];
        // find rightmost cylinder x coordinates
        for (int j = i - 1; j >= 0; --j) {
            auto d = cylinderXCoordinates[j] + delta(v[j], v[i]);
            max_ = max(max_, d);
        }
        cylinderXCoordinates.push_back(max_);
        sum_ = max(sum_, max_ + v[i]);
    }
    printf("%.8f\n", sum_);
}