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

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = "#!/usr/bin/env python3\nimport sys\ninput = sys.stdin.readline\n\nclass SegmentTree:\n    def __init__(self, a):\n        # Operator\n        self.op = lambda a, b : a + b\n        # Identity element\n        self.e = 0 \n\n        self.n = len(a)\n        self.lv = (self.n - 1).bit_length()\n        self.size = 2**self.lv\n        self.data = [self.e] * (2*self.size - 1)\n        # Bisect checking function \n        self._check = lambda x, acc : acc >= x\n        self._acc = self.e\n\n        self.initialize(a)\n\n    # Initialize data\n    def initialize(self, a):\n        for i in range(self.n):\n            self.data[self.size + i - 1] = a[i]\n        for i in range(self.size-2, -1, -1):\n            self.data[i] = self.op(self.data[i*2 + 1], self.data[i*2 + 2])\n\n    # Update ak as x (0-indexed)\n    def update(self, k, x):\n        k += self.size - 1\n        self.data[k] = x\n        while k > 0:\n            k = (k - 1) // 2\n            self.data[k] = self.op(self.data[2*k+1], self.data[2*k+2])\n\n    # Min value in [l, r) (0-indexed)\n    def fold(self, l, r):\n        L = l + self.size; R = r + self.size\n        s = self.e\n        while L < R:\n            if R & 1:\n                R -= 1\n                s = self.op(s, self.data[R-1])\n            if L & 1:\n                s = self.op(s, self.data[L-1])\n                L += 1\n            L >>= 1; R >>= 1\n        return s\n\n    def _bisect_forward(self, x, start, k):\n        # When segment-k is at the bottom, accumulate and return.\n        if k >= self.size - 1:\n            self._acc = self.op(self._acc, self.data[k])\n            if self._check(x, self._acc):\n                return k - (self.size - 1)\n            else:\n                return -1\n        width = 2**(self.lv - (k+1).bit_length() + 1)\n        mid = (k+1) * width + width // 2 - self.size \n        # When left-child isn't in range, just look at right-child. \n        if mid <= start:\n            return self._bisect_forward(x, start, 2*k + 2)\n        # When segment-k is in range and has no answer in it, accumulate and return -1\n        tmp_acc = self.op(self._acc, self.data[k])\n        if start <= mid - width // 2 and not self._check(x, tmp_acc):\n            self._acc = tmp_acc\n            return -1\n        # Check left-child then right-child\n        vl = self._bisect_forward(x, start, 2*k + 1)\n        if vl != -1:\n            return vl\n        return self._bisect_forward(x, start, 2*k + 2)\n    \n    # Returns min index s.t. start <= index and satisfy check(data[start:idx)) = True\n    def bisect_forward(self, x, start=None):\n        if start:\n            ret = self._bisect_forward(x, start, 0)\n        else:\n            ret = self._bisect_forward(x, 0, 0)\n        self._acc = self.e\n        return ret\n\n    def _bisect_backward(self, x, start, k):\n        # When segment-k is at the bottom, accumulate and return.\n        if k >= self.size - 1:\n            self._acc = self.op(self._acc, self.data[k])\n            if self._check(x, self._acc):\n                return k - (self.size - 1)\n            else:\n                return -1\n        width = 2**(self.lv - (k+1).bit_length() + 1)\n        mid = (k+1) * width + width // 2 - self.size \n        # When right-child isn't in range, just look at right-child. \n        if mid >= start:\n            return self._bisect_backward(x, start, 2*k + 1)\n        # When segment-k is in range and has no answer in it, accumulate and return -1\n        tmp_acc = self.op(self._acc, self.data[k])\n        if start > mid + width // 2 and not self._check(x, tmp_acc):\n            self._acc = tmp_acc\n            return -1\n        # Check right-child then left-child\n        vl = self._bisect_backward(x, start, 2*k + 2)\n        if vl != -1:\n            return vl\n        return self._bisect_backward(x, start, 2*k + 1)\n    \n    # Returns max index s.t. index < start and satisfy check(data[idx:start)) = True\n    def bisect_backward(self, x, start=None):\n        if start:\n            ret = self._bisect_backward(x, start, 0)\n        else:\n            ret = self._bisect_backward(x, self.n, 0)\n        self._acc = self.e\n        return ret\n\nn = int(input())\nquery = []\nseen = set([0])\nfor _ in range(n):\n    kind, val = map(int, input().split())\n    query.append((kind, val))\n    if val > 0: \n        seen.add(val)\n\nunique = list(seen)\nunique.sort()\ncomp = {val: i for i, val in enumerate(unique)}\ndecomp = {i: val for i, val in enumerate(unique)}\ndecopm = {}\nnn = len(comp)\n\nbase = [0] * nn\nSTfire = SegmentTree(base)\nSTnum = SegmentTree(base)\nSTval = SegmentTree(base)\n\ntnum = 0\nfnum = 0\nspell = 0\ntotal = 0\nfor kind, val in query:\n    cd = comp[abs(val)]\n    if val > 0:\n        STval.update(cd, val)\n        STnum.update(cd, 1)\n        total += val\n        if kind == 1:\n            tnum += 1\n        else:\n            STfire.update(cd, 1)\n            fnum += 1\n    else:\n        total += val\n        STval.update(cd, 0)\n        STnum.update(cd, 0)\n        if kind == 1:\n            tnum -= 1\n        else:\n            STfire.update(cd, 0)\n            fnum -= 1\n    spell = tnum + fnum\n    if fnum == 0:\n        fid = -1\n    else:\n        fid = STfire.bisect_forward(fnum)\n    l = STnum.bisect_forward(spell - tnum)\n    if tnum == 0:\n        print(total)\n        continue\n    if fid >= l + 1:\n        double_total = STval.fold(l + 1, nn)\n        print(total + double_total)\n    else:\n        l = STnum.bisect_forward(spell - tnum + 1)\n        double_total = STval.fold(l + 1, nn)\n        if fnum > 0:\n            print(total + double_total + decomp[fid])\n        else:\n            print(total + double_total)"
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 18287