Maximize sum of product of neighbouring elements of the element removed from Array | Function to calculate maximum possible score using the given operations ; Iterate through all possible len1gths of the subarray ; Iterate through all the possible starting indices i having len1gth len1 ; Stores the rightmost index of the current subarray ; Initial dp [ i ] [ j ] will be 0. ; Iterate through all possible values of k in range [ i + 1 , j - 1 ] ; Return the answer ; Driver Code ; Function Call ; Function Call
def maxMergingScore ( A , N ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 101 ) ] for j in range ( 101 ) ] NEW_LINE for len1 in range ( 1 , N , 1 ) : NEW_LINE INDENT for i in range ( 0 , N - len1 , 1 ) : NEW_LINE INDENT j = i + len1 NEW_LINE dp [ i ] [ j ] = 0 NEW_LINE for k in range ( i + 1 , j , 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k ] [ j ] + A [ i ] * A [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ 0 ] [ N - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE A = [ 1 , 2 , 3 , 4 ] NEW_LINE print ( maxMergingScore ( A , N ) ) NEW_LINE N = 2 NEW_LINE B = [ 1 , 55 ] NEW_LINE print ( maxMergingScore ( B , N ) ) NEW_LINE DEDENT