Need help to solve problem

https://www.codechef.com/INOIPRAC/problems/TINOI17B

How to approach these type of problems?