Need help in solving this problem on codechef

here is the problem link
https://www.codechef.com/CDX2018/problems/CODX02, can someone please tell me the approach to solve this problem and intuition behind that approach.