Amazon interview question

Hard dynamic programming question, see leetcode most frequently asked