import Control.Monad
import Data.List
import Data.Ord
import Control.Applicative

main = do
  n <- readLn
  ab <- replicateM n $ words <$> getLine
  let abInt = reverse $ sortBy (comparing get1st) $ map sumh $ map (map read) ab :: [(Int,Int,Int)]
  putStrLn $ show $ (sumt abInt) - (suma abInt)
  
sumt :: [(Int,Int,Int)] -> Int
sumt (a:as) = (get2nd a) + (sumt (drop 1 as))
sumt _ = 0

suma :: [(Int,Int,Int)] -> Int
suma (_:(a:as)) = (get3rd a) + (suma as)
suma _ = 0

sumh :: [Int] -> (Int,Int,Int)
sumh [a,b] = ((a + b),a,b)

get1st (a,_,_) = a
get2nd (_,a,_) = a
get3rd (_,_,a) = a
