Two Types of Spells
Contest: Educational Codeforces Round 93 (Rated for Div. 2)
Difficulty rating: 2200
Tags: binary search, data structures, greedy, implementation, math, sortings

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 format:
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 format:
After each change, print the maximum damage Polycarp can deal with his current set of spells.

Constraints:
Time limit: 3.5 s
Memory limit: 256.0 MB

Example input:
6
1 5
0 10
1 -5
0 5
1 11
0 -10

Example output:
5
25
10
15
36
21

Editorial:
Let's solve this problem for fixed set of spells. For example, we have a fireball spells with powers $$$f_1, f_2, \dots , f_m$$$ and lighting spells with powers $$$l_1, l_2, \dots, l_k$$$. We reach the maximum total damage if we can double all $$$k$$$ spells with maximum damage. It's possibly iff the set of $$$k$$$ largest by power spell (let's denote this set as $$$s$$$) contains at least one fireball spell. Otherwise (if set $$$s$$$ contains only lightning spells) the maximum damage reach when we double $$$k-1$$$ largest spells in set $$$s$$$ and one largest spell not from set $$$s$$$ (if such spell exist).
Now how do you solve the original problem when spells are added and removed? All we have to do it maintain the set of $$$x$$$ largest by power spells (where $$$x$$$ is current number of lightning spells), and change this set by adding or removing one spell. Also you have to maintain the sum of spells power in set this set and the number of fireball spells in this set. You can do it by set in C++ or TreeSet on Java.