Dynamic programming: explain with examples