0

I have an array of n Objects with an int 'value' and another int 'cost'. I want to get the subset of size k (k < n) of that array that maximizes the sum of the values. For instance...

Value - Cost

32 - 24

25 - 17

39 - 40

10 - 10

47 - 44

0 - 10

18 - 10

I need to choose, say, 5 of those that will maximize value while staying below a certain total cost (say, 100). I don't get bonus points for having the lowest cost, only for having the highest value. I'm not looking to get the most bang for my buck, I'm looking to get the most bang, while staying below a given buck.

Is there just a name for the algorithm I'm looking for? I don't want to have to brute force it.

  • I believe that would be an instance of the 0-1 [knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem#Definition). Cost here would be equivalent to weight in the classic formulation. – גלעד ברקן Dec 30 '19 at 03:52
  • 1
    Something like [https://stackoverflow.com/questions/26445874/knapsack-0-1-with-fixed-quanitity](https://stackoverflow.com/questions/26445874/knapsack-0-1-with-fixed-quanitity) – גלעד ברקן Dec 30 '19 at 03:54
  • Wow that's exactly what I'm looking for! Thanks! Now I just need to turn that into C#. Thanks for all your help! – Jordan Polun Dec 30 '19 at 11:11

0 Answers0