#C14030. Trapping Rain Water

    ID: 43635 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 it can trap after raining. The water trapped at each position i can be calculated using the formula:

\( \text{water}_i = \min(\text{left\_max}_i, \text{right\_max}_i) - \text{height}_i \)

where:

  • \( \text{left\_max}_i \) is the maximum height from index 0 to \( i \) (inclusive).
  • \( \text{right\_max}_i \) is the maximum height from index \( i \) to \( n-1 \) (inclusive).

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

inputFormat

The input consists of a single line containing non-negative integers separated by spaces. These integers represent the elevation map.

outputFormat

Output a single integer: the total amount of trapped water.

## sample
0 1 0 2 1 0 1 3 2 1 2 1
6