Skip to content
UPEEC

Trapping Rain Water

Hard#arrays#two-pointers#stack· solved by 0 · time limit 2.0 s

Bars of width 1 have heights h1…hnh_1 \dots h_n. After rain, how much water is trapped between them?

Input

nn, then nn heights.

Output

Units of trapped water.

Constraints

1≤n≤2×1051 \le n \le 2 \times 10^5, 0≤hi≤1050 \le h_i \le 10^5

Sample 1

Input

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

Output

6

Sample 2

Input

6
4 2 0 3 2 5

Output

9

Sign in or join free to run and submit your solution.