Google interview question

An improvement using dynamic programming was required for the last question.