A Dynamic Programming Formulation with Diverse Applications