Need help in the question.

I want to solve the following question on the codeforces link , but i am unable to get the idea how to approach the soln. Also the tutorial given is not very good
So i need some help regarding the idea how to solve it.

Thanks.

//