spoj#ENVIRON. Environmental Engineering

Environmental Engineering

One of the major challenges for environmental engineers in the future will be the overall scarcity of potable water. Not only vegetation and wildlife are fundamentally dependent upon adequate freshwater resources, but also humans. Several desalination processes were developed to make sea water drinkable.

Nonetheless, the water distribution is not uniform on planet, and so some regions are lacking water while others possess it on excess. The International Committee for Precious Consumables (ICPC) worked out an ambitious project fostering the uniform distribution of water on Earth (a perfect ball). The richest and poorest regions in terms of water resources were found to lie equidistantly distributed along a circle of maximum length around the Earth (i.e. the centre of the Earth is also the centre of this circle). So the idea came to construct gigantic pipelines that can transport water along this circle.

 

 

Each pipeline has two terminals, starting at a location with water abundance and ending in a region with water scarcity. As drilling the Earth on so long distances is not a reasonable option, all pipelines will be above Earth. Further are there dedicated ducts for the pipelines at different heights. That is, a pipeline grows vertically into the sky until one of the allowed heights is reached, makes a 90° angle, and follows the duct at constant height until it is above the depletion region, where it forms another 90° angle to descent vertically to ground.

As all pipelines’ projection to ground must follow the same circle, the ICPC faces a serious problem. They must make sure that no pipelines cross each other in this 2D plane! Further they are concerned about the total length of the pipes, which they’d like to minimize. Your task is to calculate the minimal total length of all the pipes, knowing that the allowed duct heights are non-zero integer multiples of the distance between two adjacent locations (measured along the circle on surface level), which for our purpose has a value of 1.

 

INPUT

The input consists of several test-cases separated by an empty line. Each test-case starts with the number of locations N (0<=N<=500), followed by a line containing N numbers (‘0’ or ‘1’) and describing the locations along the circle. A ‘0’ stands for a location with water depletion, a ‘1’ for a region with water abundance. You may safely assume that there are as many ‘0’ as ‘1’. Input terminates on a test-case with N=0, which must not be evaluated.

 

OUTPUT

For each test-case, output the minimum total length (to a precision of 10-2) of the pipes so that each abundance region connects to exactly one depletion region.

 

SAMPLE INPUT

4

1 0 0 1

 

8

1 1 1 1 0 0 0 0

 

0

 

SAMPLE OUTPUT

 

9.14

31.00

 

          

Sample input 1                   Sample input 2                   A larger test-case