Help Needed (In Past Contest). What's wrong in my solution?

Hello Everyone!
Here is My Solution to the problem Problem Statement I have checked it so many times. But I am not able to find where the problem is. Can anyone suggest to me where my solution fails?
A little help will be appreciated. Thank you in advance.

Refer to the editorial.

Thanks for the editorial. But can you find the problem in my solution? I am expecting where the solution fails. Is it not the optimal one?