Filling a bookshelf (2)

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 b can be as large as 10⁵.

I.e., the problem is: Given b books, each one with width w_(i) and
height h_(i), 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
h_(i)’s, and that the goal is to maximize the sum of the w_(i)’s of the
chosen books.

Input

Input consists of several cases. Each case begins with b, followed by b
pairs with w_(i) and h_(i). Assume 1 ≤ b ≤ 10⁵ and
1 ≤ w_(i), h_(i) ≤ 10⁹. A special case with b = 0 marks the end of
input.

Output

For every case, print the maximum possible sum of the widths of the
chosen books.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T10:26:49.501Z

© Jutge.org, 2006–2026.
https://jutge.org
