Splay.
#include <cstdio>
#include <cstdlib>
int n,flag,ans;
struct Snode
{
int val;
Snode *Prev,*Ch[2];
};
struct Splay
{
Snode *NIL,*root,*pool;
Splay(int size)
{
pool=(Snode*)malloc(size*sizeof(Snode));
NIL=root=pool++;
NIL->Ch[0]=NIL->Ch[1]=NULL;
}
void rotate(Snode *x,bool type)
{
Snode *y=x->Prev,*z=y->Prev,*b=x->Ch[type^1];
b->Prev=y;
y->Prev=x;
x->Prev=z;
y->Ch[type]=b;
x->Ch[type^1]=y;
if(z->Ch[0]==y) z->Ch[0]=x;
if(z->Ch[1]==y) z->Ch[1]=x;
}
void splay(Snode *x,Snode *obj)
{
if(x==NIL) return ;
while(x->Prev!=obj){
Snode *y=x->Prev,*z=y->Prev;
if(z==NIL){
if(y->Ch[0]==x) rotate(x,0);
else rotate(x,1);
}
else{
if(z->Ch[0]==y&&y->Ch[0]==x){
rotate(y,0);
rotate(x,0);
}
else if(z->Ch[1]==y&&y->Ch[1]==x){
rotate(y,1);
rotate(x,1);
}
else if(z->Ch[1]==y){
rotate(x,0);
rotate(x,1);
}
else{
rotate(x,1);
rotate(x,0);
}
}
}
if(obj==NIL) root=x;
}
bool empty()
{
return root==NIL;
}
Snode* prev(Snode *p)
{
Snode *t=p->Ch[0];
while(t!=NIL&&t->Ch[1]!=NIL) t=t->Ch[1];
splay(t,NIL);
return t;
}
Snode* succ(Snode *p)
{
Snode *t=p->Ch[1];
while(t!=NIL&&t->Ch[0]!=NIL) t=t->Ch[0];
splay(t,NIL);
return t;
}
void insert(int k)
{
_insert(&root,k,NIL);
}
void _insert(Snode **p,int k,Snode *last)
{
while(1){
if(*p==NIL){
*p=pool++;
(*p)->val=k;
(*p)->Prev=last;
(*p)->Ch[0]=(*p)->Ch[1]=NIL;
splay(*p,NIL);
return ;
}
last=*p;
if(k<=(*p)->val) p=&(*p)->Ch[0];
else p=&(*p)->Ch[1];
}
}
void erase(Snode *p)
{
splay(p,NIL);
Snode *pr=prev(p),*su=succ(p);
if(pr!=NIL){
splay(p,NIL);
splay(pr,root);
pr->Ch[1]=root->Ch[1];
root->Ch[1]->Prev=pr;
pr->Prev=NIL;
root=pr;
}
else if(su!=NIL){
splay(p,NIL);
splay(su,root);
su->Ch[0]=root->Ch[0];
root->Ch[0]->Prev=su;
su->Prev=NIL;
root=su;
}
else root=NIL;
}
};
int main()
{
scanf("%d",&n);
Splay T(n<<1);
for(int i=1;i<=n;i++){
int f,v;
scanf("%d %d",&f,&v);
if(T.empty()){
flag=f;
T.insert(v);
}
else if(flag==f) T.insert(v);
else{
T.insert(v);
Snode *r=T.root,*pr=T.prev(r),*su=T.succ(r);
T.erase(r);
if(pr==T.NIL){
ans+=su->val-v;
T.erase(su);
}
else if(su==T.NIL){
ans+=v-pr->val;
T.erase(pr);
}
else if(v-pr->val>su->val-v){
ans+=su->val-v;
T.erase(su);
}
else{
ans+=v-pr->val;
T.erase(pr);
}
ans%=1000000;
}
}
printf("%d",ans);
return 0;
}