Here is my version of the knapsack problem with the added constraint of number of items (written in python). https://github.com/slek120/AutoDeck
In the normal knapsack problem, you make sets that give the highest value for a total cost starting from zero and incrementing until max cost is reached. The next set is the better of the previous set or the set with total cost - cost of item with the item appended. Since there is an item limit, instead of just appending the item, items have to be replaced. So instead, the set with total cost - cost of item + cost of replaced item is used.
I also created a greedy algorithm that works much faster but doesn't give the best answer. In this case, you fill the knapsack with the most cost efficient items until the item limit is reached. Then replace the item with the next most cost efficient item that gives the most reward. Continue until max cost is reached.