#include <iostream>
#include <cstdio>
#include <queue>
#include <utility>
#define maxn 1000005
typedef long long ll;
using namespace std;
ll head[maxn];
ll len = 0,now;
void push(ll y)
{
head[++len] = y;
now = len;
while((now >> 1) >= 1)
{
if(head[now] < head[now >> 1])
{
swap(head[now],head[now >> 1]);
now >>= 1;
}
else
{
break;
}
}
}
void pop()
{
swap(head[1],head[len]);
len--;
now = 1;
while((now << 1) <= len)
{
ll to = now << 1;
if(to + 1 <= len && head[to+1] < head[to])to++;
if(head[to] < head[now])swap(head[to],head[now]);
else break;
now = to;
}
}
ll top()
{
return head[1];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
ll n;
cin >> n;
for(int i = 1; i <= n; i ++)
{
ll x,y;
cin >> x;
if(x == 1)
{
cin >> y;
push(y);
}
else
{
if(x == 2)
{
cout << top() << endl;
}
if(x == 3)
{
pop();
}
}
}
return 0;
}