Cannot figure out mistake in 0-N Knapsack

I know my code is not giving the correct output but i can’t seem to figure out why. Also, for 0-1 knapsack i almost used an identical approach with just a slight change and it was working.
Here is the code.

Hi
Can you pls comment your code or explain me your logic here?

I have added some comments, feel free to ask for more explanation on the code logic.

Also, can you have a look at my 0-1 knapsack solution as well, because of 2 reasons. One it is correct, Second since i have used very similar approach for 0-N, I’ll be able to explain my 0-N knapsack solution in a better way.
https://ide.codingblocks.com/s/100582 --> This is 0-1 Knapsack with clearer explaination.

PS-Any help is much appreciated

Hi
Both of your logics are exactly same
But this is not what question demands, in 0-N knapsack you can take one item any number of times.
Are you trying to ask the exact logic of solving 0-N knapsack? As clearly 0-1 approach wont work here

Hi, I figured out the mistake. Since i was using the same logic as 0-1 knapsack, i had to use a 2D DP array for 0-N knapsack as well. I modified the code by just adding 2D DP instead of 1D, it worked. Moreover, I believe my 0-1 Knapsack solution is wrong but i still got the points because of weak test-cases.
https://ide.codingblocks.com/s/101855 --> 0-N Knapsack with 2D DP.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.