How to solve CHGORAM in August19 challenge?

Editorial has not been posted yet.I don’t have any clue except brute force .Can some one explain the solution to the problem.
Thanks
Problem link - https://www.codechef.com/AUG19A/problems/CHGORAM/

There are @vijju123’s Setter’s Notes here:

and also my fully-documented solution with high-level overview and lots of comments :slight_smile: :

https://www.codechef.com/viewsolution/25925347

3 Likes