Conditions for optimality in dynamic programming and for the limit of n-stage optimal policies to be optimal