#C8815. Trapped Water Calculation

    ID: 52839 Type: Default 1000ms 256MiB

Trapped Water Calculation

Trapped Water Calculation

You are given an array of non-negative integers where each element represents the height of a building with unit width. Your task is to compute how much water can be trapped between the buildings after it rains.

More formally, let \( h_i \) be the height of the i-th building. The water trapped at the i-th index is given by: \[ \text{water}_i = \min(\max_{j \le i} h_j, \max_{j \ge i} h_j) - h_i \] provided that this value is positive, otherwise it is 0. The total trapped water is the sum of water at all indices.

For example, given the elevations: [0, 1, 0, 2, 1, 0], the trapped water is 1.

inputFormat

The input consists of a single line containing space‐separated integers representing the heights of the buildings.

For example: 0 1 0 2 1 0

outputFormat

Output a single integer which is the total amount of trapped water.

## sample
0 1 0 2 1 0
1