#include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; // using mint = modint998244353; using mint = modint1000000007; using vi = vector; using vvi = vector>; using vl = vector; using vvl = vector>; using ll = long long; template using max_heap = priority_queue; template using min_heap = priority_queue, greater<>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, f, n) for (int i = (int) f; i < (int)(n); i++) #define repd(i, n, l) for (int i = (int) n; i >= (int) l; i--) #define all(p) p.begin(),p.end() vector> dydx{{-1, 0}, {1, 0}, {0, -1 }, {0, 1}}; const ll inf = 1LL << 60; void print(){ putchar(' '); } void print(bool a){ printf("%d", a); } void print(int a){ printf("%d", a); } void print(unsigned a){ printf("%u", a); } void print(long a){ printf("%ld", a); } void print(long long a){ printf("%lld", a); } void print(unsigned long long a){ printf("%llu", a); } void print(char a){ printf("%c", a); } void print(char a[]){ printf("%s", a); } void print(const char a[]){ printf("%s", a); } void print(float a){ printf("%.15f", a); } void print(double a){ printf("%.15f", a); } void print(long double a){ printf("%.15Lf", a); } void print(const string& a){ for(auto&& i : a) print(i); } template void print(const complex& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); } template void print(const vector&); template void print(const array&); template void print(const pair& p); template void print(const T (&)[size]); template void print(const vector& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } } template void print(const deque& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } } template void print(const array& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } } template void print(const pair& p){ print(p.first); putchar(' '); print(p.second); } template void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } } template void print(const T& a){ cout << a; } int main() { int n, q; cin >> n >> q; min_heap> h1; min_heap> h2; unordered_map mp; int cnt = 0; vector> ret; while (q--){ int t; cin >> t; if (t == 1){ string s; int r; cin >> s >> r; mp[s] = false; h1.push({r, s}); cnt++; } else if (t == 2){ int x;cin >> x; n -= x; } else if (t == 3){ string s; int x; cin >> s >> x; n += x; mp[s] = true; } while (!h1.empty() && cnt > n){ int r = h1.top().first; string c = h1.top().second; h1.pop(); if (!mp[c]){ ret.push_back({r, c}); cnt--; } else { h2.push({r, c}); } } while (!h2.empty() && cnt > n){ int r = h2.top().first; string c = h2.top().second; ret.push_back({r, c}); cnt--; } if (ret.size() >= 1) { sort(all(ret)); for (auto [_, c] : ret) cout << c << endl; ret.clear(); } } return 0; }