LogiQuest

40. Trapping Rain Water

HardTwo PointersArray

Given bar heights of width 1, compute how many units of rain water are trapped between the bars.

Input format

Line 1: n Line 2: n non-negative heights

Output format

The trapped water.

Example 1

Input

12
0 1 0 2 1 0 1 3 2 1 2 1

Output

6

Example 2

Input

6
4 2 0 3 2 5

Output

9

Constraints

1 ≤ n ≤ 2 × 10⁴

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…