Please someone help me for solving this problem.

codeforces

#1

problem link:: http://codeforces.com/contest/986/problem/C

I tried and i got TLE on testcase 15.
This is my solution.
link:: http://codeforces.com/contest/986/submission/39397854


#2

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