Dynamic Programming in Datalog with Aggregates