using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); int q = inpt[0]; int k = inpt[1]; SortedDictionary primaryList = new SortedDictionary(); SortedDictionary secandaryList = new SortedDictionary(); int itemCount = 0; StringBuilder ans = new StringBuilder(); for (int i = 0; i < q; i++) { long[] tmp = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray(); if(tmp[0]==1) { long num = tmp[1]; if(itemCount>=k) { if(primaryList.Keys.Last()>=num) { if(primaryList.ContainsKey(num)) { primaryList[num]++; } else { primaryList.Add(num, 1); } itemCount++; while(itemCount>k) { long key = primaryList.Last().Key; if(primaryList[key]<=1) { primaryList.Remove(key); } else { primaryList[key]--; } itemCount--; if(secandaryList.ContainsKey(key)) { secandaryList[key]++; } else { secandaryList.Add(key, 1); } } } else { if(secandaryList.ContainsKey(num)) { secandaryList[num]++; } else { secandaryList.Add(num, 1); } } } else { if(primaryList.ContainsKey(num)) { primaryList[num]++; } else { primaryList.Add(num, 1); } itemCount++; } } else { if(itemCount0) { long addKey = secandaryList.First().Key; if(primaryList.ContainsKey(addKey)) { primaryList[addKey]++; } else { primaryList.Add(addKey, 1); } if(secandaryList[addKey]<=1) { secandaryList.Remove(addKey); } else { secandaryList[addKey]--; } } else { itemCount--; } } } } Console.Write(ans.ToString()); } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 1 1 2 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }