Forest

Time limit1sMemory limit128 MB

Summary
Given non-overlapping circles, find the farthest one visible from the origin along some segment that touches no other circle, and report its closest-point distance.
Level

Medium7 of 10

Topics
Geometry, Sorting, Implementation, Math
Solved
No attempts yet

Problem

Bruce Force is standing in the forest. He wonders which tree trunk is the farthest away that is not blocked from his view by other tree trunks.

Bruce has drawn a map of the trees in the forest. On the map his current position is the origin of a Cartesian coordinate system. Tree ii is drawn as a circle with center (xi,yi)(x_i, y_i) and radius rir_i. A tree trunk is considered visible if and only if there is a line segment on the map from the origin (0,0)(0, 0) to some point on the border of that tree's circle such that the segment does not intersect or touch any other circle.

Input

The input contains several test cases. The first line of each test case contains an integer nn (1≤n≤10001 \le n \le 1000), the number of trees on the map. Each of the following nn lines contains three integers xix_i, yiy_i, rir_i (−10000≤xi,yi≤10000-10000 \le x_i, y_i \le 10000, 1≤ri≤10001 \le r_i \le 1000), where (xi,yi)(x_i, y_i) is the center of the circle representing tree trunk ii and rir_i is its radius.

No two circles intersect: for any two circles, the distance between their centers is strictly greater than the sum of their radii. No circle contains the origin.

The last test case is followed by a line containing a single 00.

Output

For each test case, print one line with the maximum Euclidean distance from the origin to a visible tree. The distance to a tree is measured to the point of that tree closest to the origin (that is, xi2+yi2−ri\sqrt{x_i^2 + y_i^2} - r_i), regardless of whether that particular point is itself visible.

Round the answer to exactly 3 digits after the decimal point.

Examples2

  1. Example 1

    Input
    3
    10 10 11
    1 1 1
    -20 -10 20
    5
    1 2 2
    -2 1 1
    2 -1 1
    -1 -2 2
    10000 -10000 1000
    0
    
    Expected output
    3.142
    1.236
    
  2. Example 2

    Input
    1
    5 0 1
    0
    
    Expected output
    4.000