while stack is not empty and the top element is greater than next a ) NSE for top is next , use top 's index to maintain original order b) pop the top element from stack
while ( ! s . empty ( ) && s . top ( ) . first > next ) { ans [ s . top ( ) . second ] = next ; s . pop ( ) ; }