Amazon Interview Question

Hard dynamic programming question, see leetcode most frequently asked