Under construction

Still being built. Things may change

Simple Polygon

Time limit2sMemory limit512 MB

Summary
Given n segments with one endpoint on the x-axis, decide whether they can all lie on the boundary of one simple polygon, and find the minimum boundary length if so.
Level

Hard9 of 10

Topics
Geometry, Dynamic programming, Sorting
Solved
No attempts yet

Problem

Let SS be a set of nn line segments. One endpoint of each segment lies on the x-axis, and the other lies above the x-axis. All endpoints are distinct, but segments may intersect at their interiors.

We want to draw a simple polygon PP such that every segment of SS is part of the boundary of PP. That is, each segment is either an edge of PP or part of an edge. A polygon PP is simple if the angle at each vertex is not 180∘180^\circ, and no two edges meet except when two adjacent edges meet at a shared vertex.

Figure H.1 shows segment sets for which simple polygons can be drawn. The black solid segments belong to SS. Adding the red dotted segments gives a simple polygon. The left figure has every endpoint of SS as a vertex of PP. The right figure has a simple polygon with 6 vertices, where two endpoints each lie in the interior of an edge of PP. An endpoint of SS does not have to be a vertex of PP.

Figure H.1: Examples of line segments for which simple polygons can be drawn.

Figure H.2 shows segment sets for which no simple polygon can be drawn.

Figure H.2: Examples of line segments for which no simple polygon can be drawn.

If more than one simple polygon exists for a set, we want the one with the minimum boundary length. Figure H.3 shows a simple polygon whose boundary is shorter than that of any other simple polygon for its set.

Figure H.3: A simple polygon with the minimum boundary length.

Given a set of line segments, determine whether a simple polygon exists for the set. If it does, find a simple polygon with the minimum boundary length.

Input

The first line contains an integer nn (3≤n≤20,0003 \le n \le 20{,}000), the number of segments. Each of the next nn lines contains three integers x1x_1, x2x_2, and y2y_2 (1≤x1,x2,y2≤1061 \le x_1, x_2, y_2 \le 10^6). These describe the segment with endpoints (x1,0)(x_1, 0) and (x2,y2)(x_2, y_2). All endpoints are distinct, so no two endpoints occupy the same position.

Output

Print exactly one line. If a simple polygon exists, print the minimum boundary length rounded to one decimal place. Otherwise, print −1-1.

Examples3

  1. Example 1

    Input
    4
    1 1 2
    2 2 1
    3 3 1
    4 4 2
    
    Expected output
    12.0
    
  2. Example 2

    Input
    4
    1 1 2
    2 3 2
    5 4 1
    7 1 4
    
    Expected output
    19.3
    
  3. Example 3

    Input
    4
    5 6 6
    4 3 2
    1 3 4
    9 7 3
    
    Expected output
    -1