#include <iostream>
#include <vector>
#include <map>
#include <algorithm>


int64_t get_ans(std::vector< std::pair<int64_t, int64_t> > & v)
{
    int64_t sum_a = 0;
    for (auto & p : v)
    {
        sum_a += p.first;
        p.second += p.first;
    }
    std::sort(v.begin(), v.end());
    
    std::vector<size_t> index(v.size());
    index[0] = 1;
    index[1] = 0;
    int64_t max_b = std::max(v[0].second, v[1].second);
    size_t max_b_index = static_cast<size_t>(v[0].second <= v[1].second);
    
    for (size_t i = 2; i < v.size(); i++)
    {
        index[i] = index[max_b_index];
        index[max_b_index] = i;
        
        if (v[i].second >= max_b)
        {
            max_b = v[i].second;
            max_b_index = i;
        }
    }
    
    int64_t ans = 0;
    for (int i = 0; i < v.size(); i++)
        ans += std::max(v[i].second, v[index[i]].first);
    return ans - sum_a;
}

void init()
{
    int n;
    std::cin>>n;
    std::vector< std::pair<int64_t, int64_t> > v(n);
    for (int i = 0; i < n; i++) std::cin>>v[i].first>>v[i].second;
    std::cout<<get_ans(v)<<std::endl;
}

int main()
{
    init();
    return 0;
}
