in reply to Knapsack problem

This is the knapsack problem. It is NP-hard. Were you to find a polynomial-time solution to it, you'd have basically solved all the hardest problems in computer science.

There are heuristic approaches that shoot for decent solutions that don't attempt or claim to be optimal solutions. If you follow the Google link above, you'll probably find some.