get elements from stack until element greater or equal to A [ i ] found
while ( s2 . length != 0 && ( s2 [ s2 . length - 1 ] [ 0 ] ) >= A [ i ] ) { cnt += s2 [ s2 . length - 1 ] [ 1 ] ; s2 . pop ( ) ; } s2 . push ( [ A [ i ] , cnt ] ) ; right [ i ] = cnt ; } var result = 0 ;