Amazon Interview Question

Solve this using dynamic programming