Problem: Example

Input

2
10 10


Output

40.00000000

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int main(){
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<double> r(N);
  for (int i = 0; i < N; i++){
    cin >> r[i];
  }
  vector<double> c(N);
  c[0] = r[0];
  for (int i = 1; i < N; i++){
    double tv = 1000000000;
    double fv = max(r[i], c[i - 1]);
    for (int j = 0; j < 100; j++){
      double mid = (tv + fv) / 2;
      bool ok = true;
      for (int k = 0; k < i; k++){
        if (hypot(mid - c[k], r[i] - r[k]) < r[i]  + r[k]){
          ok = false;
        }
      }
      if (ok){
        tv = mid;
      } else {
        fv = mid;
      }
    }
    c[i] = tv;
  }
  double ans = 0;
  for (int i = 0; i < N; i++){
    ans = max(ans, c[i] + r[i]);
  }
  cout << ans << endl;
}