IIT Academy interview question

Dynamic programming related question with optimising it further