Minimum total sum from the given two arrays | C # program for the above approach ; Function that prints minimum sum after selecting N elements ; Initialise the dp array ; Base Case ; Adding the element of array a if previous element is also from array a ; Adding the element of array a if previous element is from array b ; Adding the element of array b if previous element is from array a with an extra penalty of integer C ; Adding the element of array b if previous element is also from array b ; Print the minimum sum ; Driver Code ; Given array arr1 [ ] and arr2 [ ] ; Given cost ; Function call
using System ; class GFG { static void minimumSum ( int [ ] a , int [ ] b , int c , int n ) { int [ , ] dp = new int [ n , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = ( int ) 1e6 ; } } dp [ 0 , 0 ] = a [ 0 ] ; dp [ 0 , 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = Math . Min ( dp [ i , 0 ] , dp [ i - 1 , 0 ] + a [ i ] ) ; dp [ i , 0 ] = Math . Min ( dp [ i , 0 ] , dp [ i - 1 , 1 ] + a [ i ] + c ) ; dp [ i , 1 ] = Math . Min ( dp [ i , 1 ] , dp [ i - 1 , 0 ] + b [ i ] + c ) ; dp [ i , 1 ] = Math . Min ( dp [ i , 1 ] , dp [ i - 1 , 1 ] + b [ i ] ) ; } Console . Write ( Math . Min ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 } ; int [ ] arr2 = { 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 } ; int C = 2 ; int N = arr1 . Length ; minimumSum ( arr1 , arr2 , C , N ) ; } }