Minimum total sum from the given two arrays | 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 arr [ ] ; Given cost ; Function Call
def minimumSum ( a , b , c , n ) : NEW_LINE INDENT dp = [ [ 1e6 for i in range ( 2 ) ] for j in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = a [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = b [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) NEW_LINE dp [ i ] [ 0 ] = min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 1 ] + a [ i ] + c ) NEW_LINE dp [ i ] [ 1 ] = min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + b [ i ] + c ) NEW_LINE dp [ i ] [ 1 ] = min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 1 ] + b [ i ] ) NEW_LINE DEDENT print ( min ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 ] NEW_LINE arr2 = [ 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 ] NEW_LINE C = 2 NEW_LINE N = len ( arr1 ) NEW_LINE minimumSum ( arr1 , arr2 , C , N ) NEW_LINE DEDENT