#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define int ll
pair<int, int> a[500005];
int x[500005], y[500005];
int32_t main(){
cin.tie(0)->sync_with_stdio(false);
int n;
cin >> n;
for(int i=1; i<=n; i++)cin >> a[i].first >> a[i].second;
sort(a+1, a+n+1);
for(int i=1; i<=n; i++){
x[i]=x[i-1]+a[i].second;
y[i]=y[i-1]+a[i].second;
}
for(int i=1; i<=n; i++)x[i]-=a[i].first;
for(int i=0; i<n; i++)y[i]-=a[i+1].first;
int mn=y[0], ans=0;
for(int i=2; i<=n; i++){
mn=min(mn, y[i-1]);
ans=max(ans, x[i]-mn);
}
cout << ans;
return 0;
}