SDE Path

Trapping Rain Water

Hard

Trapping Rain Water

Given an elevation map, compute how much water it can trap after raining.

Input format

The first line contains N. The second line contains N non-negative heights.

Output format

Print the total units of water trapped.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
12
0 1 0 2 1 0 1 3 2 1 2 1
Expected output
6
Example 2
Input
6
4 2 0 3 2 5
Expected output
9