Problem 06C. Trapping Rain Water ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given n non-negative integers h0, h1, …, hn − 1 representing an elevation map, each bar having width 1. Compute how much water it can trap after raining.

Input format

The first line contains one integer n. The second line contains n space-separated integers h0… hn − 1.

Output format

Print one integer — the total trapped water.

Constraints

1 ≤ n ≤ 2 ⋅ 105

0 ≤ hi ≤ 106

Sample tests

No. Standard input Standard output
1
12
0 1 0 2 1 0 1 3 2 1 2 1
6
2
6
4 2 0 3 2 5
9
3
3
1 1 1
0

0.037s 0.012s 16