DUCS CUP Problem 6

Hi there Coders

I recently appeared for DUCS cup challenge
https://www.codechef.com/DCC2020

I was able to solve 5 questions

However i wasn’t able to understand the 6th problem

I have tried seeing others solution under submission

Seems like most have used Dynammic Programing to get the solution

However i would like to understand how the solution works or an understanding like editorial

I know it may me much to ask; but if anyone is willing to share the details please do so

NOTE: EVEN IF YOU WRITE THE SOLUTION IN A FEW WORDS WITHOUT TOO MUCH GOING INTO DETAILS ; IT WOULD WORK

Thanks in Advance