-- import Debug.Trace
import Data.List
import Data.Ord

getInts :: IO [Int]
getInts = map read . words <$> getLine

getData 0 i xs = return xs
getData n i xs = do
  (a:b:[]) <- getInts
  getData (n - 1) (i + 1) ((i,a,b):xs)

getA (_,x,_) = x
getB (_,_,x) = x

scoreA [] bs xs n = n
scoreA ((i,a,b):as) bs xs n
  | elem i xs = scoreA as bs xs n
  | otherwise = scoreB as bs (i:xs) (n + a)

scoreB as [] xs n = n
scoreB as ((i,a,b):bs) xs n
  | elem i xs = scoreB as bs xs n
  | otherwise = scoreA as bs (i:xs) (n - b)

main = do
  (n:ns) <- getInts
  xs <- getData n 0 []
  print $ scoreA (sortBy (comparing (Down . getA)) xs) (sortBy (comparing (Down . getB)) xs) [] 0
