Problems > Rainwater in the Ruins

Rainwater in the Ruins

Difficulty: 1700 3000 ms 128 MB

two-pointers

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

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.