Heritage
Time limit0.3sMemory limit64 MB
Divide a region under a polygonal line into parcels whose areas match given ratios, choosing vertical cuts that minimize the total fence length.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Geometry, Binary search, Prefix sum
- Solved
- No attempts yet
Problem
The old Count D owns a piece of land that he wants to divide among his sons.
The land lies above the axis. Its lower boundary is the horizontal segment on the axis, its left and right boundaries are the vertical segments and , and its upper boundary is the polygonal line , which lies entirely above the axis.
The Count builds vertical fences. Each fence joins the base segment to the polygonal line, so a fence placed at coordinate has length equal to the height of the polygonal line at . The fences split the land into parcels, ordered from left to right.
The parcelling must satisfy both of the following conditions:
- The parcels can be assigned to the sons so that each son's parcel has an area directly proportional to his age.
- Among all parcellings that satisfy condition 1, the total length of the fences is as small as possible.
Given the points and the ages of the sons, compute the minimum possible total fence length.
Input
The first line contains two integers and .
The second line contains integers — the ages of the sons.
Each of the next lines contains two integers and — the coordinates of point .
Output
Print a single real number: the minimum possible total length of the fences, rounded to exactly 6 decimal places.
Constraints
- Fence widths are negligible.
- Use double-precision floating point for the computation.
Hint
In the sample (the first example, ) only one fence is needed.

Placing the fence at , where the polygonal line has height , gives a total fence length of . The son aged takes the left parcel (area ) and the son aged takes the right parcel (area ); both areas are proportional to the ages.

If the fence were placed instead at (length ), the son aged would take the left parcel and the son aged the right one. That split still satisfies the area-proportionality condition, but its fence is longer, so it is not optimal. Every other position fails the area-proportionality condition.