Question
The Monsoon Gorge Overflow

A geologist is surveying a canyon whose cross-section is described by n elevation readings. After heavy rain, water collects in the dips between the higher walls on either side. The amount of water trapped above any position is determined by the minimum of the highest wall to its left and the highest wall to its right, minus the ground elevation at that position.

Calculate the total units of water trapped across the entire canyon after the rain.

Input

The first line contains KaTeX can only parse string typed expression. The second line contains KaTeX can only parse string typed expression space-separated non-negative integers — the elevation map.

Output

A single integer — the total water trapped.

Example
Example 1:
Input
12

0 1 0 2 1 0 1 3 2 1 2 1
Output
6

Example 2:
Input
4

4 2 0 3
Output
4

Online