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 JAVA (judge verdict: Wrong Answer)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

public class Main {
	int N;
	int[] r;
	double[] x;
	public boolean check(double mid,int i){

		for(int j = i - 1;j >= 0;j--){
			double a = (mid - x[j]);
			double b = (r[i] - r[j]);
			if(r[i] + r[j] > Math.sqrt(a * a + b * b)){
				return false;
			}
		}
		return true;
	}

	public void solve() {
		N = nextInt();
		r = new int[N];
		for(int i = 0;i < N;i++){
			r[i] = nextInt();
		}

		x = new double[N];

		for(int i = 0;i < N;i++){

			double low = r[i] - 1e-8;
			double high = 1e8;

			while(high - low > 1e-8){
				double mid = (high + low) / 2;

				if(check(mid,i)){
					high = mid;
				}else{
					low = mid;
				}
			}
			x[i] = high;
		}
		double ans = 0;
		for(int i = 0;i < N;i++){
			ans = Math.max(ans,r[i] + x[i]);
		}
		out.println(String.format("%.08f",ans));
	}
	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}