Given price and number of pages of each book, What is the maximum number of pages you can buy?
I am afraid that the following recurrence equation as in the question is incorrect.

f(x, 0) = max( f(x-price[indx], indx+1) + pages[indx], f(x,indx+1) )




Let g(i,j) be the maximum number of pages possible when you have i units of money to spare and books 0..j available to select, where 0..j means 0, 1, ..., and j. (I use g instead of f since I am not absolutely sure of the definition of f in the question. It looks like g(i,j) should be the same as f(i,j) in the question. )

Here is the recurrence equation.

g(x, indx+1) = ( x &gt;= price[indx+1] ) ? 
              max( g(x - price[indx+1], indx) + pages[indx+1], g(x,indx) ) :
              g(x, indx)


In plain words, there are (at most) two possibilities when books 0..(indx+1) are available to select.


Book indx+1 is selected (when x &gt;= price[indx+1]). You have pages[indx+1] more pages. 

Then you are left with x - price[indx+1] units of money and books 0..indx to select.
Otherwise.

Then you are left with x units of money and books 0..indx to select.


Note that the available books on the right-hand side of the equation are always less than the available books on the left-hand side. That reduction of a larger problem to a smaller problem signifies the power of recursion.

The base case are when no books are available, i.e., g(i,0) = 0 for all i, and when there is no money to spare, i.e., g(0, j) = 0 for all j.