Maximize sum of product of neighbouring elements of the element removed from Array | Stores the dp state where dp [ i ] [ j ] represents the maximum possible score in the subarray from index i to j ; Function to calculate maximum possible score using the given operations ; Iterate through all possible lengths of the subarray ; Iterate through all the possible starting indices i having length len ; 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
let dp = Array ( 101 ) . fill ( ) . map ( ( ) => Array ( 101 ) ) ; function maxMergingScore ( A , N ) { for ( let len = 1 ; len < N ; ++ len ) { for ( let i = 0 ; i + len < N ; ++ i ) { let j = i + len ; dp [ i ] [ j ] = 0 ; for ( let k = i + 1 ; k < j ; ++ k ) { dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k ] [ j ] + A [ i ] * A [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } let N = 4 ; let A = [ 1 , 2 , 3 , 4 ] ; document . write ( maxMergingScore ( A , N ) + " " ) ; N = 2 ; let B = [ 1 , 55 ] ; document . write ( maxMergingScore ( B , N ) + " " ) ;