#include #include typedef long long int int64; typedef int64 node; typedef struct binaryHeap{ node *h; int size; int (*cmp)(const node*,const node*); } heap; heap* init(int maxSize,int (*cmpF)(const node*,const node*)){ heap *res=(heap *)malloc(sizeof(heap)); res->h=(node *)malloc(sizeof(node)*(maxSize+1)); res->size=0; res->cmp=cmpF; return res; } void push(heap *q,const node *p){ q->size+=1; int k=q->size; int (*cmp)(const node*,const node*)=q->cmp; q->h[k]=*p; while(k>1){ if(cmp(q->h+k/2,q->h+k)) return; node swap=q->h[k/2]; q->h[k/2]=q->h[k]; q->h[k]=swap; k/=2; } return; } void pop(heap *q,node *res){ node *h=q->h; *res=h[1]; h[1]=h[q->size]; q->size-=1; int n=q->size; int (*cmp)(const node*,const node*)=q->cmp; int k=1; while(2*k+1<=n){ int next=cmp(h+2*k,h+2*k+1)?2*k:2*k+1; if(cmp(h+k,h+next)) return; node swap=h[k]; h[k]=h[next]; h[next]=swap; k=next; } if(2*k<=n && cmp(h+2*k,h+k)){ node swap=h[k]; h[k]=h[2*k]; h[2*k]=swap; } return; } int cmpMin(const node *a,const node *b){ return *a<=*b; } int cmpMax(const node *a,const node *b){ return !cmpMin(a,b); } void run(void){ int q,k; scanf("%d%d",&q,&k); heap *maxHeap=init(k+1,cmpMax); heap *minHeap=init(q,cmpMin); while(q--){ int c; scanf("%d",&c); if(c==1){ int64 v; scanf("%lld",&v); push(maxHeap,&v); if(maxHeap->size>k){ pop(maxHeap,&v); push(minHeap,&v); } } else { if(maxHeap->size==k){ int64 v; pop(maxHeap,&v); printf("%lld\n",v); if(minHeap->size>0){ pop(minHeap,&v); push(maxHeap,&v); } } else { printf("-1\n"); } } } } int main(void){ run(); return 0; }