Dynamic Programming General Method Algorithm Example