#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define IS_INF(x) (std::isinf(x))
#define IS_NAN(x) (std::isnan(x))
#define fi first
#define se second
#define int long long
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define div ___div
#define prev ___prev
#define next ___next
#define left ___leftc
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define __Im_sogood__ main()
#define __builtin_popcount __builtin_popcountll
using namespace std;
const int MAXN = 5005;
const int INF = 1e18;
int n, m, Back[MAXN], First[MAXN], pre[MAXN]; pair<int, int> a[MAXN];
vector<int> compress;
int getIdx(int x) { return lower_bound(ALL(compress), x) - compress.begin(); }
void subtaskGeneral() {
FOR(i, 1, n) {
if(Back[a[i].first] == 0) { Back[a[i].first] = First[a[i].first] = i; }
else {
Back[a[i].first] = i;
}
}
int ans = 0, pref = compress[a[1].first] - pre[0];
REP(i, m) {
int segL = First[i], segR = Back[i];
ans = max(ans, pre[segR] - pre[segL - 1]);
}
ans = max(ans, a[1].second - compress[a[1].first]);
FOR(i, 2, n) {
ans = max(ans, pre[i] - compress[a[i].first] + pref);
pref = max(pref, compress[a[i].first] - pre[i - 1]);
}
cout << ans;
}
void solve() {
cin >> n; FOR(i, 1, n) cin >> a[i].first >> a[i].second; sort(a + 1, a + n + 1); FOR(i, 1, n) compress.push_back(a[i].first);
compress.erase(unique(ALL(compress)), compress.end());
m = (int)compress.size(); FOR(i, 1, n) a[i].first = getIdx(a[i].first); FOR(i, 1, n) pre[i] = pre[i - 1] + a[i].second;
subtaskGeneral();
}
__Im_sogood__{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}