Polycarp plays a computer game (yet again). In this game, he fights monsters using magic spells.

There are two types of spells: fire spell of power $x$ deals $x$ damage to the monster, and lightning spell of power $y$ deals $y$ damage to the monster and doubles the damage of the next spell Polycarp casts. Each spell can be cast only once per battle, but Polycarp can cast them in any order.

For example, suppose that Polycarp knows three spells: a fire spell of power $5$, a lightning spell of power $1$, and a lightning spell of power $8$. There are $6$ ways to choose the order in which he casts the spells:

  first, second, third. This order deals $5 + 1 + 2 \cdot 8 = 22$ damage;  first, third, second. This order deals $5 + 8 + 2 \cdot 1 = 15$ damage;  second, first, third. This order deals $1 + 2 \cdot 5 + 8 = 19$ damage;  second, third, first. This order deals $1 + 2 \cdot 8 + 2 \cdot 5 = 27$ damage;  third, first, second. This order deals $8 + 2 \cdot 5 + 1 = 19$ damage;  third, second, first. This order deals $8 + 2 \cdot 1 + 2 \cdot 5 = 20$ damage. 

Initially, Polycarp knows $0$ spells. His spell set changes $n$ times, each time he either learns a new spell or forgets an already known one. After each change, calculate the maximum possible damage Polycarp may deal using the spells he knows.


-----Input-----

The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of changes to the spell set.

Each of the next $n$ lines contains two integers $tp$ and $d$ ($0 \le tp_i \le 1$; $-10^9 \le d \le 10^9$; $d_i \neq 0$) — the description of the change. If $tp_i$ if equal to $0$, then Polycarp learns (or forgets) a fire spell, otherwise he learns (or forgets) a lightning spell.

If $d_i > 0$, then Polycarp learns a spell of power $d_i$. Otherwise, Polycarp forgets a spell with power $-d_i$, and it is guaranteed that he knew that spell before the change.

It is guaranteed that the powers of all spells Polycarp knows after each change are different (Polycarp never knows two spells with the same power).


-----Output-----

After each change, print the maximum damage Polycarp can deal with his current set of spells.


-----Example-----
Input
6
1 5
0 10
1 -5
0 5
1 11
0 -10

Output
5
25
10
15
36
21

Difficulty: interview

Starter code:

Solutions:
class BIT():
    def __init__(self,n):
        self.BIT=[0]*(n+1)
        self.num=n

    def query(self,idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            idx -= idx&(-idx)
        return res_sum

    #Ai += x O(logN)
    def update(self,idx,x):
        while idx <= self.num:
            self.BIT[idx] += x
            idx += idx&(-idx)
        return

import sys,heapq,random

input=sys.stdin.readline

n=int(input())
spell=[tuple(map(int,input().split())) for i in range(n)]


S=set([])
for i in range(n):
    S.add(abs(spell[i][1]))
S=list(S)
S.sort(reverse=True)
comp={i:e+1 for e,i in enumerate(S)}
N=len(S)

x_exist=BIT(N)
y_exist=BIT(N)
power=BIT(N)

X,Y,S=0,0,0
Xmax=[]
Ymin=[]
x_data=[0]*(N+1)
y_data=[0]*(N+1)

for i in range(n):
    t,d=spell[i]
    S+=d
    if d<0:
        id=comp[-d]
        if t==0:
            X-=1
            x_exist.update(id,-1)
            power.update(id,d)
            x_data[id]-=1
        else:
            Y-=1
            y_exist.update(id,-1)
            power.update(id,d)
            y_data[id]-=1
    else:
        id=comp[d]
        if t==0:
            X+=1
            x_exist.update(id,1)
            power.update(id,d)
            heapq.heappush(Xmax,-d)
            x_data[id]+=1
        else:
            Y+=1
            y_exist.update(id,1)
            power.update(id,d)
            heapq.heappush(Ymin,d)
            y_data[id]+=1
    if X==0:
        if Y==0:
            print(0)
        else:
            while not y_data[comp[Ymin[0]]]:
                heapq.heappop(Ymin)
            print(2*S-Ymin[0])
    else:
        if Y==0:
            print(S)
        else:
            start=0
            end=N
            while end-start>1:
                test=(end+start)//2
                if x_exist.query(test)+y_exist.query(test)<=Y:
                    start=test
                else:
                    end=test
            if y_exist.query(start)!=Y:
                print(S+power.query(start))
            else:
                while not y_data[comp[Ymin[0]]]:
                    heapq.heappop(Ymin)
                while not x_data[comp[-Xmax[0]]]:
                    heapq.heappop(Xmax)
                print(S+power.query(start)-Ymin[0]-Xmax[0])


#!/usr/bin/env python3
import sys
input = sys.stdin.readline

class SegmentTree:
    def __init__(self, a):
        # Operator
        self.op = lambda a, b : a + b
        # Identity element
        self.e = 0 

        self.n = len(a)
        self.lv = (self.n - 1).bit_length()
        self.size = 2**self.lv
        self.data = [self.e] * (2*self.size - 1)
        # Bisect checking function 
        self._check = lambda x, acc : acc >= x
        self._acc = self.e

        self.initialize(a)

    # Initialize data
    def initialize(self, a):
        for i in range(self.n):
            self.data[self.size + i - 1] = a[i]
        for i in range(self.size-2, -1, -1):
            self.data[i] = self.op(self.data[i*2 + 1], self.data[i*2 + 2])

    # Update ak as x (0-indexed)
    def update(self, k, x):
        k += self.size - 1
        self.data[k] = x
        while k > 0:
            k = (k - 1) // 2
            self.data[k] = self.op(self.data[2*k+1], self.data[2*k+2])

    # Min value in [l, r) (0-indexed)
    def fold(self, l, r):
        L = l + self.size; R = r + self.size
        s = self.e
        while L < R:
            if R & 1:
                R -= 1
                s = self.op(s, self.data[R-1])
            if L & 1:
                s = self.op(s, self.data[L-1])
                L += 1
            L >>= 1; R >>= 1
        return s

    def _bisect_forward(self, x, start, k):
        # When segment-k is at the bottom, accumulate and return.
        if k >= self.size - 1:
            self._acc = self.op(self._acc, self.data[k])
            if self._check(x, self._acc):
                return k - (self.size - 1)
            else:
                return -1
        width = 2**(self.lv - (k+1).bit_length() + 1)
        mid = (k+1) * width + width // 2 - self.size 
        # When left-child isn't in range, just look at right-child. 
        if mid <= start:
            return self._bisect_forward(x, start, 2*k + 2)
        # When segment-k is in range and has no answer in it, accumulate and return -1
        tmp_acc = self.op(self._acc, self.data[k])
        if start <= mid - width // 2 and not self._check(x, tmp_acc):
            self._acc = tmp_acc
            return -1
        # Check left-child then right-child
        vl = self._bisect_forward(x, start, 2*k + 1)
        if vl != -1:
            return vl
        return self._bisect_forward(x, start, 2*k + 2)
    
    # Returns min index s.t. start <= index and satisfy check(data[start:idx)) = True
    def bisect_forward(self, x, start=None):
        if start:
            ret = self._bisect_forward(x, start, 0)
        else:
            ret = self._bisect_forward(x, 0, 0)
        self._acc = self.e
        return ret

    def _bisect_backward(self, x, start, k):
        # When segment-k is at the bottom, accumulate and return.
        if k >= self.size - 1:
            self._acc = self.op(self._acc, self.data[k])
            if self._check(x, self._acc):
                return k - (self.size - 1)
            else:
                return -1
        width = 2**(self.lv - (k+1).bit_length() + 1)
        mid = (k+1) * width + width // 2 - self.size 
        # When right-child isn't in range, just look at right-child. 
        if mid >= start:
            return self._bisect_backward(x, start, 2*k + 1)
        # When segment-k is in range and has no answer in it, accumulate and return -1
        tmp_acc = self.op(self._acc, self.data[k])
        if start > mid + width // 2 and not self._check(x, tmp_acc):
            self._acc = tmp_acc
            return -1
        # Check right-child then left-child
        vl = self._bisect_backward(x, start, 2*k + 2)
        if vl != -1:
            return vl
        return self._bisect_backward(x, start, 2*k + 1)
    
    # Returns max index s.t. index < start and satisfy check(data[idx:start)) = True
    def bisect_backward(self, x, start=None):
        if start:
            ret = self._bisect_backward(x, start, 0)
        else:
            ret = self._bisect_backward(x, self.n, 0)
        self._acc = self.e
        return ret

n = int(input())
query = []
seen = set([0])
for _ in range(n):
    kind, val = map(int, input().split())
    query.append((kind, val))
    if val > 0: 
        seen.add(val)

unique = list(seen)
unique.sort()
comp = {val: i for i, val in enumerate(unique)}
decomp = {i: val for i, val in enumerate(unique)}
decopm = {}
nn = len(comp)

base = [0] * nn
STfire = SegmentTree(base)
STnum = SegmentTree(base)
STval = SegmentTree(base)

tnum = 0
fnum = 0
spell = 0
total = 0
for kind, val in query:
    cd = comp[abs(val)]
    if val > 0:
        STval.update(cd, val)
        STnum.update(cd, 1)
        total += val
        if kind == 1:
            tnum += 1
        else:
            STfire.update(cd, 1)
            fnum += 1
    else:
        total += val
        STval.update(cd, 0)
        STnum.update(cd, 0)
        if kind == 1:
            tnum -= 1
        else:
            STfire.update(cd, 0)
            fnum -= 1
    spell = tnum + fnum
    if fnum == 0:
        fid = -1
    else:
        fid = STfire.bisect_forward(fnum)
    l = STnum.bisect_forward(spell - tnum)
    if tnum == 0:
        print(total)
        continue
    if fid >= l + 1:
        double_total = STval.fold(l + 1, nn)
        print(total + double_total)
    else:
        l = STnum.bisect_forward(spell - tnum + 1)
        double_total = STval.fold(l + 1, nn)
        if fnum > 0:
            print(total + double_total + decomp[fid])
        else:
            print(total + double_total)