Problem in Minimum Money needed

The question’s link is https://online.codingblocks.com/player/12341/content/4827?s=1515
I’m trying top down dp in this problem but my answer is coming out to be wrong.

my solution’s link is https://ide.codingblocks.com/s/48008
Please guide.

Heyy Surender !!! The thing is , you are considering this question as 1D-DP but as you can see in the recursive function there are two parameters changing as per the function call , so this is a 2D - DP problem .
So , suppose dp[i][w] denotes the minimum money needed to buy w kg from using the packets of i+1 onwards , and keep in mind that there are infinite amount of each packets . so, your recursive relations will be like
dp[i][w]=min(dp[i+1][w],dp[i][w-i]) (w>=i).
If you still have doubt in it , feel free to ask :blush:

1 Like

sir, i have got your point but i’m unable to implement the code. Could you please share the pseudo code?