Maximize sum by selecting X different | Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
function FindMaxS ( X , Y , Z , n , A , B , C ) { if ( X < 0 Y < 0 Z < 0 ) return - 1000000000 ; if ( n < 0 ) return 0 ; var ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; var ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; var co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; var no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; var maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; return maximum ; } var X = 1 ; var Y = 1 ; var Z = 1 ; var A = [ 10 , 0 , 5 ] ; var B = [ 5 , 10 , 0 ] ; var C = [ 0 , 5 , 10 ] ; var n = B . length ; document . write ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ;