#include<bits/stdc++.h>
using namespace std;
int n;
const int MAX_N = 1e6 + 5;
pair<long long, long long> arr[MAX_N];
bool cmp(pair<long long, long long> x, pair<long long, long long> y)
{
return max(x.first, x.first + y.first - x.second) < max(y.first, x.first + y.first - y.second);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin >> n)
{
for (int i = 0; i < n; ++i)
cin >> arr[i].first >> arr[i].second;
sort(arr, arr + n, cmp);
long long ans = 0, s = 0;
for (int i = 0; i < n; ++i)
{
long long need = arr[i].first - s;
if (need > 0)
{
ans += need;
s += need;
}
s += arr[i].second - arr[i].first;
}
cout << ans << '\n';
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBuOwpjb25zdCBpbnQgTUFYX04gPSAxZTYgKyA1OwpwYWlyPGxvbmcgbG9uZywgbG9uZyBsb25nPiBhcnJbTUFYX05dOwoKCmJvb2wgY21wKHBhaXI8bG9uZyBsb25nLCBsb25nIGxvbmc+IHgsIHBhaXI8bG9uZyBsb25nLCBsb25nIGxvbmc+IHkpCnsKICAgIHJldHVybiBtYXgoeC5maXJzdCwgeC5maXJzdCArIHkuZmlyc3QgLSB4LnNlY29uZCkgPCBtYXgoeS5maXJzdCwgeC5maXJzdCArIHkuZmlyc3QgLSB5LnNlY29uZCk7Cn0KCmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKDApOwogICAgY291dC50aWUoMCk7CgogICAgd2hpbGUoY2luID4+IG4pCiAgICB7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpCiAgICAgICAgICAgIGNpbiA+PiBhcnJbaV0uZmlyc3QgPj4gYXJyW2ldLnNlY29uZDsKICAgICAgICBzb3J0KGFyciwgYXJyICsgbiwgY21wKTsKICAgICAgICAKICAgICAgICBsb25nIGxvbmcgYW5zID0gMCwgcyA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpCiAgICAgICAgewogICAgICAgICAgICBsb25nIGxvbmcgbmVlZCA9IGFycltpXS5maXJzdCAtIHM7CiAgICAgICAgICAgIGlmIChuZWVkID4gMCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgYW5zICs9IG5lZWQ7CiAgICAgICAgICAgICAgICBzICs9IG5lZWQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcyArPSBhcnJbaV0uc2Vjb25kIC0gYXJyW2ldLmZpcnN0OwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGFucyA8PCAnXG4nOwogICAgfQogICAgcmV0dXJuIDA7Cn0=