The statement of this problem is almost identical to the problem problem://problemsjutge.org:problems/upc/2011-semi/2-biblio-1.pbm, with two exceptions: Now, when filling the bookshelf, the relative order of the books in the input can be changed. And can be as large as .
I.e., the problem is: Given books, each one with width and height , use them to fill a bookshelf as much as possible. The second book (if any) must be shorter than the first book, the third book must be taller than the second book, …, and the last book must be taller than the penultimate book. Note that “short” and “tall” refer to the ’s, and that the goal is to maximize the sum of the ’s of the chosen books.
Input consists of several cases. Each case begins with , followed by pairs with and . Assume and . A special case with marks the end of input.
For every case, print the maximum possible sum of the widths of the chosen books.
Input
3 900000000 8 700000000 4 800000000 6 2 2 8 3 6 4 8 2 9 3 6 1 7 4 2 5 7 4 7 4 4 20 6 10 3 20 8 10 6 15 3 11 1 12 3 10 2 14 2 15 3 6 15 3 11 1 12 3 10 2 14 3 15 3 6 11 1 15 2 12 2 10 3 14 2 15 2 0
Output
2400000000 3 24 5 15 67 65 41