#include <bits/stdc++.h>
using namespace std;
class fastIO{private:char ibuf[1000007],*p1=ibuf,*p2=ibuf,obuf[1000007],*p3=obuf,sta[50];bool file_end=false;char get(){return p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,1000007,stdin),p1==p2)?(file_end=true),EOF:*p1++;}void put(char x){p3-obuf<1000007?*p3++=x:(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x);}public:explicit operator bool(){return!file_end;}size_t flush(){size_t f=fwrite(obuf,p3-obuf,1,stdout);p3=obuf;*p3=0;return f;}fastIO&operator>>(char&t){for(t=get();!isgraph(t);t=get());return*this;}template<typename any>typename std::enable_if<std::is_same<any,char>::value,any>::type tpval(){char t;for(t=get();!isgraph(t);t=get());return t;}fastIO&operator>>(char*t){char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())*t=c,t++;*t=0;return*this;}fastIO&operator>>(std::string&t){t.clear();char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())t+=c;return*this;}template<typename any>typename std::enable_if<std::is_same<any,std::string>::value,any>::type tpval(){std::string t;char c;for(c=get();!isgraph(c);c=get());for(;isgraph(c);c=get())t+=c;return t;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,fastIO>::type&operator>>(any&t){t=0;bool y=0;char c=get();for(;!isdigit(c);c=get())if(c==45)y=true;for(;isdigit(c);c=get())t=(t<<3)+(t<<1)+(c^48);if(y==1)t=-t;return*this;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,any>::type tpval(){any t=0;bool y=0;char c=get();for(;!isdigit(c);c=get())if(c==45)y=true;for(;isdigit(c);c=get())t=(t<<3)+(t<<1)+(c^48);if(y==1)t=-t;return t;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value||std::is_same<any,__uint128_t>::value),fastIO>::type&operator>>(any&t){t=0;char c=get();for(;!isdigit(c);c=get());for(;isdigit(c);c=get())t=(t<<3)+(t<<1)+(c^48);return*this;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__uint128_t>::value,any>::type tpval(){any t=0;char c=get();for(;!isdigit(c);c=get());for(;isdigit(c);c=get())t=(t<<3)+(t<<1)+(c^48);return t;}template<typename any1,typename any2>fastIO&operator>>(std::pair<any1,any2>&t){return*this>>t.first>>t.second;}fastIO&operator<<(const char t){put(t);return*this;}fastIO&operator<<(const char*t){for(;*t;t++)put(*t);return*this;}fastIO&operator<<(const std::string&t){for(const char it:t)put(it);return*this;}template<typename any>typename std::enable_if<(std::is_signed<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value)||std::is_same<any,__int128_t>::value,fastIO>::type&operator<<(any t){if(!t){put(48);return*this;}int len=0;if(t<0)t=-t,put(45);while(t)sta[len++]=t%10+48,t/=10;while(len--)put(sta[len]);return*this;}template<typename any>typename std::enable_if<(std::is_unsigned<any>::value&&std::is_integral<any>::value&&!std::is_same<any,char>::value||std::is_same<any,__uint128_t>::value),fastIO>::type&operator<<(any t){if(!t){put(48);return*this;}int len=0;while(t)sta[len++]=t%10+48,t/=10;while(len--)put(sta[len]);return*this;}template<typename any1,typename any2>fastIO&operator<<(const std::pair<any1,any2>&t){return*this<<t.first<<' '<<t.second;}~fastIO(){fwrite(obuf,p3-obuf,1,stdout);}}fio;
const int N = (int) 2e5 + 233;
struct Node { int l, r, key, val, cnt, size; } tr[N];
const int INF = 1e8;
int n;
namespace Treap1 {
int root, idx;
void pushup(int p) { tr[p].size = tr[tr[p].l].size + tr[tr[p].r].size + tr[p].cnt; }
int get_node(int key) { tr[++ idx].key = key, tr[idx].val = rand(), tr[idx].cnt = tr[idx].size = 1; return idx; }
void build() { get_node(-INF), get_node(INF); root = 1, tr[1].r = 2; pushup(root); }
void zig(int &p) { int q = tr[p].l; tr[p].l = tr[q].r, tr[q].r = p, p = q; pushup(tr[p].r), pushup(p); }
void zag(int &p) { int q = tr[p].r; tr[p].r = tr[q].l, tr[q].l = p, p = q; pushup(tr[p].l), pushup(p); }
void insert(int &p, int key) {
if (!p) p = get_node(key);
else if (tr[p].key == key) tr[p].cnt ++;
else if (tr[p].key > key) { insert(tr[p].l, key); if (tr[tr[p].l].val > tr[p].val) zig(p); }
else { insert(tr[p].r, key); if (tr[tr[p].r].val > tr[p].val) zag(p); }
pushup(p);
} void remove(int &p, int key) {
if (!p) return ;
if (tr[p].key == key) { if (tr[p].cnt > 1) tr[p].cnt --; else if (tr[p].l || tr[p].r) { if (tr[p].cnt > 1) tr[p].cnt --; else if (tr[p].l || tr[p].r) if (!tr[p].r || tr[tr[p].l].val > tr[tr[p].r].val) { zig(p); remove(tr[p].r, key); } else { zag(p); remove(tr[p].l, key); } } else p = 0; }
else if (tr[p].key > key) remove(tr[p].l, key);
else remove(tr[p].r, key);
pushup(p);
} int grbk(int &p, int key) {
if (!p) return 0;
if (tr[p].key == key) return tr[tr[p].l].size + 1;
if (tr[p].key > key) return grbk(tr[p].l, key);
return tr[tr[p].l].size + tr[p].cnt + grbk(tr[p].r, key);
} int gkbr(int &p, int rank) {
if (!p) return INF;
if (tr[tr[p].l].size >= rank) return gkbr(tr[p].l, rank);
if (tr[tr[p].l].size + tr[p].cnt >= rank) return tr[p].key;
return gkbr(tr[p].r, rank - tr[tr[p].l].size - tr[p].cnt);
} int gp(int &p, int key) {
if (!p) return -INF;
if (tr[p].key >= key) return gp(tr[p].l, key);
return max(tr[p].key, gp(tr[p].r, key));
} int gn(int &p, int key) {
if (!p) return INF;
if (tr[p].key <= key) return gn(tr[p].r, key);
return min(tr[p].key, gn(tr[p].l, key));
}
} namespace Treap2 {
struct Node { int l, r, key, val, cnt, size; } tr[N];
int root, idx;
void pushup(int p) { tr[p].size = tr[tr[p].l].size + tr[tr[p].r].size + tr[p].cnt; }
int get_node(int key) { tr[++ idx].key = key, tr[idx].val = rand(), tr[idx].cnt = tr[idx].size = 1; return idx; }
void build() { get_node(-INF), get_node(INF); root = 1, tr[1].r = 2; pushup(root); }
void zig(int &p) { int q = tr[p].l; tr[p].l = tr[q].r, tr[q].r = p, p = q; pushup(tr[p].r), pushup(p); }
void zag(int &p) { int q = tr[p].r; tr[p].r = tr[q].l, tr[q].l = p, p = q; pushup(tr[p].l), pushup(p); }
void insert(int &p, int key) {
if (!p) p = get_node(key);
else if (tr[p].key == key) tr[p].cnt ++;
else if (tr[p].key > key) { insert(tr[p].l, key); if (tr[tr[p].l].val > tr[p].val) zig(p); }
else { insert(tr[p].r, key); if (tr[tr[p].r].val > tr[p].val) zag(p); }
pushup(p);
} void remove(int &p, int key) {
if (!p) return ;
if (tr[p].key == key) { if (tr[p].cnt > 1) tr[p].cnt --; else if (tr[p].l || tr[p].r) { if (tr[p].cnt > 1) tr[p].cnt --; else if (tr[p].l || tr[p].r) if (!tr[p].r || tr[tr[p].l].val > tr[tr[p].r].val) { zig(p); remove(tr[p].r, key); } else { zag(p); remove(tr[p].l, key); } } else p = 0; }
else if (tr[p].key > key) remove(tr[p].l, key);
else remove(tr[p].r, key);
pushup(p);
} int grbk(int &p, int key) {
if (!p) return 0;
if (tr[p].key == key) return tr[tr[p].l].size + 1;
if (tr[p].key > key) return grbk(tr[p].l, key);
return tr[tr[p].l].size + tr[p].cnt + grbk(tr[p].r, key);
} int gkbr(int &p, int rank) {
if (!p) return INF;
if (tr[tr[p].l].size >= rank) return gkbr(tr[p].l, rank);
if (tr[tr[p].l].size + tr[p].cnt >= rank) return tr[p].key;
return gkbr(tr[p].r, rank - tr[tr[p].l].size - tr[p].cnt);
} int gp(int &p, int key) {
if (!p) return -INF;
if (tr[p].key >= key) return gp(tr[p].l, key);
return max(tr[p].key, gp(tr[p].r, key));
} int gn(int &p, int key) {
if (!p) return INF;
if (tr[p].key <= key) return gn(tr[p].r, key);
return min(tr[p].key, gn(tr[p].l, key));
}
}
multiset <int> se, se2;
signed main() {
Treap1::build(), Treap2::build();
fio >> n;
int _1 = 0, _2 = 0, ans = 0;
while (n --) {
int opt;
fio >> opt;
if (opt == 0) {
int x;
fio >> x;
if (_2) {
_2 --;
if (!se2.count(x)) {
int x1 = Treap2::gp(Treap2::root, x);
int x2 = Treap2::gn(Treap2::root, x);
if (abs(x - x1) <= abs(x - x2)) {
ans += abs(x - x1);
se2.erase(se.find(x1));
Treap2::remove(Treap2::root, x1);
} else {
ans += abs(x - x2);
se2.erase(se.find(x2));
Treap2::remove(Treap2::root, x2);
}
} else {
se2.erase(se.find(x));
Treap2::remove(Treap2::root, x);
}
} else {
se.insert(x);
Treap1::insert(Treap1::root, x);
_1 ++;
}
} else {
int x;
fio >> x;
if (_1) {
_1 --;
if (!se.count(x)) {
int x1 = Treap1::gp(Treap1::root, x);
int x2 = Treap1::gn(Treap1::root, x);
if (abs(x - x1) <= abs(x - x2)) {
ans += abs(x - x1);
se.erase(se.find(x1));
Treap1::remove(Treap1::root, x1);
} else {
ans += abs(x - x2);
se.erase(se.find(x2));
Treap1::remove(Treap1::root, x2);
}
} else {
se.erase(se.find(x));
Treap1::remove(Treap1::root, x);
}
} else {
_2 ++;
se.insert(x);
Treap2::insert(Treap2::root, x);
}
}
}
fio << ans << '\n';
return 0;
}