Dynamic programming solution for UVa714