Help me with this Dynamic Programming Question kind of stuck on this

If anyone has any solution or approach for the 2nd question, please message me privately.
I have completed the test and tried all the approaches. But nothing seems to work.