Fair Candy for Ratings — Solution & Editorial
Two sweeps: left to right enforce the rising-neighbour rule, then right to left take the max with the falling-neighbour rule. Sum the candies.
Complexity: O(N)
Watch out: A single left-to-right pass misses descents — the right-to-left pass with max() is essential.