how to solve KMAX of LOC AUG 2017

can someone tell me the approach of the problem KMAX of LOC Aug 2017 . Here is https://www.codechef.com/LOCAUG17/problems/KMAX link of the problem.
Thanks in advance.