Gopher is embarking on yet another adventurous tour, this time across cities, each defined by its two-dimensional coordinates and having Power. Gopher will visit all the cities but can only visit each city once. He will start from the city with the lowest Power and return to the starting city after visiting all the cities.
But, as with any journey, there are rules to abide by:
Before reaching the city with the highest Power, Gopher can go from city to city only if the Power of city is lower than that of city .
Once he reaches the city with the highest Power, Gopher's path can go from city to city if the Power of city is higher than that of city .
The cost of moving from city to city is calculated using the formula , where represents the coordinates of city and represents the coordinates of city .
Gopher could complete his tour through various routes, he wants to accomplish it with the minimum cost. Thus, he turns to you for assistance in this.
The first line will contain one integer denoting the number of cities Gopher will visit in his tour.
Next lines will contain three integers , , denoting the Power of city and denoting the coordinator of city.
Print a single line containing the minimum cost to visit all the cities in the Gophers tour. Errors less than will be ignored.
Input | Output |
---|---|
4 1 2 3 2 6 3 3 3 7 4 5 2 | 14.9225 |
The minimum cost path is (2,3)->(6,3)->(5,2)->(3,7)->(2,3). |