hint:

Solve problem backwards from the end to the beginning.