local n = io.read("*n")
local wmax = 0
local score = 0
local ws, vs = {}, {}
local samesum = 0
for i = 1, n do
  local a, b = io.read("*n", "*n")
  if a < b then
    score = score + b
    wmax = wmax + b - a
  elseif a == b then
    samesum = samesum + b
  else
    table.insert(ws, a - b + 1)
    table.insert(vs, b)
  end
end
if 0 < wmax then
  score = score + samesum
end

local mma, mmi = math.max, math.min
local dpret = 0
local dpmin = 0
local tn = #ws
for i = 1, tn do
  local w, v = ws[i], vs[i]
  dpret = dpret + v
  dpmin = i == 1 and v or mmi(dpmin, v)
end
print(score + dpret - dpmin)
