Help in Maximizing Marks

Can anyone tell why My code is giving Wrong Answer for the Question “Maximizing Marks”

My Solution:

i have gone through the tutorial even looked accepted Solution for the problem…But Still Cant find and problem in My code…

Please Someone help me…

this is 01 knapsack problem. where marks are profit and time is weight.

Yeh I know its 0/1 knapsack…
nd I have implemented it in my code…

I think you need to correct this:

Math.max(mark[i]+dp1[i-1][j-(int)time[i]], dp1[n-1][j])

Why are you considering the dp1[n-1][j] element in the loop while constructing dp1[i][j]?

2 Likes

For the normal 0/1 knapsack algo…

For checking that we are getting maximum while taking element I or by not taking element I in the knapsack…

Just change the ‘n-1’ to ‘i-1’ to get AC :slight_smile:

1 Like

Thankx a lot… :slight_smile:
I have been Searching for this like for 4hrs…

You’re welcome :slight_smile:

//