#include <bits/stdc++.h>
using namespace std;
const int N=1e7;
long long h[N];
int n,op;
int idx;
void siftup(int pos)
{
while( pos>1 && h[pos]< h[pos/2])
{
swap(h[pos],h[pos/2]);
pos /=2;
}
}
void siftdown(int pos)
{
int j=pos*2;
if( j <= idx)
{
if( j+1<=idx && h[j]>h[j+1]) j++;
}
while(pos <= idx && h[pos]>h[j] )
{
swap(h[pos],h[j]);
pos=j;
j *=2;
}
}
void insert(long long x){
h[++idx]=x;
siftup(idx);
}
void top(){
cout<<h[1]<<endl;
}
void remove(){
if(idx >= 1)
{
h[1]=h[idx--];
siftdown(1);
}
}
int main(){
cin>>n;
while(n--)
{
cin>>op;
if(op == 1) {long long x;cin>>x;insert(x);}
if(op == 2) top();
if(op == 3) remove();
}
return 0;
}