#include<iostream>
using namespace std;
const int MAXN=1e6+5;
int heap[MAXN];
int len=0;
void push(int x)
{
heap[++len]=x;
int sign=len;
while(sign>1&&heap[sign]<heap[sign/2])
{
swap(heap[sign],heap[sign/2]);
sign=sign/2;
}
}
void pop(int x)
{
heap[1]=heap[len--];
int sign=1;
while(2*sign<=len)
{
int son=2*sign;
if(son<len&&heap[son]>heap[son+1])
{
son++;
}
if(heap[sign]>heap[son])
{
swap(heap[sign],heap[son]) ;
sign=son ;
}
else
break;
}
}
int main()
{
int N;
int xx;
int signs;
cin>>N;
while(N--)
{
cin>>signs;
if(signs==1)
{
cin>>xx;
push(xx);
}
else if(signs==2)
{
cout<<heap[1]<<endl;;
}
else
pop(xx);
}
return 0;
}