Rainwater in the Ruins
Walls of the given heights stand in a row (width 1 each). After rain, how much water is trapped between them?
Input: the first line has N; the second line has N non-negative heights.
Output: the total trapped water.
Constraints:
- 1 ≤ N ≤ 105
- 0 ≤ hi ≤ 109
Sample Tests
Input 1
12 0 1 0 2 1 0 1 3 2 1 2 1
Output 1
6
Input 2
2 5 5
Output 2
0
Discussion (opens after you solve — no spoilers before)
No comments yet.