#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n;
long long ans,cnt;
struct node{
int v;
long long h;
}fsz[N];
long long a[N],b[N];//分别存储发射站从左侧和右侧接收的能量
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-'){t=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getchar();}
return x*t;
}
int main()
{
stack<node> s1;
stack<node> s2;
n=read();
for(int i=1;i<=n;i++)
fsz[i].h=read(),fsz[i].v=read();
//当栈内的元素即将压入的元素高于已经入栈的元素,那么它可以收到栈内比它矮的发射站的能量
for(int i=1;i<=n;i++)//从左到右维护一个单调递减的栈,计算一个发射站从左侧能收到多少能量
{
if(s1.empty()||fsz[i].h<=s1.top().h)//当栈为空或入栈元素小于之前的元素,直接入栈
{
s1.push(fsz[i]);//入栈
a[i]=0;//最矮的发射站收不到任何能量
}
else//发射站高于之前的发射站
{
cnt=0;//记录该发射站能从比它矮的发射站中收到多少能量
while(!s1.empty()&&fsz[i].h>s1.top().h)//当栈不为空并且入栈元素高于栈顶元素
{
cnt+=s1.top().v;//该发射站能够收到栈顶元素的能量
s1.pop();//将栈顶元素弹出
}
a[i]=cnt;//将从左侧收到的总能量存储起来
s1.push(fsz[i]);//入栈
}
}
for(int i=n;i>=1;i--)//从右到左维护一个单调递减的栈,计算一个发射站从右侧能收到多少能量
{
if(s2.empty()||fsz[i].h<=s2.top().h)
{
s2.push(fsz[i]);
b[i]=0;
}
else
{
cnt=0;
while(!s2.empty()&&fsz[i].h>s2.top().h)
{
cnt+=s2.top().v;
s2.pop();
}
b[i]=cnt;
s2.push(fsz[i]);
}
}
for(int i=1;i<=n;i++)
ans=max(ans,a[i]+b[i]);//答案为从左侧和右侧收到的能量总数最多的
cout<<ans;
return 0;
}