#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(A) A.begin(),A.end()
#define bp '\n'
#define vp cout<<'\n';
const int MOD=1e9+7;
const int MNLL=-1e18;
const int MXLL=1e18;
const int N=505;
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n,l,r,mx=MNLL,sum=0;
cin>>n;
vector<pair<int,int>>a(n+1); a[0]={MNLL,MNLL};
vector<int>b(n+1,0);
for(int i=1;i<=n;++i){
cin>>l>>r;
a[i]=make_pair(l,r);
mx=max(mx,r);
}
sort(all(a));
for(int i=1;i<=n;++i){
b[i]=b[i-1]+a[i].second;
}
//for(int i=0;i<n;++i)cout<<"--> "<<a[i].first<<' '<<a[i].second<<bp;
l=1,r=n;
while(l<r){
sum=b[r]-b[l-1];
mx=max(mx,sum-(a[r].first-a[l].first));
//cout<<sum-(a[r].first-a[l].first)<<' '<<sum<<bp;
int tl,tr;
tl=((b[r]-b[l])-(a[r].first-a[l+1].first));
tr=((b[r-1]-b[l-1])-(a[r-1].first-a[l].first));
if(tl>tr)++l;
else --r;
}
cout<<mx;
}
/*
*/