import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

alias Node = Tuple!(ulong, "index", long, "value");
bool comp(Node v, Node w) {
	if (v.value != w.value) {
		return v.value > w.value;
	}
	return v.index < w.index;
};

void main()
{
	int n = readln.chomp.to!int;
	long[] a;
	long[] b;
	foreach (i; 0..n) {
		auto t = readln.chomp.split.map!(to!long);
		a ~= t.front;
		b ~= t.back;
	}
	alias Tree = RedBlackTree!(Node, comp);
	Tree maji = new Tree();
	foreach (i; 0..n) {
		maji.insert(Node(i, b[i] - a[i]));
	}
	long cnt = 0;
	for (;;) {
		if (maji.length == 0) {
			break;
		}
		Node node = maji.front;
		maji.removeFront;
		if (node.value <= 0) {
			continue;
		}
		long va = a[node.index];
		cnt += va;
		a[node.index] = 0;
		long vb = b[node.index];
		b[node.index] -= va;
	}
	Tree red = new Tree();
	long del = 0;
	foreach (i, v; a) {
		if (v == 0) {
			del += b[i];
			b[i] = 0;
			continue;
		}
		red.insert(Node(i, v));
	}
	cnt += cnt;
	while (del) {
		if (red.length == 0) {
			break;
		}
		auto node = red.front;
		red.removeFront;
		long v = min(del, node.value);
		del -= v;
		a[node.index] = v;
	}
	long v = 0;
	foreach (i; 0..n) {
		v += abs(a[i] - b[i]);
	}
	v /= 2;
	cnt += v;
	cnt.writeln;
}
