Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem B
Squeeze the Cylinders

Laid on the flat ground in the stockyard are a number of heavy metal cylinders with (possibly) different diameters but with the same length. Their ends are aligned and their axes are oriented to exactly the same direction.

We'd like to minimize the area occupied. The cylinders are too heavy to lift up, although rolling them is not too difficult. So, we decided to push the cylinders with two high walls from both sides.

Your task is to compute the minimum possible distance between the two walls when cylinders are squeezed as much as possible. Cylinders and walls may touch one another. They cannot be lifted up from the ground, and thus their order cannot be altered.

Figure B.1. Cylinders between two walls

Input

The input consists of a single test case. The first line has an integer $N$ $(1 \leq N \leq 500)$, which is the number of cylinders. The second line has $N$ positive integers at most 10,000. They are the radii of cylinders from one side to the other.

Output

Print the distance between the two walls when they fully squeeze up the cylinders. The number should not contain an error greater than 0.0001.

Sample Input 1

2
10 10

Sample Output 1

40.00000000

Sample Input 2

2
4 12

Sample Output 2

29.85640646

Sample Input 3

5
1 10 1 10 1

Sample Output 3

40.00000000

Sample Input 4

3
1 1 1

Sample Output 4

6.00000000

Sample Input 5

2
5000 10000

Sample Output 5

29142.13562373

The following figures correspond to the Sample 1, 2, and 3.

Solution in C++ (judge verdict: Accepted)

#include <algorithm>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <climits>

#define all(c) (c).begin(), (c).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF=100000000;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
using namespace std;
typedef pair<int ,int > P;
typedef long long ll;
struct Circle {
    double x,y,r;
    Circle(double x=0,double y=0,double r=0):
        x(x),y(y),r(r){}
};

// c1, c2??????????????????
bool ok(const Circle &c1, const Circle &c2) {
    double d = sqrt((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y));
    return d > c1.r+c2.r && c1.x < c2.x;
}
double dist(Circle c1, Circle c2) {
    double x=c1.x-c2.x;
    double y=c1.y-c2.y;
    return sqrt(x*x+y*y);
}

int n;
int r[502];
Circle cs[502];

bool ok(int m) {
    if(cs[m].x-cs[m].r<0) return false;
    rep(i,m) {
        //cout<<"comp: "<<i<<", "<<m<<endl;
        if(!ok(cs[i],cs[m])) return false;
    }

    return true;
}

void func(int m) {
    double lb=0,ub=INF;
    rep(i,100) {
        double mid=(lb+ub)/2;
        cs[m].x=mid;
        if(ok(m)) ub=mid;
        else lb=mid;
    }
}

int main() {
    cin>>n;
    rep(i,n) cin>>r[i];
    rep(i,n) cs[i]=Circle(0,r[i],r[i]);
    rep(i,n) func(i);
    double ans=0;
    rep(i,n) ans=max(ans,cs[i].x+cs[i].r);
    printf("%.8f\n",ans);
    return 0;
}