分块思想
//
// main.cpp
// PATA1057
//
// Created by Phoenix on 2018/2/14.
// Copyright © 2018年 Phoenix. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn = 317;
int a[maxn] = {0}, b[maxn * maxn] = {0}, num = 0;
stack<int> s;
int main(int argc, const char * argv[]) {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
char str[12];
scanf("%s", str);
if(str[1] == 'u') {
int t;
scanf("%d", &t);
s.push(t);
a[t / maxn]++;
b[t]++;
}else if (str[1] == 'o') {
if(!s.empty()) {
int top = s.top();
printf("%d\n", top);
s.pop();
a[top / maxn]--;
b[top]--;
} else {
printf("Invalid\n");
}
} else if(str[1] == 'e') {
if(!s.empty()) {
int mid = (s.size() + 1) / 2;
int k = 0, ans = 0;
while(a[k] + ans < mid) {
ans += a[k];
k++;
}
k = k * maxn;
while(b[k] + ans < mid) {
ans += b[k];
k++;
}
printf("%d\n", k);
} else {
printf("Invalid\n");
}
}
}
return 0;
}