Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

int main() {
    int N;
    cin >> N;
    int r[N];
    for (int i = 0; i < N; i++) {
        cin >> r[i];
    }
    double pos[N];
    pos[0] = r[0];
    double ans = 2 * r[0];
    for (int i = 1; i < N; i++) {
        double maxpos = r[i];
        for (int j = 0; j < i; j++) {
            double d = r[i] + r[j];
            double h = abs(r[i] - r[j]);
            double x = pos[j] + sqrt(d * d - h * h);
            if (maxpos < x) {
                maxpos = x;
            }
        }
        pos[i] = maxpos;
        if (pos[i] + r[i] > ans) {
            ans = pos[i] + r[i];
        }
    }
    cout << fixed;
    cout << setprecision(8);
    cout << ans << endl;
}