#include #include #include using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, Q, i, r, x; string s; char t; set> priority, member; map rating; cin >> N >> Q; for (i = 0; i != Q; ++i) { cin >> t; switch (t) { case '1': cin >> s >> r; member.emplace(r, s), rating.insert({ s, r }); if (priority.size() + member.size() > N) { if (!member.empty()) { cout << member.begin()->second << '\n'; rating.erase(rating.find(member.begin()->second)); member.erase(member.begin()); } else { cout << priority.begin()->second << '\n'; rating.erase(rating.find(priority.begin()->second)); priority.erase(priority.begin()); } } break; case '2': cin >> x; N -= x; while (priority.size() + member.size() > N) { if (!member.empty()) { cout << member.begin()->second << '\n'; rating.erase(rating.find(member.begin()->second)); member.erase(member.begin()); } else { cout << priority.begin()->second << '\n'; rating.erase(rating.find(priority.begin()->second)); priority.erase(priority.begin()); } } break; case '3': cin >> s >> x; N += x; if (member.find(make_pair(rating[s], s)) != member.end()) { priority.emplace(rating[s], s); member.erase(member.find(make_pair(rating[s], s))); } break; } } return 0; }