TIME LIMIT ERROR OCCURED RAIN WATER HARVESTING

3 testcases show tle i exceeded time complexity please help in reducing it.
i am not able to solve it since begining

This is because you are doing it with O(n*n) complexity, with given constraints you should do it in O(n) or O(nlogn)

how please how should i remove complexity and what should i do in code


refer to this

got it using array for both left and right side and iterating over them