Okay, so i did the problem. Took a lot of thinking (and time) but it turned out to have a simple approach. I will suggest you to follow an iterative approach.
Your code will be really simple if you term of dp table in terms of weight. Look at spoilers below, if you’re doing dp table in terms of weight. I did it by nested loop, outer loop for values and inner loop updating dp, that upto which weight can this value be included.
Spoilers

Click to view
Make sure dp table is 1 indexed. Its best in terms of convenience. The approach is based of including the element where ever possible, if it leads to a higher value than current.(hint use of max function)

Click to view
When will we add the value? When including its weight will still result in total weight less than or equal to w. Hence, can the expression for dp have something to do with (dp[i+weight[i]]+val[i]) ??

Click to view
Lets say we include an element of value val[i] and weight w[i]. In expression given in spoiler 2.
, we did dp[i+weight[i]]+val[i]. Will it be worthwhile to loop through all regions W such that 0<=W<=TotalCapacityweight[i] and update them?? Can we set limits of inner loop using this?
Hint  Updating them will be of use again, in case we come across a smaller weight.

Click to view
This spoiler contains the loop i used to fill the dp table.
Click to view
for(i=0;i<n;i++)
{
for(j=0;j<=wweight[i];j++)
{
dp[j]=max(dp[j],dp[j+weight[i]]+val[i]);
}
}
Okay…posting hints is tougher than posting solution XD. I hope they help you though ^^. If not, i can explain my solution to you.
BTW, i wont lie, i had to look up at the complexity of proposed solution to start. Once i saw proposed solution’s complexity is not linear, then i started thinking in right direction. Else, I felt it was possible in O(N), but couldnt think of anything. After that I tried doing it with dp[n] (failed), dp[w] (0 base index and well…got overwhelemed in thought process). I didnt expect my solution to pass tho, but looking at working i got some confidence ^^