eolymp
bolt
Try our new interface for solving problems
Problems

Falling Ice

Falling Ice

\includegraphics{https://static.e-olymp.com/content/06/06d6eb28eda96099a5e7089d698ae75f621c2608.jpg} Imagine disks of ice falling, one at a time, into a box, each ending up at the lowest point it can reach without overlapping or moving previous disks. Each disk then freezes into place, so it cannot be moved by later disks. Your job is to find the overall height of the final combination of disks. So that the answer is unique, assume that any disk reaching the bottom of the box rolls as far to the left as possible. Also the data is chosen so there will be a unique lowest position for any disk that does not reach the bottom. The data is also such that there are no "perfect fits": each disk that lands will be in contact with only two other points, on previous circles or the sides of the box. The illustrations above show white filled disks labeled with the order in which they fall into their boxes. The gray circle in the fourth illustration is not intended to be a disk that fell in. The gray disk is included to demonstrate a point: the gray disk is the same size as disk \textbf{2}, so there is \textit{space} for disk \textbf{2} on the very bottom of its box, but disk \textbf{2} cannot \textit{reach} that position by falling from the top. It gets caught on disk \textbf{1} and the side of the box. One way to find the top intersection point of two intersecting circles is as follows. Suppose circle \textbf{1} has center (\textbf{x_1},\textbf{y_1}) and radius \textbf{r_1}, and suppose circle \textbf{2} has center (\textbf{x_2}, \textbf{y_2}), and radius \textbf{r_2}. Also assume that circle \textbf{1} is to the left of circle \textbf{2}, i.e., \textbf{x_1} < \textbf{x_2}. Let \textbf{dx = x_2 - x_1},\textbf{dy = y_2 - y_1},\textbf{D = sqrt(dx*dx + dy*dy)},\textbf{E = (r_1*r_1 - r_2*r_2 + D*D)/(2*D)},\textbf{F = sqrt(r_1*r_1 - E*E)}; then the upper intersection point is (\textbf{x_1 + (E*dx - F*dy)/D}, \textbf{y_1 + (F*dx + E*dy)/D}). \InputFile The input consists of one or more data sets, followed by a line containing only \textbf{0} that signals the end of the input. Each data set is on a line by itself and contains a sequence of three or more blank-separated positive integers, in the format \textbf{w}, \textbf{n}, \textbf{d_1}, \textbf{d_2}, \textbf{d_3}, ..., \textbf{d_n}, where \textbf{w} is the width of the box, \textbf{n} is the number of disks, and the remaining numbers are the diameters of the disks, in the order in which they fall into the box. You can assume that \textbf{w} < \textbf{100}, that \textbf{n} < \textbf{10}, and that each diameter is less thanw. \OutputFile For each data set, output a single line containing the height of the pile of disks, rounded to two places beyond the decimal point. The example data matches the illustrations above.
Time limit 1 second
Memory limit 64 MiB
Input example #1
10 3 5 2 3 
8 2 5 5 
11 3 10 2 4 
9 3 4 4 6 
10 6 5 4 6 3 5 2 
0
Output example #1
5.00
9.00
12.99
9.58
14.19
Source ACM Mid-Central Regional Programming Contest 2006