Storing elements and indexes
vector < pair < int , int > > Ap , Bp ; for ( int i = 0 ; i < n ; i ++ ) Ap . push_back ( make_pair ( A [ i ] , i ) ) ; for ( int i = 0 ; i < n ; i ++ ) Bp . push_back ( make_pair ( B [ i ] , i ) ) ; sort ( Ap . begin ( ) , Ap . end ( ) ) ; sort ( Bp . begin ( ) , Bp . end ( ) ) ; int i = 0 , j = 0 , ans [ n ] = { 0 } ;