Amazon interview question

Solve this using dynamic programming