Added caching for small instances of the problem.

Reduced run time to 60 ms on my machine.