There Are n Chairs
InterviewTime limit1sMemory limit1024 MB
Pick chairs whose total width reaches H so that the largest height gap between neighbors in the row is as small as possible.
- Level
Medium5 of 10
- Topics
- Sorting, Binary search, Greedy
- Solved
- No attempts yet
Problem
Vlad has finally become team lead, but he has no time to get home and will have to sleep at the office. Not every IT company can afford a roomy coworking space with a place to nap, so Vlad will sleep on office chairs.
The office has chairs. The -th chair has height and width . Vlad picks any set of chairs and lines them up in a row to lie on. Vlad's height is , so for him to lie down comfortably, the total width of the chosen chairs must be at least :
Sleeping on chairs of different heights is uncomfortable. The discomfort of a row is the largest height difference between two adjacent chairs in it:
A row with one chair has discomfort . Help Vlad choose chairs so that he can lie down and the discomfort is as small as possible.
Input
The first line contains two integers and , the number of chairs and Vlad's height (; ). The second line contains integers , the chair heights (). The third line contains integers , the chair widths (). It is guaranteed that does not exceed the sum of all .
Output
Print one integer: the minimum possible discomfort among all valid sets.
Hints
In the first sample, place chairs 2 and 4 in either order. In the second sample, the sets [1, 5] and [2, 4, 3] both work. Order matters: the row [2, 3, 4] has discomfort , which is more than the row [2, 4, 3] gives.