If the previous difference is positive
if ( rightDP [ i ] > 0 ) rightDP [ i ] = rightDiff + rightDP [ i - 1 ] ; else rightDP [ i ] = rightDiff ; } c ++ ; } let max = 0 ; for ( let i = 0 ; i < n / 2 ; i ++ ) { max = Math . max ( max , Math . max ( leftDP [ i ] , rightDP [ i ] ) ) ; } return max + sum ; }