Bookcase
Assignment
Sally and Mark plan to get a new bookcase for their books. They are debating about its design. The bookcase will consist of several shelves of the same length. Mark thinks it is a good idea to set heights between shelves variably so that the placed books just fit in. If the books are put onto shelves by their heights, this might result in a bookcase of smaller height. However, Sally does not agree. She wants to keep the books alphabetically ordered by the author and title. Mark is not happy with this requirement, but he must accept it. After some thinking he believes he can find an optimal solution even for the alphabetical ordering. He realizes that some books arrangements that do not necessary fill each of the shelves to the end might give better results. Sally is not thrilled even by this proposal. She worries there might be large, ugly looking gaps at the shelves ends. But Mark assures her that he can take gaps sizes into account as well and he can come up with a satisfactory solution.
The task
Given a shelf length L and a sequence of books Bi, i=1,..,N, where each Bi is of height Hi and width Wi. Let an arrangement of the books on k shelves be represented by a partition of the set \{1,..,N} into sets S1, S2, .., Sk where Si represents indices of books placed on the i-th shelf. Note that, for each i=1,..,k, it must hold |Si| ≥ 1 and ∑j∈Si~~ Wj ≤ L, and, for each 1 ≤ i < j ≤ k, every element of Si is less than any element of Sj. Also note that there is no explicit restriction on the number of shelves k and the shelves depth is large enough to store any of the books.
Let us define cost(S1,..,Sk)= ∑i=1,..,k maxj∈Si~~ Hj. This arrangement cost is proportional to the bookcase height, except for shelves thickness which is neglected.
Our task is to find an arrangement of the minimum cost. If there are more such optimal arrangements S1,..,Sk, we search among them for that one which minimizes maxGap(S1,..,Sk)=maxi=1,..,k (L-∑j∈Si~~ Wj). i.e., we minimize the maximum gap at the shelves ends.
To demonstrate efficiency of the optimal solution, we would also like to compare it with a solution obtained by the greedy algorithm. This algorithm creates an arrangement by taking a book by book and placing it onto the current shelf whenever it fits there. If it does not fit, the next shelf becomes the current one and the book is placed there.
Input
The first input line contains two integers, N and L, separated by space. The first integer is the number of books, the second integer is the shelf length. N lines representing books in their alphabetical order follow. An i-th line contains integers Hi and Wi, separated by space.
It holds 1 ≤ N ≤ 6 × 105, 1 ≤ L ≤ 3 × 104. For each i, it holds 1 ≤ Hi ≤ 135, 1 ≤ Wi ≤ 55, and Wi ≤ L.
Public data
The public data set is intended for easier debugging and approximate program correctness checking. The public data set is stored also in the upload system and each time a student submits a solution it is run on the public dataset and the program output to stdout and stderr is available to him/her.