human
Solve in Python:
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...

Write me a Python code to calculate the maximum possible damage Polycarp may deal using the spells he knows?
assistant
#Here is Python code to calculate the maximum possible damage Polycarp may deal using the spells he knows.

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:
              ...