import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main() {
  int n;
  readf (" %d", &n);
  auto a = new long[n];
  auto b = new long[n];
  foreach (t; 0 .. n) {
    readf (" %d %d", &a[t], &b[t]);
  }
  auto s = a.sum;
  auto c = iota (0, n).filter! (i => a[i] > b[i]).map! ( i => tuple (abs (a[i] - b[i]), -b[i], i)).array;
  //auto c = a.zip(b).filter! (t => t[0] > t[1]).map! ( t => abs (t[0] - t[1])).array;
  if (c.length == 0) {
    writeln (0);
  } else {
    sort (c);
    int i = c[$-1][2];
    writeln (s - b[i]);
    //writeln (s - c.reduce! (max));
  }

  //auto s1 = a.zip(b).filter! (t => t[0] < t[1]).map! ( t => abs (t[0] - t[1])).sum;
  //auto s2 = a.zip(b).filter! (t => t[0] > t[1]).map! ( t => abs (t[0] - t[1])).sum;
  //writeln (max (s1, s2));
}
