856. The Most Books You Can Take
A shelf unit has n shelves in a row; shelf i holds b[i] books.
Choose a CONTIGUOUS range of shelves [l, r] and take k[i] books from each shelf i in that
range, where
0 <= k[i] <= b[i] and k[l] < k[l+1] < ... < k[r]
so the counts must strictly increase from left to right. Print the largest total number of books you can take.
8 5 2 7 9 -> 19
7 0 3 4 5 -> 12
1 1 1 1 1 -> 1
For the first row, take the range [0, 4] with counts 0, 1, 2, 7, 9, a total of 19.
The last row looks harsh and is worth checking: with every shelf holding one book, the counts must
strictly increase, so at best they read 0, 1, and one book is the answer.
## Fixing the right end makes the choice greedy
Suppose the range ends at shelf r. Then taking k[r] = b[r] is optimal, because raising the
last count never forces any earlier count down: the constraint only says earlier counts are
smaller. Working leftwards, the same argument makes each count as large as it may be:
k[r] = b[r]
k[i] = min( b[i], k[i+1] - 1 ) for i < r
and the range stops as soon as that would go below 0.
So define dp[i] as the best total for a range ENDING at shelf i, and the answer is the largest
dp[i]. Running the greedy from every i is O(n^2), which is far too slow; at n = 100000 on a row like
100000, 100000, ... every run walks the whole way back, which is 10^10 steps.
## The quantity that makes the stack work
While the cap does not bind, the counts fall by exactly one per step: from shelf i the count at
shelf t is b[i] - (i - t). The cap at shelf t binds exactly when
b[t] <= b[i] - (i - t) which rearranges to b[t] - t <= b[i] - i
So the whole problem turns on the derived array b[t] - t, and the first shelf to the left where
the cap binds is the PREVIOUS SMALLER OR EQUAL position in that derived array. Finding it is a
plain monotonic stack.
## The recurrence
Let j be the last index before i with b[j] - j <= b[i] - i.
if j exists:
m = i - j
dp[i] = dp[j] + m * b[i] - m * (m - 1) / 2
else:
m = min(i + 1, b[i] + 1)
dp[i] = m * b[i] - m * (m - 1) / 2
The term m * b[i] - m(m-1)/2 is the arithmetic series b[i] + (b[i]-1) + ... taken m times.
When j exists the series never reaches a negative term, because b[j] >= 0 together with
b[j] - j <= b[i] - i forces b[i] >= i - j. When j does not exist the series may run out of
books first, which is what the min clamps, and it is exactly the case 1 1 1 1 1 tests.
## The stack
Sweep left to right, popping while b[top] - top > b[i] - i, so the stack holds the derived
values in non-decreasing order from the bottom up. What remains on top is j. Each index is
pushed once and popped once, so the sweep is O(n).
## Size
The total reaches roughly n * max(b), which is 10^10, so dp must be 64-bit. The subterm
m(m-1)/2 alone reaches 5 * 10^9.
## Related problems
Problem 838 computes the same boundary on the raw array rather than on b[i] - i, and problem 858
carries a different value forward on the stack for a different recurrence.
## Sizes and widths
The total reaches roughly n * max(b), which is 10^10, and the subterm m(m-1)/2 alone reaches
5 * 10^9. Both are past a signed 32 bit integer, so dp and every intermediate product need 64
bits.
## Cost
Time O(n). Space O(n).
Constraints - `1 ≤ n ≤ 100000` - `0 ≤ b[i] ≤ 100000` - the answer can reach `10^10`, so it needs 64 bits
Input
The first line contains one integer n.
The second line contains n integers b[0] ... b[n-1], the number of books on each shelf.
Output
Print one integer alone on a line: the largest total number of books that can be taken under the rules. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.