{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Data.List(foldl', unfoldr)
import Data.Char(isSpace)
import Data.Monoid((<>))
import Debug.Trace
import System.Mem.Weak
import System.Mem
import Control.Exception

sort :: Ord a => [a] -> [a]
sort = sortBy compare

{-# NOINLINE sort #-}
sortBy :: (a -> a -> Ordering) -> [a] -> [a]
sortBy compare = mergeAll . sequences
    where
    sequences (a:b:xs)
      | a `compare` b == GT = descending b [a]  xs
      | otherwise       = ascending  b (a:) xs
    sequences xs = [xs]

    descending a as (b:bs)
      | a `compare` b == GT = descending b (a:as) bs
    descending a as bs  = (a:as): sequences bs

    ascending a as (b:bs)
      | a `compare` b /= GT = ascending b (\ys -> as (a:ys)) bs
    ascending a as bs   = let !x = as [a]
                          in x : sequences bs

    mergeAll [x] = x
    mergeAll xs  = mergeAll (mergePairs xs)

    mergePairs (a:b:xs) = let !x = merge a b
                          in x : mergePairs xs
    mergePairs xs       = xs

    merge as@(a:as') bs@(b:bs')
      | a `compare` b == GT = (b: ) $! merge as  bs'
      | otherwise       = (a: ) $! merge as' bs
    merge [] bs         = bs
    merge as []         = as

data PI = PI {-# UNPACK #-}!Int {-# UNPACK #-} !Int {-# UNPACK #-} !Int
    deriving (Eq, Ord)

parse :: B.ByteString -> [PI]
parse = unfoldr step
  where
  step !s | B.null s = Nothing
          | otherwise = do
               (n1, s1) <- B.readInt s
               (n2, s2) <- B.readInt (B.tail s1)
               let !s3 = B.dropWhile isSpace s2
               return (PI (- (n1 + n2)) n1 n2, s3)

main :: IO ()
main = do
    n <- fmap read getLine :: IO Int
    content <- B.getContents
    let asbs = parse content
    let l  = sort asbs
        ps = cycle [1,-1 :: Int]
        ans = sum $ zipWith (\(PI _ a b) p -> if p == 1 then a else -b) l ps
    evaluate l
    traceMarker "sort" (print ans)
