Please someone help me for solving this problem.

problem link::

I tried and i got TLE on testcase 15.
This is my solution.

Your solution seems like naive way of doing that question. The complxity is too high for constraints given in question.
Take a look at editorial or this solution link text