Under construction

Still being built. Things may change

Segment Drawing

Time limit5sMemory limit2048 MB

Summary
Each point is paired with a segment on the x-axis. Connect every point to its segment without strict crossings, minimizing total length, or report -1.
Level

Medium7 of 10

Topics
Geometry, Greedy, Sorting
Solved
No attempts yet

Problem

You are given some points, each paired with a line segment. All points lie strictly above the x-axis, and each line segment lies on the x-axis. No two line segments share a point.

For each point and segment pair, draw one new segment that connects the point to its segment. The pairing is fixed, so you cannot change which point connects to which segment.

No two drawn segments may strictly intersect. Two drawn segments may touch at a shared endpoint.

Find the minimum total length of all drawn segments, or determine that no valid drawing exists.

Input

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5), the number of point and segment pairs.

Each of the next nn lines contains four integers xx, yy, ll, rr (−106≤x≤106-10^6 \le x \le 10^6, 0<y≤1060 < y \le 10^6, −106≤l≤r≤106-10^6 \le l \le r \le 10^6). The line describes a point at (x,y)(x, y) and its segment with endpoints (l,0)(l, 0) and (r,0)(r, 0).

The segments are given from left to right. No two segments share a common point. Two points may be at the same location.

Output

Output a single real number, the minimum total length of all drawn segments. An answer is accepted if its absolute or relative error is at most 10−610^{-6}. If the segments cannot be drawn without a strict intersection, output −1-1.

Examples3

  1. Example 1

    Input
    3
    0 6 -4 -2
    -2 1 -1 0
    0 4 1 4
    
    Expected output
    11.9995169566
    
  2. Example 2

    Input
    3
    0 6 -4 -2
    0 4 -1 0
    -2 1 1 4
    
    Expected output
    -1
    
  3. Example 3

    Input
    2
    0 5 -1000000 -1
    0 5 1 1000000
    
    Expected output
    10.1980390272