Limits 1s, 512 MB

Gopher is embarking on yet another adventurous tour, this time across NN cities, each uniquelyuniquely defined by its two-dimensional coordinates and having distinctdistinct 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:

  1. Before reaching the city with the highest Power, Gopher can go from city ii to city jj only if the Power of city ii is lower than that of city jj (Poweri<Powerj)(Power_i < Power_j).

  2. Once he reaches the city with the highest Power, Gopher's path can go from city ii to city jj if the Power of city ii is higher than that of city jj (Poweri>Powerj)(Power_i > Power_j).

The cost of moving from city ii to city jj is calculated using the formula (xixj)2+(yiyj)2\sqrt{(x_i - x_j)^2 + (y_i - y_j)^2}, where (xi,yi)(x_i, y_i) represents the coordinates of city ii and (xj,yj)(x_j, y_j) represents the coordinates of city jj.

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.

Input

The first line will contain one integer N(2N3×103)N(2\leq N \leq 3\times{10}^3) denoting the number of cities Gopher will visit in his tour.

Next NN lines will contain three integers Poweri(1PoweriN)Power_i(1 \leq Power_i\leq N), xi,yi(1xi,yi106)x_i,y_i(1 \leq x_i,y_i \leq {10}^6), PoweriPower_i denoting the Power of ithi_{th} city and xi,yix_i,y_i denoting the coordinator of ithi_{th} city.

Output

Print a single line containing the minimum cost to visit all the cities in the Gophers tour. Errors less than 10410^{-4} will be ignored.

Sample

InputOutput
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).


Submit

Login to submit.

Statistics

86% Solution Ratio
monna4335Earliest, 2w ago
NadiimFastest, 0.1s
NadiimLightest, 5.2 MB
YouKnowWhoShortest, 963B
Toph uses cookies. By continuing you agree to our Cookie Policy.