Please someone help me for solving this problem.

problem link:: Problem - C - Codeforces

I tried and i got TLE on testcase 15.
This is my solution.
link:: Submission #39397854 - Codeforces

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