// author - shreyanshjn
#include <bits/stdc++.h>
using namespace std;
class Heap {
private:
vector<int> arr;
public:
int getRightChild(int i) {
return i * 2 + 2;
}
int getLeftChild(int i) {
return i * 2 + 1;
}
int getPar(int i) {
return (i - 1) / 2;
}
bool hasRightChild(int i) {
return i * 2 + 2 < (int)arr.size();
}
bool hasLeftChild(int i) {
return i * 2 + 1 < (int)arr.size();
}
int top() {
return arr[0];
}
void push(int x) {
arr.push_back(x);
heapifyUp();
}
void pop() {
int index = arr.size() - 1;
swap(arr[0], arr[index]);
arr.pop_back();
heapifyDown();
}
void heapifyUp() {
int index = arr.size() - 1;
int par = (index - 1) / 2;
while(par >= 0 and arr[index] > arr[par]) {
swap(arr[index], arr[par]);
index = par;
par = (index - 1) / 2;
}
}
void heapifyDown() {
int index = 0;
while(hasLeftChild(index)) {
int greater = getLeftChild(index);
if(hasRightChild(index) and arr[getRightChild(index)] > arr[greater]) {
greater = getRightChild(index);
}
if(arr[index] > arr[greater]) {
break;
}
else {
swap(arr[index], arr[greater]);
}
index = greater;
}
}
};
int main() {
int n; cin >> n;
Heap h;
for(int i = 0; i < n; i++) {
string str; cin >> str;
if(str == "push") {
int a; cin >> a;
h.push(a);
}
else if(str == "pop") {
h.pop();
}
else if(str == "top") {
cout << h.top() << endl;
}
}
}
Ly8gYXV0aG9yIC0gc2hyZXlhbnNoam4KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBIZWFwIHsKcHJpdmF0ZToKICAgIHZlY3RvcjxpbnQ+IGFycjsKcHVibGljOgogICAgaW50IGdldFJpZ2h0Q2hpbGQoaW50IGkpIHsKICAgICAgICByZXR1cm4gaSAqIDIgKyAyOwogICAgfQogICAgaW50IGdldExlZnRDaGlsZChpbnQgaSkgewogICAgICAgIHJldHVybiBpICogMiArIDE7CiAgICB9CiAgICBpbnQgZ2V0UGFyKGludCBpKSB7CiAgICAgICAgcmV0dXJuIChpIC0gMSkgLyAyOwogICAgfQogICAgYm9vbCBoYXNSaWdodENoaWxkKGludCBpKSB7CiAgICAgICAgcmV0dXJuIGkgKiAyICsgMiA8IChpbnQpYXJyLnNpemUoKTsKICAgIH0KICAgIGJvb2wgaGFzTGVmdENoaWxkKGludCBpKSB7CiAgICAgICAgcmV0dXJuIGkgKiAyICsgMSA8IChpbnQpYXJyLnNpemUoKTsKICAgIH0KICAgIGludCB0b3AoKSB7CiAgICAgICAgcmV0dXJuIGFyclswXTsKICAgIH0KICAgIHZvaWQgcHVzaChpbnQgeCkgewogICAgICAgIGFyci5wdXNoX2JhY2soeCk7CiAgICAgICAgaGVhcGlmeVVwKCk7CiAgICB9CiAgICB2b2lkIHBvcCgpIHsKICAgICAgICBpbnQgaW5kZXggPSBhcnIuc2l6ZSgpIC0gMTsKICAgICAgICBzd2FwKGFyclswXSwgYXJyW2luZGV4XSk7CiAgICAgICAgYXJyLnBvcF9iYWNrKCk7CiAgICAgICAgaGVhcGlmeURvd24oKTsKICAgIH0KICAgIHZvaWQgaGVhcGlmeVVwKCkgewogICAgICAgIGludCBpbmRleCA9IGFyci5zaXplKCkgLSAxOwogICAgICAgIGludCBwYXIgPSAoaW5kZXggLSAxKSAvIDI7CiAgICAgICAgd2hpbGUocGFyID49IDAgYW5kIGFycltpbmRleF0gPiBhcnJbcGFyXSkgewogICAgICAgIAlzd2FwKGFycltpbmRleF0sIGFycltwYXJdKTsKICAgICAgICAJaW5kZXggPSBwYXI7CiAgICAgICAgCXBhciA9IChpbmRleCAtIDEpIC8gMjsKICAgICAgICB9CiAgICB9CiAgICB2b2lkIGhlYXBpZnlEb3duKCkgewogICAgICAgIGludCBpbmRleCA9IDA7CiAgICAgICAgd2hpbGUoaGFzTGVmdENoaWxkKGluZGV4KSkgewogICAgICAgICAgICBpbnQgZ3JlYXRlciA9IGdldExlZnRDaGlsZChpbmRleCk7CiAgICAgICAgICAgIGlmKGhhc1JpZ2h0Q2hpbGQoaW5kZXgpIGFuZCBhcnJbZ2V0UmlnaHRDaGlsZChpbmRleCldID4gYXJyW2dyZWF0ZXJdKSB7CiAgICAgICAgICAgICAgICBncmVhdGVyID0gZ2V0UmlnaHRDaGlsZChpbmRleCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoYXJyW2luZGV4XSA+IGFycltncmVhdGVyXSkgewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBzd2FwKGFycltpbmRleF0sIGFycltncmVhdGVyXSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaW5kZXggPSBncmVhdGVyOwogICAgICAgIH0KICAgIH0KfTsKCmludCBtYWluKCkgewogICAgaW50IG47IGNpbiA+PiBuOwogICAgSGVhcCBoOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHN0cmluZyBzdHI7IGNpbiA+PiBzdHI7CiAgICAgICAgaWYoc3RyID09ICJwdXNoIikgewogICAgICAgICAgICBpbnQgYTsgY2luID4+IGE7CiAgICAgICAgICAgIGgucHVzaChhKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihzdHIgPT0gInBvcCIpIHsKICAgICAgICAgICAgaC5wb3AoKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihzdHIgPT0gInRvcCIpIHsKICAgICAgICAgICAgY291dCA8PCBoLnRvcCgpIDw8IGVuZGw7CiAgICAgICAgfQogICAgfQp9