#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;
}