Dynamic Programming and the Calculus of Variations