import std.stdio; import std.range; import std.array; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.container; void main() { auto val = readln.chomp.split.map!(to!int); int q = val.front; int k = val.back; alias TreeOne = RedBlackTree!(ulong, "a < b", true); TreeOne treeLeft = new TreeOne(); TreeOne treeRight = new TreeOne(); foreach (i; 0..q) { auto wal = readln.chomp.split.map!(to!ulong); if (wal.front == 1) { auto trg = wal.back; treeLeft.insert(trg); if (treeLeft.length > k) { auto dst = treeLeft.back; treeLeft.removeKey = dst; treeRight.insert(dst); } } else { auto l = treeLeft.length; if (l < k) { writeln = -1; continue; } auto v = treeLeft.back; treeLeft.removeKey = v; if (treeRight.length) { auto w = treeRight.front; treeRight.removeKey = w; treeLeft.insert(w); } v.writeln; } } }