The general method of dynamic programming