Tidied, memoized version of the knapsack.