text: Maximum sum of values of N items in 0 | C # implementation for the above approach ; Function to find the maximum value ; base condition ; K elements already reduced to half of their weight ; Dont include item ; If weight of the item is less than or equal to the remaining weight then include the item ; Return the maximum of both cases ; If the weight reduction to half is possible ; Skip the item ; Include item with full weight if weight of the item is less than the remaining weight ; Include item with half weight if half weight of the item is less than the remaining weight ; Return the maximum of all 3 cases ; Driver code
code: using System ; public class GFG { static int maximum ( int [ ] value , int [ ] weight , int weight1 , int flag , int K , int index ) { if ( index >= value . Length ) { return 0 ; } if ( flag == K ) { int skip = maximum ( value , weight , weight1 , flag , K , index + 1 ) ; int full = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 ) ; } return Math . Max ( full , skip ) ; } else { int skip = maximum ( value , weight , weight1 , flag , K , index + 1 ) ; int full = 0 ; int half = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 ) ; } if ( weight [ index ] / 2 <= weight1 ) { half = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] / 2 , flag , K , index + 1 ) ; } return Math . Max ( full , Math . Max ( skip , half ) ) ; } } public static void Main ( String [ ] args ) { int [ ] value = { 17 , 20 , 10 , 15 } ; int [ ] weight = { 4 , 2 , 7 , 5 } ; int K = 1 ; int W = 4 ; Console . WriteLine ( maximum ( value , weight , W , 0 , K , 0 ) ) ; } }