text: Maximum sum possible from given Matrix by performing given operations | C # code for the above approach ; Function to print the maximum sum possible by selecting at most one element from each column such that no consecutive pairs are selected from a single row ; Initialize variables ; Traverse each column ; Print answer ; Driver Code ; Numberof columns
code: using System ; class GFG { static void maxSum ( int [ , ] arr , int n ) { int r1 = 0 , r2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int temp = r1 ; r1 = Math . Max ( r1 , r2 + arr [ 0 , i ] ) ; r2 = Math . Max ( r2 , temp + arr [ 1 , i ] ) ; } Console . WriteLine ( Math . Max ( r1 , r2 ) ) ; } public static void Main ( ) { int [ , ] arr = { { 1 , 50 , 21 , 5 } , { 2 , 10 , 10 , 5 } } ; int n = arr . GetLength ( 1 ) ; maxSum ( arr , n ) ; } }