#include <bits/stdc++.h>   using namespace std;   using i64 = long long;   struct SplayNode {     using pSplayNode = SplayNode *;     static pSplayNode root;     int sz;     pSplayNode fa, son[2];     int val, type, type1num;     i64 sum;     SplayNode(int _type, int _val) {       sz = 1;       fa = son[0] = son[1] = nullptr;       type = _type;       type1num = type;       sum = val = _val;     }     void Pushup() {       sz = (son[0] != nullptr ? son[0]->sz : 0) +            (son[1] != nullptr ? son[1]->sz : 0) + 1;       sum = (son[0] != nullptr ? son[0]->sum : 0) +             (son[1] != nullptr ? son[1]->sum : 0) + val;       type1num = (son[0] != nullptr ? son[0]->type1num : 0) +                  (son[1] != nullptr ? son[1]->type1num : 0) + type;     }     void Rotate() {       if (fa == nullptr) return;       pSplayNode x = this, y = fa, z = y->fa;       int p = y->son[1] == x ? 1 : 0;       y->son[p] = x->son[p ^ 1];       if (x->son[p ^ 1] != nullptr) x->son[p ^ 1]->fa = y;       x->son[p ^ 1] = y;       y->fa = x;       x->fa = z;       if (z != nullptr) z->son[y == z->son[1] ? 1 : 0] = x;       y->Pushup();       x->Pushup();     }     void Splay(pSplayNode goal = root) {       if (this == goal) return;       pSplayNode x = this, y, z;       while ((y = x->fa) != goal) {         z = y->fa;         if (z != goal)           ((z->son[0] == y) ^ (y->son[0] == x)) ? x->Rotate() : y->Rotate();         x->Rotate();       }       x->Rotate();       if (goal == root) root = x;     }     pSplayNode GetKth(int k) {       int _k = son[0] == nullptr ? 1 : son[0]->sz + 1;       if (k == _k) return this;       if (k < _k) return son[0]->GetKth(k);       return son[1]->GetKth(k - _k);     }     pSplayNode Split(int l, int r) {       root->GetKth(l)->Splay();       root->GetKth(r + 2)->Splay(root->son[1]);       return root->son[1]->son[0];     }     void Insert(pSplayNode x) {       if (x->val > val || x->val == val && x->type < type) {         if (son[0] == nullptr) {           son[0] = x;           x->fa = this;           x->Splay();         } else           son[0]->Insert(x);       } else {         if (son[1] == nullptr) {           son[1] = x;           x->fa = this;           x->Splay();         } else           son[1]->Insert(x);       }     }     pSplayNode GetVal(int _t, int _v) {       if (_v == val && _t == type) return this;       if (_v > val || _v == val && _t < type) return son[0]->GetVal(_t, _v);       return son[1]->GetVal(_t, _v);     }     void Delete() {       Splay();       if (root->son[0] != nullptr) root->son[0]->fa = nullptr;       if (root->son[1] != nullptr) root->son[1]->fa = nullptr;       root = Union(root->son[0], root->son[1]);       delete this;     }     pSplayNode Union(pSplayNode a, pSplayNode b) {       if (a == nullptr) return b;       auto c = Union(a->son[1], b);       a->son[1] = c;       if (c != nullptr) c->fa = a;       a->Pushup();       return a;     }   } *SplayNode::root = nullptr, *&Root = SplayNode::root;   int q, i, o, x, num1;   i64 sumall;   i64 Query() {     if (num1 == 0) return sumall;     auto rg = Root->Split(1, num1);     if (rg->type1num < num1) return sumall + rg->sum;     if (Root->sz - 2 == num1)       return sumall + rg->sum - (Root->GetKth(num1 + 1)->val);     return sumall + rg->sum - (Root->GetKth(num1 + 1)->val) +            (Root->GetKth(num1 + 2)->val);   }   int main() {     default_random_engine gen;     Root = new SplayNode(0, numeric_limits<int>::max());     auto tmp = new SplayNode(0, numeric_limits<int>::min());     tmp->fa = Root;     Root->son[1] = tmp;     Root->Pushup();     scanf( %d , &q);     for (i = 1; i <= q; ++i) {       scanf( %d%d , &o, &x);       sumall += x;       if (x > 0) {         num1 += o;         Root->Insert(new SplayNode(o, x));       } else {         num1 -= o;         Root->GetVal(o, -x)->Delete();       }       printf( %lld n , Query());       Root->GetKth(gen() % (Root->sz) + 1)->Splay();     }   }