#include <bits/stdc++.h>
#define ll long long
const ll maxn = 2e5 + 5;
typedef struct heap {
ll capacity;
ll curr_size;
ll *arr;
ll (*func)(ll, ll);
}heap_t;
ll max_heap(ll a, ll b);
ll min_heap(ll a, ll b);
heap_t* init(ll n, ll (*cmp)(ll, ll));
void insert(heap_t* heap, ll x);
void insert_help(heap_t* heap, ll idx, ll (*fcmp)(ll, ll));
ll extract(heap_t* heap);
void heapify(heap_t* heap, ll idx, ll (*fcmp)(ll, ll));
ll max_heap(ll a, ll b) {
return a > b;
}
ll min_heap(ll a, ll b) {
return a < b;
}
heap_t* init(ll cap, ll (*cmp)(ll, ll)) {
heap_t* heap = (heap_t*)calloc(1, sizeof(heap_t));
if (!heap) {
return NULL;
}
heap->capacity = cap;
heap->arr = (ll*)calloc(cap, sizeof(ll));
if (!heap->arr) {
free(heap);
return NULL;
}
heap->func = cmp;
return heap;
}
void insert(heap_t* heap, ll x) {
if (heap->curr_size >= heap->capacity) {
printf("loh\n");
return;
}
heap->arr[heap->curr_size] = x;
heap->curr_size++;
insert_help(heap, heap->curr_size - 1, heap->func);
}
void insert_help(heap_t* heap, ll idx, ll (*fcmp)(ll, ll)) {
ll parent = (idx - 1) / 2;
if (fcmp(heap->arr[idx], heap->arr[parent])) {
ll temp = heap->arr[parent];
heap->arr[parent] = heap->arr[idx];
heap->arr[idx] = temp;
insert_help(heap, parent, heap->func);
}
}
ll extract(heap_t* heap) {
if (!heap->curr_size || !heap) {
printf("Heap is empty!\n");
return INT_MIN;
}
ll extr = heap->arr[0];
heap->arr[0] = heap->arr[heap->curr_size - 1];
heap->curr_size--;
heapify(heap, 0, heap->func);
return extr;
}
void heapify(heap_t* heap, ll idx, ll (*fcmp)(ll, ll)) {
ll left = 2 * idx + 1;
ll right = 2 * idx + 2;
ll child = idx;
if (left >= heap->curr_size) {
left = -1;
}
if (right >= heap->curr_size) {
right = -1;
}
if (left != -1 && fcmp(heap->arr[left], heap->arr[child])) {
child = left;
}
if (right != -1 && fcmp(heap->arr[right], heap->arr[child])) {
child = right;
}
if (child != idx) {
ll temp = heap->arr[child];
heap->arr[child] = heap->arr[idx];
heap->arr[idx] = temp;
heapify(heap, child, heap->func);
}
}
int main() {
FILE* input = fopen("heapuri.in", "r");
FILE* output = fopen("heapuri.out", "w");
ll n = 0;
fscanf(input, "%lld", &n);
std::vector<ll> a;
heap_t* heap = init(maxn, min_heap);
for (ll i = 0; i < n; i++) {
ll type = 0;
fscanf(input, "%lld", &type);
if (type == 1) {
ll x = 0;
fscanf(input, "%lld", &x);
a.push_back(x);
insert(heap, x);
}
else if (type == 2) {
ll x = 0;
fscanf(input, "%lld", &x);
ll idx = 0;
x--;
for (ll i = 0; i < heap->curr_size; i++) {
if (a[x] == heap->arr[i]) {
idx = i;
break;
}
}
heap->arr[idx] = heap->arr[heap->curr_size - 1];
heap->curr_size--;
heapify(heap, idx, min_heap);;
}
else {
fprintf(output, "%lld\n", heap->arr[0]);
}
}
return 0;
}