problem in depicting the solution to the "aloknath and sanskar problem"

I tried to solve this problem based on dp but I didn’t get how to solve it. I referred to editorial but it is very unclear. Can anyone please tell me the logic or explain the editorial to solve this problem.

//