I\'m doing a Knapsack in Java where we only use weights no value. The weightlimit is 1000. We get 5 weights scanned from keyboard which we use. The twist is that you can ac
I would evaluate this problem first as a classical knapsack problem taking value[i] = weight[i] ;
, where i
is the i'th item and maximum weight to be the given max_wt (1000 kg) and item[] be an array containing the items in ascending order of their weights .
Let the answer of this problem be x , say 990 kg , now i would calculate the difference 'd' ,
d = max_wt - x ;
iterate over item[] till item[i] exceeds d :
int i = 0 ;
while(item[i] < d )
i++;
,lastly add the first item that exceeds 'd' to the answer you got through the classical knapsack problem :
answer = dp[n-1][w-1] + item[i] \\dp[n-1][w-1] is the answer of the classical
\\knapsack problem