#K63112. Container Water Accumulation
Container Water Accumulation
Container Water Accumulation
Given an array of building heights \(H = [h_0, h_1, \ldots, h_{n-1}]\), determine the total amount of water that can be collected between these buildings. For each building at index \(i\), the water accumulated above it is given by:
\(w_i = \max(0, L_i - h_i)\)
where \(L_i = \max_{0 \leq j < i} h_j\) is the maximum height among the buildings to its left. Note that the first building never accumulates any water.
Your task is to compute the sum \(\sum_{i=1}^{n-1} w_i\) which represents the total water collected.
inputFormat
The first line contains an integer n
, the number of buildings.
The second line contains n
space-separated integers representing the heights of the buildings.
outputFormat
Output a single integer representing the total amount of water collected.
## sample6
3 1 2 4 2 3
6