ENDCORR - Editorial

Prerequisite :- Priority Queue
Problem :- Given two integers N and M, representing the wealth of different citizens and the number of times the king visits, citizens will keep visiting the king’s court. Whenever the king visits the court, he will execute the wealthiest citizen present in the court. Print the wealth of the executed citizens.

Explanation :-
Using a Priority Queue will help us solve this problem. It is a data structure that allows us to store data based on priority. In this specific problem, we will utilize a max priority queue to prioritize the wealthier individuals up to any given point.
Algorithm : -
Initialize a priority queue.
Keep storing the Citizen according to their wealth as priority in the priority queue.
If the king appears in the court, fetch the wealthiest person, and remove it from the queue.

For better understanding you can refer to IARCS Editorial

C++ Solution : -

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,m;
	cin>>n>>m;
	priority_queue<int>q1;
	for(int i = 0; i < n+m; i++){
    	int n1;
    	cin>>n1;
    	if(n1 == -1){
        	cout<<q1.top()<<"\n";
        	q1.pop();
    	}
    	else{
        	q1.push(n1);
    	}
	}

}

In python we can use this code-

c = []
def siw(w, l, r):
    if l > r:
        c.insert(l, w)
        return
    m = (l+r)//2
    if c[m] > w:
        siw(w, l, m-1)
    else:
        siw(w, m+1, r)
n, m = map(int, input().split())
for _ in range(n+m):
    i = int(input())
    if i == -1:
        print(c.pop())
    else:
        siw(i, 0, len(c)-1)

This code uses Binary Search Algorithm.