#C250. Rainwater Trapping
Rainwater Trapping
Rainwater Trapping
You are given a list of non-negative integers representing the heights of buildings in a row. When it rains, water is trapped between the buildings. Your task is to compute the total amount of rainwater that is trapped.
The amount of water trapped at any building is determined by the formula:
$$w_i = \min(\text{leftMax}_i,\,\text{rightMax}_i) - h_i $$where \(h_i\) is the height of the i-th building, \(\text{leftMax}_i\) is the maximum height to the left of the i-th building (including itself) and \(\text{rightMax}_i\) is the maximum height to the right of the i-th building (including itself). If the number of buildings is less than 3, or any building has a negative height, the program should output an error message.
Note: The error message for a negative height at index i should be exactly "Height at index i is negative!" and for insufficient buildings, it should be "Insufficient number of buildings".
inputFormat
The first line contains an integer \(n\) representing the number of buildings. The second line contains \(n\) space-separated integers where each integer represents the height of a building.
Note: The input is provided via standard input (stdin).
outputFormat
If the input is valid, output a single integer representing the total amount of rainwater trapped. If an error occurs due to a negative height or insufficient number of buildings, output the corresponding error message.
Note: The output should be written to standard output (stdout).
## sample12
0 1 0 2 1 0 1 3 2 1 2 1
6