Amazon Interview Question

1-dimensional DP with additional optimizations