#include #include using namespace std; int main() { // small.top() は small の最大値 (K個になるようにする) priority_queue small; // large.top() は large の最小値 priority_queue, greater > large; int Q, K; cin >> Q >> K; for (int i = 0; i < Q; ++i) { int T; cin >> T; if (T == 1) { long long V; cin >> V; // そもそも K 個に達していなかったら small へ if (small.size() < K) { small.push(V); continue; } // k 番目の値 long long current_kth = small.top(); // 現在の k 番目の値より小さかったら、それをlarge 側へ追い出して V を small に push if (V < current_kth) { small.pop(); small.push(V); large.push(current_kth); } // そうでないときは単純に large 側に push else { large.push(V); } } else { // そもそも K 個に達していなかったらダメ if (small.size() < K) { cout << -1 << endl; continue; } // k 番目の値を small から pop して、large の top を small へ移す long long current_kth = small.top(); cout << current_kth << endl; small.pop(); if (!large.empty()) { long long next_kth = large.top(); large.pop(); small.push(next_kth); } } } }