https://www.codechef.com/ENJU2020/problems/ECJN209

Can someone explain what will be the approach… I have understood the question but how to solve it?