IntermediateRating 1300
Water on Flat Roofs
Each bar of the histogram is width 1. After rain, how much water stays on top of the bars? Solve it in one pass using two pointers.
Input
n, then n heights.
Output
One integer.
Sample 1
Input
6 4 2 0 3 2 5
Output
9
Sample 2
Input
3 1 2 3
Output
0
Output appears here