Amazon interview question

one hard question from Dynamic programming