ADADET: In some cases the rightmost building is one less than the actual answer

I know my code is O(n^3) but still please tell me why in most cases the actual answer matches with mine and in some my answer is 1 less than the actual answer. i think there is a minor error in my code.

Link to my code:

Same Here. I Tried My Code on many different test-cases , all passing but on submission WA. Link to my Submission : https://www.codechef.com/viewsolution/14304164