Added caching for cost and problems.