Dynamic programming with functional