Pushing elements in priority queue where the key is f [ i ]
p . push ( make_pair ( f [ i ] , s [ i ] ) ) ; } auto it = p . top ( ) ; int start = it . second ; int end = it . first ; p . pop ( ) ; ans . push_back ( make_pair ( start , end ) ) ; while ( ! p . empty ( ) ) { auto itr = p . top ( ) ; p . pop ( ) ; if ( itr . second >= end ) { start = itr . second ; end = itr . first ; ans . push_back ( make_pair ( start , end ) ) ; } } cout << " Following ▁ Activities ▁ should ▁ be ▁ selected . ▁ " << endl << endl ; for ( auto itr = ans . begin ( ) ; itr != ans . end ( ) ; itr ++ ) { cout << " Activity ▁ started ▁ at : ▁ " << ( * itr ) . first << " ▁ and ▁ ends ▁ at ▁ " << ( * itr ) . second << endl ; } }