Second multiply positive numbers pairwise and summed up from the last as to get maximum sum .
$ j = $ n - 1 ; while ( $ j >= 0 && $ arr [ $ j ] > 0 ) { if ( $ j != 0 && $ arr [ $ j - 1 ] > 0 ) { $ sum = ( $ sum + ( $ arr [ $ j ] * $ arr [ $ j - 1 ] ) % $ Mod ) % $ Mod ; $ j -= 2 ; } else break ; }