#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
#define MAX_N 500000
struct goods {
	ll val, siz;
};


bool cmp(const goods& a, const goods& b)
{
	return a.siz < b.siz;
}


goods g[MAX_N + 1];
//ll cs[MAX_N + 1];
int main() {
	ll S;
	ll ANS;
	int N;
	cin >> N;
	for (int i = 1; i <= N; i++) {
		cin >> g[i].siz >> g[i].val;
	}

	/*
	for (int i = 1; i <= N-1; i++) {
		for (int j = i + 1; j <= N; j++) {
			if (g[i].siz > g[j].siz) {
				goods temp;
				temp = g[i];
				g[i] = g[j];
				g[j] = temp;
			}
		}
	}
	*/

	sort(g + 1, g + N + 1, cmp);

	S = g[1].val + g[N].val;
	ANS = S - (g[N].siz - g[1].siz);
//	cs[0] = 0;
//	for (int i = 1; i <= N; i++) {
//		cs[i] = cs[i - 1] + g[i].val;
//	}
	ll SUM = 0;
	ll MIN = 0xffffffffffff;
	for (int i = 1; i <= N; i++) {
		SUM = SUM + g[i].val;
		if (SUM - g[i].val - g[i].siz < MIN)
			MIN = SUM - g[i].val - g[i].siz;
		ANS = max(ANS, SUM - g[i].siz - MIN);
	}
	cout << ANS << endl;
	return 0;
}
