Dynamic programming for hypergraphs