according to me logic is correct but not passing test case hel me in finding the mistake https://ide.codingblocks.com/s/598419
0-1 knapsack problem
-
you have to inc the size of array according to constraints
-
inc should be initialised with some lower value like -1
after these 2 changes your code gives correct ans
but as your code time complexity is high so also gives TLE in 2 Testcases
to avoid TLE you have to use DP.
Modified Code