

genio :: Int -> IO [(Int,Int)]
genio 0 = return []
genio n = do
  [a,b] <- map read.words <$> getLine
  ls <- genio (n-1)
  return $ (a,b):ls

remove :: (Eq a) => a -> [a] -> [a]
remove a (x:xs) = if x == a then xs else x:(remove a xs)

amax :: [(Int,Int)] -> (Int,Int)
amax [] = (-1,-1)
amax ((a,b):ls) = if a >= (fst $ amax ls) then (a,b) else amax ls

bmax :: [(Int,Int)] -> (Int,Int)
bmax [] = (-1,-1)
bmax ((a,b):ls) = if b >= (snd $ bmax ls) then (a,b) else bmax ls

game :: [(Int,Int)] -> Int
game l = taka 0 l

taka :: Int -> [(Int,Int)] -> Int
taka n [] = n
taka n l = if (fst $ amax l) - (snd $ bmax (remove (amax l) l)) >= (fst $ bmax l) - (snd $ bmax (remove (bmax l) l))
  then aoki (n + (fst $ amax l)) (remove (amax l) l)
  else aoki (n + (fst $ bmax l)) (remove (bmax l) l)

aoki :: Int -> [(Int,Int)] -> Int
aoki n [] = n
aoki n l = if (fst $ bmax l) - (snd $ amax (remove (bmax l) l)) >= (fst $ amax l) - (snd $ amax (remove (amax l) l))
  then taka (n - (snd $ bmax l)) (remove (bmax l) l)
  else taka (n - (snd $ amax l)) (remove (amax l) l)

main :: IO ()
main = do
  n <- read <$> getLine
  l <- genio n
  putStrLn $ show $ game l