#include #include #include #include #include using namespace std; using ll = long long; constexpr int iINF = 1'000'000'000; constexpr ll llINF = 1'000'000'000'000'000'000; int main () { int N, Q; cin >> N >> Q; map normal, setuped; map mp; vector go_back; vector> buf; auto remove_min = [&] (map& mp) { auto it = mp.lower_bound(-1); buf.push_back(make_pair(it->first, it->second)); mp.erase(it); }; auto clear_buf = [&] () { sort(buf.begin(), buf.end(), [&](pair& a, pair& b) { return a.first < b.first; }); for (auto v : buf) go_back.push_back(v.second); buf.resize(0); }; for (int i = 0; i < Q; i++) { int t; cin >> t; if (t == 1) { string s; int r; cin >> s >> r; normal[r] = s; mp[s] = r; if (N < normal.size() + setuped.size()) { remove_min(normal); } } if (t == 2) { int x; cin >> x; N -= x; while (N < normal.size() + setuped.size()) { if (0 < normal.size()) { remove_min(normal); continue; } remove_min(setuped); } } if (t == 3) { string s; int x; cin >> s >> x; N += x; setuped[mp[s]] = s; normal.erase(mp[s]); } clear_buf(); } for (auto name : go_back) { cout << name << "\n"; } }