Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
template<typename T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }





int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    
    
    
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    
    double d[n]; // i番目の丸太の中心座標
    d[0] = a[0];
    int table[505] = {0};
    
    rep3(i, 1, n) {
        // 手前のどれか(左の壁を含む)と接してる
        // その最大値
        d[i] = a[i];
        int id = -1;
        rep(j, i) {
            int y = a[i] + a[j];
            int x = abs(a[i] - a[j]);
            double D = d[j] + sqrt(y * y - x * x);
            if (d[i] < D) {
                d[i] = D;
                id = j;
            }
        }
        rep3(j, id + 1, i) {
            table[j] = 1;
        }
    }
    // どれが右端と接してるか
    rep(i, n) {
        rep3(j, i + 1, n) {
            int y = a[i] + a[j];
            int x = abs(a[i] - a[j]);
            double D = sqrt(y * y - x * x);
            if (!(a[i] > a[j] + D)) break;
            if (j == n - 1) {
                rep3(k, i + 1, n) table[k] = 1;
            }
        }
    }
    
    vector<int> b;
    rep(i, n) if (table[i] == 0) b.eb(a[i]);
    // rep(i, sz(b)) cout << b[i] << " "; cout << endl;
    
    double ans = b[0] + b[sz(b) - 1];
    rep3(i, 1, sz(b)) {
        int j = i - 1;
        int y = b[i] + b[j];
        int x = abs(b[i] - b[j]);
        double D = sqrt(y * y - x * x);
        ans += D;
    }
    cout << fixed << setprecision(5) << ans << '\n';
}