If the value at that index is negative then we need to find the index of the value just greater than - D [ i ]
else { int k = upper_bound ( D . begin ( ) , D . end ( ) , - D [ i ] ) - D . begin ( ) ; total += n - k ; } } return total ; }