import qualified Data.ByteString.Char8 as BS import Control.Monad import Data.List import qualified Data.Map.Strict as M import Data.Maybe tuplify2 (x : y : _) = (x, y) tuplify2 _ = undefined readInt = fst . fromJust . BS.readInteger readIntTuple = tuplify2 . map readInt . BS.words readIntList = map readInt . BS.words getInt = readInt <$> BS.getLine getIntList = readIntList <$> BS.getLine getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine getIntMatrix = map readIntList . BS.lines <$> BS.getContents getIntTuple = readIntTuple <$> BS.getLine getIntNTuples n = map readIntTuple <$> replicateM (fromIntegral n) BS.getLine getIntTuples = map readIntTuple . BS.lines <$> BS.getContents main = do n <- getInt s <- replicateM ((fromInteger n)-1) getIntList print $ solve s solve :: [[Integer]] -> Integer solve xs = foldl (\n v -> (max (v - 2) 0) + n) (0 :: Integer) (counter (concat xs) M.empty) counter :: [Integer] -> M.Map Integer Integer -> M.Map Integer Integer counter [] count = count counter (x : xs) count = case M.lookup x count of Nothing -> counter xs (M.insert x 1 count) Just n -> counter xs (M.insert x (n+1) count)