Amazon interview question

1-dimensional DP with additional optimizations