knapsack: memory efficient solution