Dynamic programming and the calculus of variations