Bookshelf
InterviewTime limit1sMemory limit128 MB
Partition the books, in order, into shelves of total width at most L to minimize the sum of each shelf's max height.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Segment tree, Stack
- Solved
- No attempts yet
Problem
Farmer John has collected books () and wants to build a set of bookshelves to hold them all.
Each book has a width and a height . The books must be placed on the shelves in order: the first shelf holds books for some , the second shelf starts with book , and so on. Each shelf can hold a total width of at most ().
The height of a shelf equals the height of the tallest book on it, and the total height of the bookshelf set is the sum of the heights of all shelves (they are stacked vertically).
Compute the minimum possible total height of the bookshelf set.
Input
The first line contains two space-separated integers and .
Each of the next lines contains two space-separated integers and , the height and width of book (, ).
Output
Print a single integer: the minimum possible total height of the bookshelf set.
Explanation
In the first example there are books and each shelf may hold a total width of at most . One optimal arrangement uses shelves: the first holds only book (height , width ), the second holds books (heights , so the shelf height is and the total width is ), and the third holds book (height , width ). The total height is .