Limited Library
Time limit2sMemory limit2048 MB
Given shelf heights and book heights, place all books on shelves that hold x books each, and find the most shelves that can also hold an art piece taking x-y slots.
- Level
Medium6 of 10
- Topics
- Greedy, Binary search, Sorting
- Solved
- No attempts yet
Problem
During the summer break, fewer students are on campus, so this is a good time to add new books to the TU Delft library. The new books all have the same width, but their heights vary. Because all existing bookcases are full, the library management board has decided to add a new bookcase to display these books.
The new bookcase has several shelves of varying heights. Each shelf can fit books. Because there may be leftover space, the board also wants to display some art pieces in this bookcase, at most one per shelf. An art piece fits on a shelf only if at most books sit next to it, since an art piece takes up the same space as books.

Figure L.1: The hatched areas mark three shelves that have room for an art piece while still holding all the new books.
The management board wants you to find the largest number of shelves on which you can place an art piece while all the new books still fit on the shelves.
Input
The input consists of:
- One line with four integers , , , and (, ), the number of shelves, the number of books, the number of books that fit on a full shelf, and the number of books that fit on a shelf next to an art piece.
- One line with integers (), the heights of the shelves.
- One line with integers (), the heights of the books.
Output
If it is possible to fit all the books into the shelves, output the largest number of art pieces you can place. Otherwise, output impossible.