#include <iostream>
#include <string>
#include <sstream>
#include <iomanip> 
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <complex>

using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	long long n, i, j, k, ans, INF = 1LL << 60, a, c, v1, v2;
	cin >> n;
	vector<pair<long long, long long>> plist(n);
	for (i = 0; i < n; i++) cin >> plist[i].first >> plist[i].second;
	sort(plist.rbegin(), plist.rend());

	vector<long long> dp(n, INF);
	multiset<long long> best, best2;
	multiset<long long>::iterator it;
	multiset<pair<long long, pair<long long, long long>>> pq;
	for (i = n - 1; i >= 0; i--) {
		tie(a, c) = plist[i];

		if (i == n - 1) {
			dp[i] = 0;
		}
		else {
			while (!pq.empty() && (pq.begin()->first < a)) {
				tie(v1, v2) = pq.begin()->second;
				pq.erase(pq.begin());

				it = best.find(v1);
				best.erase(it);
				best2.insert(v2);
			}

			if (!best.empty()) dp[i] = min(dp[i], *best.begin());
			if (!best2.empty()) dp[i] = min(dp[i], *best2.begin() + a);
		}

		pq.insert({ a + c, { dp[i], dp[i] - a - c } });
		best.insert(dp[i]);
	}

	ans = dp[0];
	for (auto p : plist) ans += p.second;		// add c back
	cout << ans << "\n";

	return 0;
}

