Amazon Interview Question

one hard question from Dynamic programming