We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $$$a$$$ yuan for $$$b$$$ kilos (You don't need to care about what "yuan" is), the same as $$$a/b$$$ yuan for a kilo.Now imagine you'd like to buy $$$m$$$ kilos of apples. You've asked $$$n$$$ supermarkets and got the prices. Find the minimum cost for those apples.You can assume that there are enough apples in all supermarkets.

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 5\,000$$$, $$$1 \leq m \leq 100$$$), denoting that there are $$$n$$$ supermarkets and you want to buy $$$m$$$ kilos of apples. The following $$$n$$$ lines describe the information of the supermarkets. Each line contains two positive integers $$$a, b$$$ ($$$1 \leq a, b \leq 100$$$), denoting that in this supermarket, you are supposed to pay $$$a$$$ yuan for $$$b$$$ kilos of apples.

The only line, denoting the minimum cost for $$$m$$$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $$$10^{-6}$$$. Formally, let your answer be $$$x$$$, and the jury's answer be $$$y$$$. Your answer is considered correct if $$$\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}$$$.

NoteIn the first sample, you are supposed to buy $$$5$$$ kilos of apples in supermarket $$$3$$$. The cost is $$$5/3$$$ yuan.In the second sample, you are supposed to buy $$$1$$$ kilo of apples in supermarket $$$2$$$. The cost is $$$98/99$$$ yuan.

main=getContents>>=print.minimum.f.map read.words
f(_:m:l)=g l
  where
    g(x:y:z)=(x/y*m):g z
    g _ = []

import Control.Monad

main = do
    [n, m] <- fmap (fmap read . words) getLine
    l <- replicateM n $ fmap (fmap read . words) getLine
    print . minimum . map(\[a,b] -> (fromIntegral m)*a / b) $ l

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

myread::String->Integer
myread= read

main = do
		[n,m]<-map read<$> words <$> getLine ::IO [Int]
		a<-map (\(a:b:c)-> fromIntegral a / (fromIntegral b)) <$> map (map (myread)) <$> map words <$> replicateM n getLine
		print $ (fromIntegral m)* minimum a

main = do
  [n, m] <- fmap (map read . words) getLine
  t <- mapM (\_ -> getLine) [1..n]
  let tt = map ((\[x, y] -> y / x) . map read . words) t
  print $ m / maximum tt

main = do
    [_,n] <- fmap (map read . words) getLine
    c <- fmap lines getContents
    print $ n * minimum [let [y,k] = (map read . words) l in y/k | l <- c]

import Data.List

f::[String]->Double
f (a:b:[])=(read a::Double) / (read b::Double)

main=do
     e1<-getLine
     es<-getContents
     let (n:m:[])=map read (words e1)::[Double]
         ans=head $ sort $ map f (map words (lines es))
     print $ ans*m

import Control.Applicative

main::IO ()
main=do
    [n,m]<-map read <$> words<$> getLine::IO [Double]
    a<-(map( map read) <$> map words<$> lines <$> getContents)::IO [[Double]]
    let b = minimum  $ map (\[z1,z2]-> z1/z2) a
    print $ b *m

main = interact $ show . solve . map read . tail . words
solve (m:ps) = minimum $ map (m*) $ go ps where
  go (a:b:ps) = a/b : go ps
  go [] = []

main = getContents >>= print . solve 10000.0 . map read . words

solve ans (0:_) = ans
solve ans (n:m:a:b:rst) = solve (min ans (m * a / b)) (n - 1:m:rst)


You are given one integer $$$n$$$ ($$$n > 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n > 1$$$.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.

For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n > 1$$$.

import Control.Arrow ((>>>))

main =
  interact $
  words >>> drop 1 >>> map (read >>> solve >>> map show >>> unwords) >>> unlines

solve n = n : [1 .. n - 1]

import Data.List
import System.IO

main :: IO ()
main = do
    input <- getContents
    let output = solve input
    putStr output

solve :: String -> String
solve input = let n_list = tail $ map read $ words input
                  ans_list = map (\x -> foldl (\x y -> x ++ show y ++ " ") "" (x:[1..x-1])) n_list
              in foldl (\x y -> x ++ y ++ "\n") "" ans_list

solve n = [2..n] ++ [1]
main = interact $ unlines . map (unwords . map show) . map solve . tail . map read . words

import Data.List

solve :: Int -> [Int]
solve n = [2..n] ++ [1]

main :: IO ()
main = interact $ unlines . map (intercalate " " . map show) . map solve . tail . map read . words

{-# LANGUAGE GADTs, TemplateHaskell, DeriveFunctor, OverloadedStrings #-}

import Data.List
import qualified Data.ByteString.Char8 as B8
import Data.Maybe
import Data.Int
import Text.Printf
import Data.Function ( (&), on )
import Data.Functor
import Control.Arrow ((>>>))
import Control.Monad (forM_)
import Data.Char (isAlpha)

readIntB8 :: B8.ByteString -> Maybe Int
readIntB8 = B8.readInt >>> fmap fst

solve :: Int -> [Int]
solve n = [2 .. n] ++ [1]

main :: IO ()
main = do
    Just t <- B8.getLine <&> readIntB8
    forM_ [1 .. t] $ \_ -> do
        Just n <- B8.getLine <&> readIntB8
        let answer = solve n
        forM_ answer $ printf "%d "
        printf "\n"

import Control.Monad
import Data.List

main = do
  testCases <- read <$> getLine
  replicateM testCases $ do
    permutationLength <- read <$> getLine :: IO Int
    putStrLn $ mconcat $ intersperse " " $ map show $ specialPermutations permutationLength

specialPermutations :: Int -> [Int]
specialPermutations n
  | even n = reverse [1 .. n]
  | odd n = reverse $ mconcat [[1 .. (middle - 1)], [middle + 1, middle], [middle + 2 .. n]]
  where
    middle = div n 2 + 1

{-# LANGUAGE Safe #-}

import safe Control.Arrow ((>>>))
--import safe Data.List (sort)
import Data.List

main :: IO ()
main =  interact $  lines >>> drop 1 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (l : rest) = (istoline . solve . linetoi) l : tcio rest
  itoline = show; istoline = unwords . (map itoline); isstolines = map istoline
  linetoi = read;

solve :: Int -> [Int]
solve n = (:) <$> last <*> init $ [1..n]

import Control.Monad ( replicateM )

main :: IO ()
main = do
    t <- readLn
    putStrLn . unlines . map (\n -> unwords . map show $ n : [1..(n - 1)]) =<< replicateM t (readLn :: IO Int)

import Control.Applicative ()

f :: Int -> IO ()
f 0 = return ()
f t = do
    n <- readLn
    putStrLn . unwords . map show $ n : [1..(n - 1)]
    f (t - 1)

main :: IO ()
main = f =<< readLn

f :: Int -> IO ()
f 0 = return ()
f t = do
    n <- readLn :: IO Int
    putStrLn . unwords . map show $ n : [1..(n - 1)]
    f (t - 1)

main :: IO ()
main = do
    t <- readLn
    f t

f :: Int -> IO ()
f 0 = return ()
f t = do
    n <- readLn :: IO Int
    putStrLn . unwords . map show $ n : [1..(n - 1)]
    f (t - 1)

main :: IO ()
main = do
    t <- readLn :: IO Int
    f t

f :: Int -> IO ()
f 0 = return ()
f t = do
    n <- readLn
    putStrLn . unwords . map show $ n : [1..(n - 1)]
    f (t - 1)

main :: IO ()
main = do
    t <- readLn
    f t

f :: Int -> IO ()
f 0 = return ()
f t = do
    n <- readLn :: IO Int
    putStrLn . unwords . map show $ (n : [1..(n - 1)])
    f (t - 1)

main :: IO ()
main = do
    t <- readLn :: IO Int
    f t

import Control.Monad
import Data.List

main = do
  testCases <- read <$> getLine
  replicateM testCases $ do
    permutationLength <- read <$> getLine :: IO Int
    putStrLn $ mconcat $ intersperse " " $ map show $ reverse [1 .. permutationLength]

import Control.Monad ( replicateM )

main :: IO ()
main = do
    t <- readLn
    putStrLn . unlines . map (\n -> unwords . map show $ n : [2..(n - 1)]) =<< replicateM t (readLn :: IO Int)


One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.

The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.

If the given state is reachable in the described game, output YES, otherwise NO.

import Data.Array
import Data.List
import Data.Graph
import Data.Tree
import Control.Applicative

solve n want fav = and [sort x == sort [want ! i | i <- x] | x <- forest]
   where forest = map flatten . components . buildG (1, n) $ xs
         xs = concat [filter good [(i, i+fav!i), (i, i-fav!i)] | i <- [1..n]]
         good (i, j) = 0 < j && j <= n
ints = map read . words <$> getLine
main = do
    [n] <- ints
    want <- listArray (1, n) <$> ints
    fav  <- listArray (1, n) <$> ints
    let can = solve n want fav
    putStrLn (if can then "YES" else "NO")


There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and pi > pj.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

Print a single integer — the number of inversions in the resulting sequence.

NoteIn the first sample the sequence is being modified as follows: . It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).

{-#LANGUAGE BangPatterns #-}

import Control.Monad
import Data.Int
import Data.List
import qualified Data.IntMap.Strict as M
import Data.Tuple
import Data.Maybe(fromJust)
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B
import Control.DeepSeq

data Node = Node !Int !Node !Node | Empty

getSum :: Node -> Int
getSum (Node s _ _) = s

build :: Int -> Int -> Node
build !l !r
    | l == r = Node 0 Empty Empty
    | otherwise =
        let m = (l + r) `div` 2
        in Node 0 (build l m) (build (m + 1) r)

update :: Node -> Int -> Int -> Int -> Int -> Node
update !(Node !val !left !right) !l !r !pos !delta
    | l == r = Node (val + delta) Empty Empty
    | otherwise =
        let m = (l + r) `div` 2
        in
            if pos <= m then
                Node (val + delta) (update left l m pos delta) right
            else
                Node (val + delta) left (update right (m + 1) r pos delta)

get :: Node -> Int -> Int -> Int -> Int -> Int
get !(Node !val !left !right) !tl !tr !l !r
    | l == tl && r == tr = val
    | otherwise =
        let m = (tl + tr) `div` 2
        in
            if l <= m then
                (get left tl m l (min r m)) + getSum right
            else
                get right (m + 1) tr (max (m + 1) l) r
solve :: [(Int, Int)] -> Int64

solve xs =
    let n = length xs
        step :: (Node, Int64) -> (Int, Int) -> (Node, Int64)
        step (!root, !res) (!pos, !size) =
            let add = ((fromIntegral size) :: Int64) *
                      (fromIntegral $ get root 0 (n - 1) pos (n - 1))
                root' = update root 0 (n - 1) pos size
            in
                (root', res + add)
    in
        snd $ foldl' step (build 0 (n - 1), 0::Int64) xs

makeSwaps :: [(Int, Int)] -> ([Int], [Int]) -> [(Int, Int)]
makeSwaps swaps !(vals, s) =
    let
        initVals = M.fromAscList $ zip vals vals
        step !vs (!a, !b) =
            let va = vs M.! a
                vb = vs M.! b
                vs' = M.insert a vb vs
            in
                M.insert b va vs'
        finalVals = M.assocs $ foldl' step initVals swaps
        compressed = finalVals `deepseq` M.fromAscList $ zip vals (zip [0..] s)
   in
        map (\(_, a) -> (let (f, s) = compressed M.! a in (f, s))) finalVals

getSizes :: [Int] -> ([Int], [Int])
getSizes [] = ([], [])
getSizes [x] = ([x], [1])
getSizes (x:y:xs)
    | x == y - 1 =
        let (!a, !b) = getSizes(y:xs)
        in (x:a, 1:b)
    | otherwise =
        let (!a, !b) = getSizes(y:xs)
        in (x:x + 1:a, 1:y - x - 1:b)

main = do
    input <- B.getContents
    let (line:lines) = B.lines input
    let n = lines `deepseq` (fst . fromJust $ B.readInt line)
    let swaps = map (\l -> let [x, y] = B.words l in (fst . fromJust $ B.readInt x, fst . fromJust $ B.readInt y)) lines
    let sizes = swaps `deepseq` getSizes (S.elems $ foldl' (\acc (a, b) -> S.insert a (S.insert b acc)) (S.empty::S.Set Int) swaps)
    let afterSwaps = sizes `deepseq` makeSwaps swaps sizes
    let size = afterSwaps `deepseq` length (fst sizes)
    let res = solve afterSwaps
    print res

-- This solution is purely functional.

{-#LANGUAGE BangPatterns #-}

import Control.Monad
import Data.Int
import Data.List
import qualified Data.IntMap.Strict as M
import Data.Tuple
import Data.Maybe(fromJust)
import qualified Data.IntSet as S
import qualified Data.ByteString.Char8 as B
import Control.DeepSeq

data Node = Node !Int !Node !Node | Empty

getSum :: Node -> Int
getSum (Node s _ _) = s

build :: Int -> Int -> Node
build !l !r
    | l == r = Node 0 Empty Empty
    | otherwise =
        let m = (l + r) `div` 2
        in Node 0 (build l m) (build (m + 1) r)

update :: Node -> Int -> Int -> Int -> Int -> Node
update !(Node !val !left !right) !l !r !pos !delta
    | l == r = Node (val + delta) Empty Empty
    | otherwise =
        let m = (l + r) `div` 2
        in
            if pos <= m then
                Node (val + delta) (update left l m pos delta) right
            else
                Node (val + delta) left (update right (m + 1) r pos delta)

get :: Node -> Int -> Int -> Int -> Int -> Int
get !(Node !val !left !right) !tl !tr !l !r
    | l == tl && r == tr = val
    | otherwise =
        let m = (tl + tr) `div` 2
        in
            if l <= m then
                (get left tl m l (min r m)) + getSum right
            else
                get right (m + 1) tr (max (m + 1) l) r
solve :: [(Int, Int)] -> Int64

solve xs =
    let n = length xs
        step :: (Node, Int64) -> (Int, Int) -> (Node, Int64)
        step (!root, !res) (!pos, !size) =
            let add = ((fromIntegral size) :: Int64) *
                      (fromIntegral $ get root 0 (n - 1) pos (n - 1))
                root' = update root 0 (n - 1) pos size
            in
                (root', res + add)
    in
        snd $ foldl' step (build 0 (n - 1), 0::Int64) xs

makeSwaps :: [(Int, Int)] -> ([Int], [Int]) -> [(Int, Int)]
makeSwaps swaps !(vals, s) =
    let
        initVals = M.fromAscList $ zip vals vals
        step !vs (!a, !b) =
            let va = vs M.! a
                vb = vs M.! b
                vs' = M.insert a vb vs
            in
                M.insert b va vs'
        finalVals = M.assocs $ foldl' step initVals swaps
        compressed = finalVals `deepseq` M.fromAscList $ zip vals (zip [0..] s)
   in
        map (\(_, a) -> (let (f, s) = compressed M.! a in (f, s))) finalVals

getSizes :: [Int] -> ([Int], [Int])
getSizes [] = ([], [])
getSizes [x] = ([x], [1])
getSizes (x:y:xs)
    | x == y - 1 =
        let (!a, !b) = getSizes(y:xs)
        in (x:a, 1:b)
    | otherwise =
        let (!a, !b) = getSizes(y:xs)
        in (x:x + 1:a, 1:y - x - 1:b)

main = do
    input <- B.getContents
    let (line:lines) = B.lines input
    let n = lines `deepseq` (fst . fromJust $ B.readInt line)
    let swaps = map (\l -> let [x, y] = B.words l in (fst . fromJust $ B.readInt x, fst . fromJust $ B.readInt y)) lines
    let sizes = swaps `deepseq` getSizes (S.elems $ foldl' (\acc (a, b) -> S.insert a (S.insert b acc)) S.empty swaps)
    let afterSwaps = sizes `deepseq` makeSwaps swaps sizes
    let size = afterSwaps `deepseq` length (fst sizes)
    let res = solve afterSwaps
    print res

{-#LANGUAGE BangPatterns #-}

import Control.Monad
import Data.Int
import Data.List
import qualified Data.Map.Strict as M
import Data.Tuple
import Data.Maybe(fromJust)
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B
import Data.Array.IO
import Data.Bits
import qualified Data.Sequence as Seq

updateAt :: IOUArray Int Int -> Int -> Int -> Int -> IO ()
updateAt f !n !at !delta
    | at >= n = return ()
    | otherwise = do
        cur <- readArray f at
        writeArray f at (delta + cur)
        updateAt f n (at .|. (at + 1)) delta

getAt :: IOUArray Int Int -> Int -> Int -> IO Int
getAt f !at !acc
    | at < 0 = return acc
    | otherwise = do
        cur <- readArray f at
        getAt f ((at .&. (at + 1)) - 1) (acc + cur)

solve :: [(Int, Int)] -> IOUArray Int Int -> Int -> IO Int64
solve [] _ _  = return 0
solve !((pos, size):xs) tree !n = do
    sum <- getAt tree pos 0
    let res = (fromIntegral sum) * (fromIntegral size)
    _ <- updateAt tree n pos size
    rest <- solve xs tree n
    return (rest + res)

makeSwaps :: [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
makeSwaps swaps sizes =
    let vals = map fst sizes
        s = map snd sizes
        initVals = M.fromAscList $ zip vals vals
        step !vs (!a, !b) =
            let va = vs M.! a
                vb = vs M.! b
                vs' = M.insert a vb vs
            in
                M.insert b va vs'
        finalVals = M.assocs $ foldl' step initVals swaps
        compressed = M.fromAscList $ zip vals (zip [0..] s)
   in
        map (\(_, a) -> (let (f, s) = compressed M.! a in (f, s))) finalVals

getSizes :: [Int] -> [(Int, Int)]
getSizes ![] = []
getSizes ![x] = [(x, 1)]
getSizes !(x:y:xs)
    | x == y = getSizes(y:xs)
    | x == y - 1 = (x, 1) : getSizes (y:xs)
    | otherwise = (x, 1) : (x + 1, y - x - 1) : getSizes (y:xs)

main = do
    input <- B.getContents
    let (line:lines) = B.lines input
    let n = fst . fromJust $ B.readInt line
    let swaps = map (\l -> let [x, y] = B.words l in (fst . fromJust $ B.readInt x, fst . fromJust $ B.readInt y)) lines
    let all = foldl' (\acc (a, b) -> a : b : acc) [] swaps
    let sizes = getSizes (S.elems (S.fromList all))
    let afterSwaps = makeSwaps swaps sizes
    let size = length sizes
    tree <- newArray (0, size - 1) 0 :: IO (IOUArray Int Int)
    res <- solve (reverse afterSwaps) tree size
    print res

{-#LANGUAGE BangPatterns #-}

import Control.Monad
import Data.Int
import Data.List
import qualified Data.IntMap.Strict as M
import Data.Tuple
import Data.Maybe(fromJust)
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B
import Data.Array.IO
import Data.Bits
import qualified Data.Sequence as Seq

updateAt :: IOUArray Int Int -> Int -> Int -> Int -> IO ()
updateAt f !n !at !delta
    | at >= n = return ()
    | otherwise = do
        cur <- readArray f at
        writeArray f at (delta + cur)
        updateAt f n (at .|. (at + 1)) delta

getAt :: IOUArray Int Int -> Int -> Int -> IO Int
getAt f !at !acc
    | at < 0 = return acc
    | otherwise = do
        cur <- readArray f at
        getAt f ((at .&. (at + 1)) - 1) (acc + cur)

solve :: [(Int, Int)] -> IOUArray Int Int -> Int -> IO Int64
solve [] _ _  = return 0
solve !((pos, size):xs) tree !n = do
    sum <- getAt tree pos 0
    let res = (fromIntegral sum) * (fromIntegral size)
    _ <- updateAt tree n pos size
    rest <- solve xs tree n
    return (rest + res)

makeSwaps :: [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
makeSwaps swaps sizes =
    let vals = map fst sizes
        s = map snd sizes
        initVals = M.fromAscList $ zip vals vals
        step !vs (!a, !b) =
            let va = vs M.! a
                vb = vs M.! b
                vs' = M.insert a vb vs
            in
                M.insert b va vs'
        finalVals = M.assocs $ foldl' step initVals swaps
        compressed = M.fromAscList $ zip vals (zip [0..] s)
   in
        map (\(_, a) -> (let (f, s) = compressed M.! a in (f, s))) finalVals

getSizes :: [Int] -> [(Int, Int)]
getSizes ![] = []
getSizes ![x] = [(x, 1)]
getSizes !(x:y:xs)
    | x == y = getSizes(y:xs)
    | x == y - 1 = (x, 1) : getSizes (y:xs)
    | otherwise = (x, 1) : (x + 1, y - x - 1) : getSizes (y:xs)

main = do
    input <- B.getContents
    let (line:lines) = B.lines input
    let n = fst . fromJust $ B.readInt line
    let swaps = map (\l -> let [x, y] = B.words l in (fst . fromJust $ B.readInt x, fst . fromJust $ B.readInt y)) lines
    let all = foldl' (\acc (a, b) -> a : b : acc) [] swaps
    let sizes = getSizes (S.elems (S.fromList all))
    let afterSwaps = makeSwaps swaps sizes
    let size = length sizes
    tree <- newArray (0, size - 1) 0 :: IO (IOUArray Int Int)
    res <- solve (reverse afterSwaps) tree size
    print res

{-#LANGUAGE BangPatterns #-}

import Control.Monad
import Data.Int
import Data.List
import qualified Data.Map.Strict as M
import Data.Tuple
import Data.Maybe(fromJust)
import qualified Data.ByteString.Char8 as B

data Node = Node !Int !Node !Node | Empty

build :: Int -> Int -> Node
build !l !r
    | l == r = Node 0 Empty Empty
    | otherwise =
        let m = (l + r) `div` 2
        in Node 0 (build l m) (build (m + 1) r)

update :: Node -> Int -> Int -> Int -> Int -> Node
update !(Node !val !left !right) !l !r !pos !delta
    | l == r = Node (val + delta) Empty Empty
    | otherwise =
        let m = (l + r) `div` 2
        in
            if pos <= m then
                Node (val + delta) (update left l m pos delta) right
            else
                Node (val + delta) left (update right (m + 1) r pos delta)

get :: Node -> Int -> Int -> Int -> Int -> Int
get !(Node !val !left !right) !tl !tr !l !r
    | l == tl && r == tr = val
    | otherwise =
        let m = (tl + tr) `div` 2
            sumL = if l <= m then get left tl m l (min r m) else 0
            sumR = if r > m then get right (m + 1) tr (max (m + 1) l) r else 0
        in
            sumL + sumR

solve :: [(Int, Int)] -> Int64
solve xs =
    let n = length xs
        step :: (Node, Int64) -> (Int, Int) -> (Node, Int64)
        step (!root, !res) (!pos, !size) =
            let add = ((fromIntegral size) :: Int64) *
                      (fromIntegral $ get root 0 (n - 1) pos (n - 1))
                root' = update root 0 (n - 1) pos size
            in
                (root', res + add)
    in
        snd $ foldl' step (build 0 (n - 1), 0::Int64) xs

makeSwaps :: [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
makeSwaps swaps sizes =
    let vals = map fst sizes
        s = map snd sizes
        initVals = M.fromAscList $ zip vals vals
        step vs (a, b) =
            let va = vs M.! a
                vb = vs M.! b
                vs' = M.insert a vb vs
            in
                (M.insert $ b) va $ vs'
        finalVals = M.assocs $ foldl' step initVals swaps
        compressed = M.fromAscList $ zip vals (zip [0..] s)
   in
        map (\(_, a) -> (let (f, s) = compressed M.! a in (f, s))) finalVals

getSizes :: [Int] -> [(Int, Int)]
getSizes [] = []
getSizes [x] = [(x, 1)]
getSizes (x:y:xs)
    | x == y = getSizes(y:xs)
    | x == y - 1 = (x, 1) : getSizes (y:xs)
    | otherwise = (x, 1) : (x + 1, y - x - 1) : getSizes (y:xs)

readInt :: IO Int
readInt = head `liftM` readIntList

readIntList :: IO [Int]
readIntList = do
    line <- B.getLine
    let res = map (fst . fromJust . B.readInt) $ B.words line
    return res

readSwaps :: Int -> IO ([(Int, Int)], [Int])
readSwaps 0 = (return ([], [])) :: IO ([(Int, Int)], [Int])
readSwaps n = do
    [a, b] <- readIntList
    (swaps, all) <- readSwaps (n - 1)
    return ((a, b):swaps, a:b:all)

main = do
    n <- readInt
    (swaps, all) <- readSwaps n
    let sizes = getSizes (sort all)
    print $ length sizes
    let afterSwaps = makeSwaps swaps sizes
    print $ solve afterSwaps