Help needed in a problem

Problem Link

Can someone help how to do this problem. The editorial is a bit unclear to me. I am finding it difficult to make transitions between the dp states. Thanks in advance!
@galencolin @everule1 @samarth2017 @ssrivastava990