Problem 2297 · Medium
Two monotonic stacks track the rising and falling jumps; each index takes the cheapest legal source.
Open the interactive visualisation