1 #include<algorithm>
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 #include<cmath>
6 //by NeighThorn
7 #define inf 0x3f3f3f3f
8 using namespace std;
9
10 const int maxn=80000+5,MOD=1000000;
11
12 int n,a,b,ans,cnt;
13 int root,tot,w[maxn],fa[maxn],ls[maxn],rs[maxn];
14
15 inline void zig(int x){
16 int y=fa[x];
17 if(rs[x])
18 fa[rs[x]]=y,ls[y]=rs[x];
19 else
20 ls[y]=0;
21 fa[x]=fa[y];
22 if(fa[x]){
23 if(ls[fa[x]]==y)
24 ls[fa[x]]=x;
25 else
26 rs[fa[x]]=x;
27 }
28 fa[y]=x;rs[x]=y;
29 }
30
31 inline void zag(int x){
32 int y=fa[x];
33 if(ls[x])
34 fa[ls[x]]=y,rs[y]=ls[x];
35 else
36 rs[y]=0;
37 fa[x]=fa[y];
38 if(fa[x]){
39 if(ls[fa[x]]==y)
40 ls[fa[x]]=x;
41 else
42 rs[fa[x]]=x;
43 }
44 fa[y]=x,ls[x]=y;
45 }
46
47 inline void splay(int x,int z){
48 while(fa[x]!=z){
49 int y=fa[x];
50 if(fa[y]==z){
51 if(ls[y]==x)
52 zig(x);
53 else
54 zag(x);
55 }
56 else{
57 if(ls[fa[y]]==y){
58 if(ls[y]==x)
59 zig(y),zig(x);
60 else
61 zag(x),zig(x);
62 }
63 else{
64 if(rs[y]==x)
65 zag(y),zag(x);
66 else
67 zig(x),zag(x);
68 }
69 }
70 }
71 if(!z)
72 root=x;
73 }
74
75 inline void ins(int rt,int x){
76 if(rt==0){
77 w[++tot]=x;root=tot;
78 return;
79 }
80 if(w[rt]==x)
81 return;
82 else if(x<w[rt]){
83 if(!ls[rt])
84 w[++tot]=x,ls[rt]=tot,fa[tot]=rt,splay(tot,0);
85 else
86 ins(ls[rt],x);
87 }
88 else{
89 if(!rs[rt])
90 w[++tot]=x,rs[rt]=tot,fa[tot]=rt,splay(tot,0);
91 else
92 ins(rs[rt],x);
93 }
94 }
95
96 inline void del(int rt,int x){
97 if(w[rt]==x){
98 splay(rt,0);
99 int l=ls[rt],r=rs[rt];
100 if(!l)
101 root=r,fa[r]=0;
102 else{
103 while(rs[l])
104 l=rs[l];
105 splay(l,rt);
106 root=l,fa[l]=0,rs[l]=rs[rt],fa[rs[rt]]=l;
107 }
108 }
109 else if(x<w[rt])
110 del(ls[rt],x);
111 else
112 del(rs[rt],x);
113 }
114
115 inline int pre(int rt,int x){
116 int res=-inf;
117 while(rt){
118 if(x==w[rt])
119 return x;
120 else{
121 if(w[rt]<x)
122 res=max(res,w[rt]),rt=rs[rt];
123 else
124 rt=ls[rt];
125 }
126 }
127 return res;
128 }
129
130 inline int suf(int rt,int x){
131 int res=inf;
132 while(rt){
133 if(x==w[rt])
134 return x;
135 else{
136 if(w[rt]>x)
137 res=min(res,w[rt]),rt=ls[rt];
138 else
139 rt=rs[rt];
140 }
141 }
142 return res;
143 }
144
145 signed main(void){
146 memset(ls,0,sizeof(ls));
147 memset(rs,0,sizeof(rs));
148 memset(fa,0,sizeof(fa));
149 scanf("%d",&n);tot=root=ans=cnt=0;
150 for(int i=1;i<=n;i++){
151 scanf("%d%d",&a,&b);
152 if(a==0){
153 if(cnt>=0)
154 ins(root,b);
155 else{
156 int lala1=pre(root,b),lala2=suf(root,b);
157 if(abs(lala1-b)<=abs(lala2-b))
158 (ans+=abs(lala1-b))%=MOD,del(root,lala1);
159 else
160 (ans+=abs(lala2-b))%=MOD,del(root,lala2);
161 }
162 cnt++;
163 }
164 else{
165 if(cnt<=0)
166 ins(root,b);
167 else{
168 int lala1=pre(root,b),lala2=suf(root,b);
169 if(abs(lala1-b)<=abs(lala2-b))
170 (ans+=abs(lala1-b))%=MOD,del(root,lala1);
171 else
172 (ans+=abs(lala2-b))%=MOD,del(root,lala2);
173 }
174 cnt--;
175 }
176 }
177 printf("%d\n",ans);
178 return 0;
179 }