a classic dp problem