I am building an xml parser in java which will handle a catalog with some books (that have price and usefulness) and I want to implement an optimum algorithm that selects the books from the catalog which have the maximum usefulness while staying within the budget.
Example:

<catalog cash=”100”>
<book usefulness=”90” price=”60” >The Art of Computer Programming</book>
<book usefulness=”90” price=”40”>Programming Pearls</book>
<book usefulness=”60” price=”40”>American Psycho</book>
</catalog>

,in this case the chosen books would be the first two books because they have a maximum usefulness of 180 and they stay in the budget.

Help would be appreciated,
Cheers

here is some pseudo-code for you

List<Book> usefulBooks;
int cashAvailable = cash;
booklist = query books by usefulness descending
for(Book book : booklist)
{
if(cashAvailable - book.getPrice() > 0)
{
//add the book
usefulBooks.add(book);
//decrement our cash
cashAvailable = cashAvailable - book.getPrice();
}
}
This question has already been answered. Start a new discussion instead.