# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7; //924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourth; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum[700000], ans, dp[700000];
Pll a[700000], b[700000];
string str, ss;
bool f[4000][4000];
char c[4000][4000];
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<Edge>>vec;
vector<edge>ed;
vector<LL>v;
map<string, LL>ma;
set<LL>st;

map<LL, LL>zip;
LL unzip[700000];
void compress(vector<LL>x) {
	LL now = -1;
	cnt = 0;
	sort(x.begin(), x.end());
	rep(i, x.size()) {
		if (now != x[i]) {
			zip[x[i]] = cnt;
			unzip[cnt] = x[i];
			now = x[i];
			cnt++;
		}
	}
	cnt = 0;
}
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

int main() {
	cin >> n;
	rep(i, n) {
		cin >> a[i].fir >> a[i].sec;
	}
	sort(a,a+n);
	h = -1; cnt = -1;
	rep(i, n) {
		if (h != a[i].fir) {
			cnt++;
			b[cnt].fir = a[i].fir;
			b[cnt].sec += a[i].sec;
			h = a[i].fir;
		}
		else {
			b[cnt].sec += a[i].sec;
		}
	}
	n = cnt + 1;
	rep(i, n)v.push_back(b[i].fir), a[i] = b[i];
	compress(v);
	rep(i, n) {
		a[i].fir = zip[a[i].fir];
		sum[i + 1] += a[i].sec;
	}
	rep(i, n) {
		sum[i + 1] += sum[i];
		dp[i] = sum[i+1] - a[i].sec - unzip[a[i].fir];
	}
	LL l = INF;
	ans = a[0].sec;
	for (int i = 0; i < n; i++) {
		l = min(l, dp[i]);
		ans = max(ans, sum[i + 1] - l - unzip[a[i].fir]);
	}
	cout << ans << endl;
	return 0;
}

