Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
#define int long long
signed main() {
  int N; cin >> N;
  vector<int> R(N); REP(i,N) cin >> R[i];
  // double ans = 0.0;
  // ans += R[0] + R[N - 1];
  // for(int i = 0; i < N - 1; ++i) {
  //   int mx = max(R[i], R[i + 1]);
  //   int mn = min(R[i], R[i + 1]);
  //   ans += sqrt(max<double>(0.0, (R[i] + R[i + 1]) * (R[i] + R[i + 1]) - (mx - mn) * (mx - mn)));
  // }
  // cout << ans << endl;
  vector<double> pos(N);
  double ans = R[0] * 2;
  pos[0] = R[0];
  for(int i = 1; i < N; ++i) {
    pos[i] = R[i];
    REP(j,i) {
      int mx = max(R[i], R[j]);
      int mn = min(R[i], R[j]);
      double update = pos[j] + sqrt(max<double>(0.0, (R[i] + R[j]) * (R[i] + R[j]) - (mx - mn) * (mx - mn)));
      // DEBUG(update);
      pos[i] = max(pos[i], update);
    }
    ans = max(ans, pos[i] + R[i]);
    // cerr << "pos[" << i << "] = " << pos[i] << endl;
  }
  cout.setf(ios::fixed);
  cout.precision(10);
  cout << ans << endl;
}