refactor knapsack