https://www.codechef.com/ICHN2019/problems/ICHN03

I encountered with this problem and not able to build the logic so any one can help…