Maximize sum that can be obtained from two given arrays based on given conditions | C # program for the above approach ; Function to find the maximum sum that can be obtained from two given based on the following conditions ; Stores the maximum sum from 0 to i ; Initialize the value of dp [ 0 ] [ 0 ] and dp [ 0 ] [ 1 ] ; Traverse the array A [ ] and B [ ] ; If A [ i ] is considered ; If B [ i ] is not considered ; If B [ i ] is considered ; If i = 1 , then consider the value of dp [ i ] [ 1 ] as b [ i ] ; Return maximum Sum ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int MaximumSum ( int [ ] a , int [ ] b , int n ) { int [ , ] dp = new int [ n , 2 ] ; dp [ 0 , 0 ] = a [ 0 ] ; dp [ 0 , 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) + a [ i ] ; dp [ i , 1 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) ; if ( i - 2 >= 0 ) { dp [ i , 1 ] = Math . Max ( dp [ i , 1 ] , Math . Max ( dp [ i - 2 , 0 ] , dp [ i - 2 , 1 ] ) + b [ i ] ) ; } else { dp [ i , 1 ] = Math . Max ( dp [ i , 1 ] , b [ i ] ) ; } } return Math . Max ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) ; } public static void Main ( ) { int [ ] A = { 10 , 1 , 10 , 10 } ; int [ ] B = { 5 , 50 , 1 , 5 } ; int N = A . Length ; Console . Write ( MaximumSum ( A , B , N ) ) ; } }