knapsack function using memorization