Pregunta de entrevista de Amazon

1-dimensional DP with additional optimizations