import std.stdio;
import std.algorithm;
import std.conv;
import std.numeric;
import std.math;
import std.string;

void main()
{
	auto tokens = split(chomp(readln()));
	auto N = to!uint(tokens[0]);

	auto a = new ulong[N];
	auto b = new ulong[N];
	foreach (i; 0..N)
	{
		auto tokens1 = split(chomp(readln()));
		a[i] = to!ulong(tokens1[0]);
		b[i] = to!ulong(tokens1[1]);
	}

	auto index_a = new size_t[a.length];
	makeIndex!("a < b")(a, index_a);
	auto index_b = new size_t[b.length];
	makeIndex!("a < b")(b, index_b);
	ulong mid_a = a[index_a[$/2]];
	ulong mid_b = b[index_b[$/2]];

	auto flag = new bool[N];
	fill(flag, false);

	ulong search(ulong pos, uint begin, uint end, ulong cost, ulong cnt)
	{
		stderr.writeln(pos, ":", cost);
		auto c_src = a[cast(uint)pos];
		if (cnt == N - 1)
		{
			return cost + min(c_src, b[0]);
		}
		if (c_src <= mid_a)
		{
			auto i = index_b[end];
			return search(i, begin, end - 1, cost + min(c_src, b[i]), cnt + 1);
			/*foreach_reverse (i; index_b)
			{
				if (flag[i] == false)
				{
					flag[i] = true;
					return search(i, cost + min(c_src, b[i]), cnt + 1);
				}
			}*/
		}
		else
		{
			auto i = index_b[begin];
			return search(i, begin + 1, end, cost + min(c_src, b[i]), cnt + 1);
			/*foreach (i; index_b)
			{
				if (flag[i] == false)
				{
					flag[i] = true;
					return search(i, cost + min(c_src, b[i]), cnt + 1);
				}
			}*/
		}
		return -1;
	}

	flag[0] = true;
	auto result = search(0, 0, b.length - 1, 0, 0);

	writeln(result);
	stdout.flush();
}