First multiply negative numbers pairwise and sum up from starting as to get maximum sum .
$ i = 0 ; while ( $ i < $ n && $ arr [ $ i ] < 0 ) { if ( $ i != $ n - 1 && $ arr [ $ i + 1 ] <= 0 ) { $ sum = ( $ sum + ( $ arr [ $ i ] * $ arr [ $ i + 1 ] ) % $ Mod ) % $ Mod ; $ i += 2 ; } else break ; }