Recursive formula problem in Dynamic programming

23 Views Asked by At

For my cs class Ive been given the following task. I have n different beers, and the i'th beer has a price of p[i]. the prices are: p1 = 2, p[2] = 3, p[3] = 2, p[4] = 1, p[5] = 4. I also have c coins, and I wish to figure out how many ways I can spend all the money on beers, with n = 5, and c = 5.

I know that there are 4 possible ways I can spend the money, (p1,p[2]), (p1,p[3],p[4]), (p[2],p[3]), (p[4],p[5]).

The task is to write a recursive formula, for the number of ways I can spend all my c money.

For now, I have this recursive formula:

N(C,i) = \begin{cases} 0 &\text{if C $<$ 0 or i $<$ 0 }\\ 1 &\text{if C = 0}\\ N(C,i-1)+N(C-p_i,i) & \text{otherwise} \end{cases}

But I'm not sure if it's correct. I tried using https://recursion.vercel.app to generate a recursion tree from some code I wrote, which should imitate the recursion formula. The generator returns 11 though, but should return 4. Not sure if my code is correct.

I could really use some help, if someone can see if my recursion formula is wrong, or my code is wrong. enter image description here