This page is still under construction.

Parts of this page are still being built. What you see may change.

Oil

Interview

Time limit10sMemory limit512 MB

Summary
Given up to 2000 horizontal segments at distinct points, find the maximum total length of segments intersected by a single non-horizontal line from the origin.
Level

Medium7 of 10

Topics
Geometry, Sorting, Brute force, Binary search
Solved
No attempts yet

Problem

An oil company drills one well to bring oil up from deposits buried in the earth. A newly found deposit is rarely a single body. It usually splits into many parts that lie in layers.

The company simplifies the situation to a 2-dimensional model. Each deposit is a horizontal segment parallel to the surface, and the well is drilled from the surface along a straight line. The well takes oil from every deposit it touches on the way down, and touching a deposit at an endpoint of its segment still empties that whole deposit. The oil held by a deposit equals the width of the deposit, so the yield of a well is the sum of the widths of the deposits it touches.

The well goes downward from the surface, so it is never horizontal.

Find the largest amount of oil one well can extract.

Figure 1: oil layers buried in the earth. The picture matches the first example input.

Input

The first line contains an integer nn (1≤n≤20001 \le n \le 2000), the number of deposits. Each of the next nn lines contains three integers x0x_0, x1x_1, and yy describing one deposit: the deposit is the segment whose endpoints are (x0,y)(x_0, y) and (x1,y)(x_1, y), where yy is the depth below the surface. The values satisfy ∣x0∣,∣x1∣≤106|x_0|, |x_1| \le 10^6 and 1≤y≤1061 \le y \le 10^6. No two deposits share a point. Note that x0x_0 may be larger than x1x_1, and a deposit with x0=x1x_0 = x_1 has width 0.

Output

Print one integer, the maximum amount of oil that one well can extract.

Examples2

  1. Example 1

    Input
    5
    100 180 20
    30 60 30
    70 110 40
    10 40 50
    0 80 70
    
    Expected output
    200
    
  2. Example 2

    Input
    3
    50 60 10
    -42 -42 20
    25 0 10
    
    Expected output
    25