The Great Wall
Time limit3sMemory limit512 MB
Each design picks two length-r intervals whose overlap height adds extra cost; find the k-th smallest total wall height over all interval pairs.
- Level
Hard8 of 10
- Topics
- Binary search, Prefix sum, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
You have become the emperor of Sinai, a small country. To stop the raids coming across the border you decided to build a great wall there, and you hired W Corp, the only company in the world that builds walls nobody can break through.
W Corp builds every wall from the same pattern. The wall is meters long, and each one meter piece is numbered from to along its length. Pieces may have different heights. The heights come from three fixed arrays , , of elements each, with for every , and from an integer (). The three arrays and are the same for every wall W Corp builds.
A specific design is fixed by two integers and with . Take the two ranges and , both inclusive. The height of piece is:
- if lies in neither range,
- if lies in exactly one of the ranges,
- if lies in both ranges.
The strength of a wall is the sum of the heights of its pieces.
Because , , and never change, W Corp hands you a price list of every possible design sorted in non-decreasing order of strength. You pick the -th design on that list. Report the strength of the wall you picked.
Input
The first line contains three integers , , (, , ): the length of the wall, the length of the chosen ranges, and the position on the price list.
The second line contains the elements of the array ().
The third line contains the elements of the array ().
The fourth line contains the elements of the array ().
Output
Print one integer, the strength of the -th wall on the price list. Several designs can share the same strength, and the -th strength value is the same whichever way those ties are ordered.
Hint
In the first example there are three walls you can build.
- Choosing and gives the heights and the strength .
- Choosing and gives the heights and the strength .
- Choosing and gives the heights and the strength .