--[[

3 1 4 1 5 9 20
6 5 3 5 8 9 7


4 9 20
3 9 7

*14

]]
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 dp = {}
for i = 1, wmax - 1 do dp[i] = 0 end
local mma = math.max
local dpret = 0
local tn = #ws
for i = 1, tn do
  local w, v = ws[i], vs[i]
  for j = wmax - w, 1, -1 do
    dp[j + w - 1] = mma(dp[j + w - 1], dp[j] + v)
  end
  if w <= wmax then
    dp[w - 1] = mma(dp[w - 1], v)
  end
end
for i = 1, wmax - 1 do
  dpret = mma(dpret, dp[i])
end
print(score + dpret)
