Fill Up res [ ] from left to right
for i in range ( N ) : NEW_LINE INDENT pq . sort ( ) NEW_LINE pq . reverse ( ) NEW_LINE d = pq [ 0 ] NEW_LINE del pq [ 0 ] NEW_LINE if ( d != seq [ i ] or i == N - 1 ) : NEW_LINE INDENT res [ i ] = d NEW_LINE DEDENT else : NEW_LINE DEDENT