import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)


  val N = in.next().toInt
  val A = new Array[Int](N+1)
  val B = new Array[Int](N+1)
  for (i <- 1 to N) {
    A(i) = in.next().toInt
    B(i) = in.next().toInt
  }


  //
  var ans = 0L
  var turn = 0L
  var cnt = 0
  for (i <- 1 to N) {
    if (A(i) < B(i)) {
      ans += B(i)
      turn += B(i) - A(i)
    }
    else {
      cnt += 1
    }
  }


  //
  val dp = new Array[(Int, Int)](cnt)
  var k = 0
  for (i <- 1 to N) {
    if(A(i) >= B(i)) {
      dp(k) = (-(B(i) - A(i)), B(i))
      k += 1
    }
  }
  val dp2 = dp.sorted

  for((_diff, b) <- dp2) {
    val diff = _diff

    if(diff + 1 <= turn) {
      turn -= (diff + 1)
      turn += 1
      ans += b
    }
  }

  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
