Here in the loop we will check that whether the upcoming element of array is less than top of priority queue . If yes then we calculate the difference . After that we will remove that element and push the current element in queue . And the sum is incremented by the value of difference
for ( int i = 0 ; i < n ; i ++ ) { if ( ! pq . isEmpty ( ) && pq . element ( ) < a [ i ] ) { dif = a [ i ] - pq . element ( ) ; sum += dif ; pq . remove ( ) ; } pq . add ( a [ i ] ) ; } return sum ; }