#K85642. Trapping Rain Water

    ID: 36687 Type: Default 1000ms 256MiB

Trapping Rain Water

Trapping Rain Water

Given an array of non-negative integers representing an elevation map where the width of each bar is 1, compute how much water can be trapped after raining.

For each index i, the water that can be trapped on top of the block is computed using the formula:

\( water[i] = \max\Big( \min(\text{max_left}_i, \text{max_right}_i) - height[i],\ 0 \Big) \)

where max_lefti is the maximum height of a block to the left of index i and max_righti is the maximum height to the right of index i.

Your task is to compute the total amount of water that can be trapped.

inputFormat

The input is read from the standard input (stdin) and consists of two lines:

  1. The first line contains an integer n (0 ≤ n ≤ 105) representing the number of blocks.
  2. The second line contains n space-separated non-negative integers representing the elevation map.

outputFormat

Print a single integer to the standard output (stdout) representing the total amount of water that can be trapped.

## sample
0
0