structHeap{constexprstaticintmax_size=5e5;intheap_size=0;intnode[max_size];intparent(intv){return(v-1)/2;}intlch(intv){return(v<<1)+1;}intrch(intv){return(v<<1)+2;}voidheapify(introot){intcur=root,tmp=node[root];while(cur<heap_size&&lch(cur)<heap_size){intl=lch(cur),r=rch(cur),mn_idx=-1;if(r<heap_size)mn_idx=node[l]<node[r]?l:r;elsemn_idx=l;if(node[mn_idx]<tmp){node[cur]=node[mn_idx];cur=mn_idx;}elsebreak;}node[cur]=tmp;}voidinit(int*arr,intn){heap_size=n;copy(arr,arr+n,node);// the last "non-leaf node" is the parent of the last node// parent(n-1) = ((n-1) - 1) / 2 = n / 2 - 1for(inti=n/2-1;i>=0;i--)heapify(i);}voidpush(intkey){node[heap_size++]=key;intcur=heap_size-1;while(cur!=0&&node[parent(cur)]>node[cur]){swap(node[cur],node[parent(cur)]);cur=parent(cur);}}intpop(){intret=node[0];node[0]=node[--heap_size];heapify(0);returnret;}intpeek(){returnnode[0];}};