import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln

void main() {
	int n;
	scanf("%d", &n);
	int[] a = new int[n];
	int[] b = new int[n];

	bool same = true;
	foreach (i; 0..n) {
		scanf("%d%d", &a[i], &b[i]);
		if (a[i] != b[i]) same = false;
	}
	if (same) {
		writeln(0);
		return;
	}
	long ans = 0;
	int[] x;
	foreach (i; 0..n) {
		if (a[i] <= b[i]) {
			ans += b[i];
		} else {
			x ~= b[i];
		}
	}
	x.sort;
	foreach (i; 1..x.length) {
		ans += x[i];
	}
	writeln(ans);
}

