#include <iostream>
#include <set>
#include <stack>
using namespace std;
void test01()
{
int n , i , j ;
multiset<int> se;
stack<int> st;
cin >> n ;
for (i = 0;i < n;i++)
{
cin >> j ;
if (j == 0)
{
int x ;
cin >> x ;
se.insert(x);
st.push(x);
}
else if (j == 1)
{
int k ;
k = st.top();
st.pop();
se.erase(k);
}
else if (j == 2)
{
if (st.empty())
{
cout << "0" << endl;
}
else
{
multiset<int>::iterator it = se.end();
it--;
cout << *it << endl;
}
}
}
}
int main ()
{
test01();
return 0;
}