WA on #1, read 9, expect 10.
#include <iostream>
#include <algorithm>
using namespace std;
using LL = long long;
pair<int, int> a[100003];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> a[i].first >> a[i].second;
sort(a + 1, a + n + 1);
int tag = max(a[1].second, a[1].first - a[1].second), id = 1;
for(int i = 2; i <= n; ++i) {
const int w = max(a[i].second, a[i].first - a[i].second);
if(w > tag) {
tag = w;
id = i;
} else if(w == tag && a[i].first > a[id].first)
id = i;
}
LL ans = 0;
for(int i = 1; i <= n; ++i)
if(i != id)
ans = max({ans, (LL)tag + a[i].second, (LL)tag + a[i].first - a[i].second});
cout << ans << endl;
return 0;
}