The height of each building can be chosen independently. Thus, the answer is the sum of over all buildings .
For , update in this order. Then stores the maximum of the first elements of the original array. The answer is .
The time complexity is and the space complexity is .