import sys readline=sys.stdin.readline import heapq from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item class Slope_Trick: def __init__(self,L=False,R=False,median=False,label=False,f=None,f_inve=None,e=None): self.queueL=[] self.queueR=[] self.L=L self.R=R self.median=median if self.median: self.median_value=None self.label=label self.shiftL=0 self.shiftR=0 self.min_f=0 if self.label: self.f=f self.f_inve=f_inve self.e=e self.labelL=self.e self.labelR=self.e def heappush(self,x): heappush(self.queueR,x-self.shiftR) if self.label: self.labelR=self.f(self.labelR,x) def heappop(self): x=heappop(self.queueR)+self.shiftR if self.label: self.labelR=self.f_inve(self.labelR,x) return x def heappushpop(self,x): y=heappushpop(self.queueR,x-self.shiftR)+self.shiftR if self.label: self.labelR=self.f(self.labelR,x) self.labelR=self.f_inve(self.labelR,y) return y def _heappush_max(self,x): _heappush_max(self.queueL,x-self.shiftL) if self.label: self.labelL=self.f(self.labelL,x) def _heappop_max(self): x=_heappop_max(self.queueL)+self.shiftL if self.label: self.labelL=self.f_inve(self.labelL,x) return x def _heappushpop_max(self,x): y=_heappushpop_max(self.queueL,x-self.shiftL)+self.shiftL if self.label: self.labelL=self.f(self.labelL,x) self.labelL=self.f_inve(self.labelL,y) return y def push_Left(self,x): if self.queueR: self.min_f+=max(x-(self.queueR[0]+self.shiftR),0) self._heappush_max(self.heappushpop(x)) def push_Right(self,x): if self.queueL: self.min_f+=max((self.queueL[0]+self.shiftL)-x,0) self.heappush(self._heappushpop_max(x)) def pop_Left(self): if self.queueL: retu=self._heappop_max() if self.queueR: self.min_f+=max(retu-(self.queueR[0]+self.shiftR),0) else: retu=None return retu def pop_Right(self): if self.queueR: retu=self.heappop() if self.queueL: self.min_f+=max((self.queueL[0]+self.shiftL)-retu,0) else: retu=None return retu def push(self,x): if self.L: if len(self.queueL)