import Data.Bits

main :: IO()
main = do
    co <- getContents
    let (n:m:k:ca) = map read $ words co
        va = take (1+m) ca
        fu = va !! m
        in putStrLn $ show $ (length $ filter (\x -> popCount (x `xor` fu) <= k) va) - 1

import Data.Bits(popCount,xor)

main = interact solve

solve input = show (foldr folder 0 tocheck)
    where
        folder::Int->Int->Int
        folder current count = if popCount (current `xor` own) > k then
                                    count
                               else
                                    count+1
        numbers = (map read . words) input
        (n:m:k:thelist) = numbers
        tocheck::[Int]
        tocheck = init thelist
        own = last thelist

module Main where

import Control.Monad
import Data.List
import Data.Bits

main :: IO ()
main = do
    [_, m, k] <- getLine >>= (mapM (return . (read :: String -> Int))) . words
    vals <- forM [1..(m+1)] (\_ -> getLine >>= (return . (read :: String -> Int)))
    print . length $ filter (<=k) [popCount (xor (last vals) i) | i <- init vals]

import Control.Applicative
import Data.List
import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import qualified Data.Map.Strict as M
import Data.Bits

main=do
  [n,m,k]<-map read <$> words <$> getLine ::IO [Int]
  s<-map read <$> lines <$> getContents ::IO [Int]
  let q = last s
  let s1 = init s
  print $  length $  filter (<=k) $ map popCount $ map (xor q) s1

import Data.Bits
main=getContents>>=print.solve.map read.words
solve(_:_:k:xs)=length[x|x<-xs,popCount(x`xor`last xs)<=k]-1

import Numeric (showIntAtBase)
import Data.Bits (xor)
import Data.Char (intToDigit)

main :: IO ()
main = getContents >>= print . solve . map read . words

solve :: [Int] -> Int
solve (_:_:k:xs) = length [ x | x <- init xs, length (filter (=='1') (showIntAtBase 2 intToDigit (x `xor` last xs) "")) <= k ]
solve _ = undefined

import Data.Bits (xor, popCount)

main :: IO ()
main = getContents >>= print . solve . map read . words

solve :: [Int] -> Int
solve (_:_:k:xs) = length [ x | x <- init xs, popCount (x `xor` last xs) <= k ]
solve _ = undefined

import Data.Bits
main=getContents>>=print.solve.map read.words
solve(_:_:k:xs)=length[x|x<-init xs,popCount(x`xor`last xs)<=k]

import Data.Bits
main = interact $ show . solve . map read . words
solve (n:m:k:xs) = length $ filter ((<= k) . popCount) $ map (xor f) es
  where (es,[f]) = splitAt m xs

import Data.Bits

main :: IO()
main = do
    co <- getContents
    let (n:m:k:ca) = map read $ words co
        va = take (1+m) ca
        fu = va !! m
        in putStrLn $ show $ (length [x | x <- va, popCount (x `xor` fu) <= k]) - 1

module Main where

import Data.List (unfoldr)
import Data.Bits (xor)
import Control.Monad (replicateM)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as BS

readNumbers :: IO [Int]
readNumbers =
 (fromJust
  . fmap (map fst)
  . sequence
  . map BS.readInt
  . BS.words) `fmap` BS.getLine

readNumber = head `fmap` readNumbers

binaryList :: Int -> [Int]
binaryList n0 =
  reverse $
  unfoldr (\n ->
              if n > 0
                 then Just (n `mod` 2, n `div` 2)
                 else Nothing)
            n0

friendship :: Int -> Int -> Int
friendship fedor other =
  sum (binaryList (xor fedor other))

main :: IO ()
main = do
  (_n:m:k:_) <- readNumbers
  others <- replicateM m readNumber
  fedor  <- readNumber
  print (length (filter (<= k)
                        (map (friendship fedor) others)))

import Control.Applicative
import Data.List
import Data.Bits

main= do
	[n,m,k]<- map read. words <$> getLine ::IO [Int]
	s<- map read. words <$> getContents ::IO [Int]
	let s1= init s
	let f = last s
	print $ length $ filter (<=k) $ map (popCount.xor f) s1

main = interact $ show . sol . map read . words

sol (_:_:k:xs) = (subtract 1) $ length $ filter id $ map (\x -> k >= (dif x $ last xs)) xs

dif 0 0 = 0
dif a b
    | odd a == odd b = nx
    | otherwise      = nx+1
    where nx = dif (a `div` 2) (b `div` 2)

import Data.Bits

main :: IO ()
main = do
        _:_:k:xs <- getReadAll
        print $ solve k xs

solve :: Int -> [Int] -> Int
solve k xs = length $ filter (\x-> popCount(xor fedor x) <= k) rest
    where
        rest = init xs
        fedor = last xs

getReadAll :: (Read a) => IO [a]
getReadAll = fmap (fmap read. words) getContents

import Control.Monad

type BinaryArmy = [Int]

getArmies :: Int -> IO [Int]
getArmies n = replicateM n getArmy

getArmy :: IO Int
getArmy = do
    army <- getLine
    return $ read army

countFriends :: Int -> [Int] -> Int -> Int
countFriends army [] k = 0
countFriends army (player:others) k = (if binaryDifference binaryArmy binaryPlayer > k
                                      then 0
                                      else 1) + countFriends army others k
    where binaryArmy = toBinary army
          binaryPlayer = toBinary player

toBinary :: Int -> BinaryArmy
toBinary 0 = []
toBinary n = (n `mod` 2) : toBinary (n `div` 2)

binaryDifference :: BinaryArmy -> BinaryArmy -> Int
binaryDifference [] [] = 0
binaryDifference [] binary = sum binary
binaryDifference binary [] = sum binary
binaryDifference (x:xs) (y:ys) = (if x == y
                                 then 0
                                 else 1) + binaryDifference xs ys

main :: IO ()
main = do
    input <- getLine
    let [n, m, k] = map read (words input)
    armies <- getArmies m
    _army <- getLine
    let army = read _army
    putStrLn $ show $ countFriends army armies k

import Control.Monad
main = interact $ show . solve . map read . words
solve(_:_:k:xs)=(length $ filter (\y->k>=(diff y $ last xs)) xs)-1
diff 0 0 = 0
diff x y = (diff (div x 2) (div y 2))+a
	where a=if odd x==odd y then 0 else 1

import Data.Bits

main :: IO()
main = print . solve . map read . words =<< getContents

solve :: [Int] -> Int
solve (_:m:k:x) = solve' (init x) (last x)
    where solve' :: [Int] -> Int -> Int
          solve' []     _ = 0
          solve' (x:xs) l | popCount (xor x l) <= k = 1 + solve' xs l
                          | otherwise               = solve' xs l

import Data.List
import Data.Function
import Data.Char
import Data.Maybe
import Data.Bits
import qualified Data.ByteString.Lazy.Char8 as B

getIntArray :: B.ByteString -> [Int]
getIntArray = map fst . mapMaybe B.readInt . B.words

main = interact $ show . solve . map read . words

solve :: [Int] -> Int
solve (n:m:k:xs) = sum $ map (\x -> if (popCount $ xor x l) <= k then 1 else 0) (init xs)
	where l = last xs

import Data.Bits

main = do
  [n, m, k] <- fmap (map read . words) getLine
  xs <- fmap (map read . lines) getContents :: IO [Int]

  let
    x = last xs

  print $ length $ filter (\y -> popCount (x `xor` y) <= k) $ init xs

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Bits
--import Data.Bits.Bitwise as Bit
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint = getLine>>= \ns-> let [x,y,z]= map read $ words ns  in (solve.(++) [z].concat =<< forM [1 .. y+1] ( \i -> map (fst.fromJust.C.readInt).C.words <$> C.getLine))
solve::[Int]->IO()
solve (x:xs) = let l=last xs in  print $ sum $ map (\a->if popCount (xor a l)<=x then 1 else 0) (init xs)

import Control.Applicative
import Data.Bits
import Control.Monad

solve :: Int -> [Int] -> Int -> Int
solve m ms k = length $ filter ((<=k).popCount.xor m) ms

main = do
    [_,m,k] <- fmap read.words <$> getLine
    ms <- replicateM (m+1) (read <$> getLine)
    print $ solve (last ms) (init ms) k

import Data.Bits
import Control.Applicative
import qualified Data.List as L

main = do
  [n,m,k] <- map read . words <$> getLine :: IO [Int]
  xs <- map read . lines <$> getContents
  let (ls,[fed]) = L.splitAt m xs :: ([Int], [Int])
      diff = map (popCount . xor fed) ls
  print . sum $ map (\x -> if x <= k then 1 else 0) diff


import qualified Data.Map as M
import Data.Map((!))

import qualified Data.Set as S
import qualified Data.List as L
import Data.Bits((.&.), (.|.), xor)

main = do
	t <- getLine >>= return . read :: IO Integer
	let loop t = if t == 0 then return () else do
		[n, m, x] <- getLine >>= return . (fmap read) . words
		print $ 1 + (mod (x-1) n) * m + (div (x-1) n)
		loop $ t - 1
	loop t

{-# LANGUAGE LambdaCase #-}

import Control.Applicative (liftA2)
import Control.Arrow ((>>>))
import Control.Monad.State
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromJust, fromMaybe)

main :: IO ()
main = C.interact $ runScanner (numberOf (C.pack <$> testCase)) >>> C.unlines

testCase :: Scanner String
testCase = show . solve <$> three integer

type I64 = Integer

solve :: [I64] -> I64
solve [n, m, x] = let (c, r) = (x - 1) `divMod` n in 1 + r * m + c

--- Template ---
type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith t s = evalState s . t

peek :: Scanner C.ByteString
peek = gets head

str :: Scanner C.ByteString
str = get >>= \case s : ss -> put ss >> return s

int :: Scanner Int
int = fst . fromJust . C.readInt <$> str

integer :: Scanner Integer
integer = read . C.unpack <$> str

double :: Scanner Double
double = read . C.unpack <$> str

decimal :: Int -> Scanner Int
decimal p = round . ((10 ^ p) *) <$> double

numberOf :: Scanner a -> Scanner [a]
numberOf s = int >>= flip replicateM s

many :: Scanner a -> Scanner [a]
many s = get >>= \case [] -> return []; _ -> (:) <$> s <*> many s

till :: (C.ByteString -> Bool) -> Scanner a -> Scanner [a]
till p s = do
  t <- peek
  if p t then return [] else (:) <$> s <*> till p s

times :: Int -> Scanner a -> Scanner [a]
times = replicateM

(><) = times

two, three, four :: Scanner a -> Scanner [a]
[two, three, four] = map times [2 .. 4]

pair :: Scanner a -> Scanner b -> Scanner (a, b)
pair = liftA2 (,)

import Control.Monad

searchByRows target m = ((div (target - 1) m) + 1, (mod (target - 1) m) + 1)

buildByColumn n (x, y) = (+ x) $ (y - 1) * n

solve n m target = buildByColumn m $ searchByRows target n

main = do
  n <- read <$> getLine
  replicateM_ n $ do
    [n, m, target] <- map read . words <$> getLine
    print $ solve n m target

{-# LANGUAGE BlockArguments #-}

module Main where
import Control.Monad

main :: IO()
main =
  readLn >>= flip replicateM_ do
    [n, m, x] <- fmap read.words <$> getLine
    let prev = x - 1
    let (col, row) = quotRem prev n
     in print $ row * m + col + 1

module Main where

solve :: [Integer] -> IO Integer
solve (n : m : x : _) = return $ row * m + col + 1
  where
    prev = x - 1
    col = prev `div` n
    row = prev `mod` n

output :: [IO Integer] -> IO ()
output collection = do
  out <- head collection
  print out
  if not (null $ tail collection)
    then output $ tail collection
    else pure()

main :: IO ()
main = do
  t <- read <$> getLine :: IO Int
  let ans = replicate t ((map read . words <$> getLine :: IO [Integer]) >>= solve)
    in output ans

module Main where

readArguments :: IO [Integer]
readArguments = do
  line <- getLine
  let array = map read (words line) :: [Integer]
    in return array

solve :: IO [Integer] -> IO Integer
solve arr = do
  params <- arr
  let (n : m : x : _) = params
  let prev = x - 1
  let col = prev `div` n
  let row = prev `mod` n
    in return $ row * m + col + 1

output :: [IO Integer] -> IO ()
output collection = do
  out <- head collection
  print out
  if not (null $ tail collection)
    then output $ tail collection
    else pure()

main :: IO ()
main = do
  t <- getLine
  let count = read t :: Int
  let solves = replicate count (solve readArguments)
   in output solves

{-# LANGUAGE TypeApplications #-}

import Control.Monad
main :: IO ()
main = do
    cases <- (read @Int) <$> getLine
    replicateM_ cases solve

solve :: IO ()
solve = do
    [r, c, x] <- map (read @Integer) . words <$> getLine
    let which_row = ((x - 1) `mod` r) + 1
        which_col = (x - 1) `div` r + 1
    print $ (which_row - 1) * c + which_col

{-# LANGUAGE BlockArguments #-}

import Control.Monad

main :: IO ()
main = readLn >>= flip replicateM_ do
  [n, m, a] <- fmap read . words <$> getLine
  let
    (x, y) = quotRem (a - 1) n
  print $ y * m + x + 1

module Main where

readArguments :: IO [Int]
readArguments = do
  line <- getLine
  let array = map read (words line) :: [Int]
    in return array

solve :: IO [Int] -> IO Int
solve arr = do
  params <- arr
  let (n : m : x : _) = params
  let prev = x - 1
  let col = prev `div` n
  let row = prev `mod` n
    in return $ row * m + col + 1

output :: [IO Int] -> IO ()
output collection = do
  out <- head collection
  print out
  if not (null $ tail collection)
    then output $ tail collection
    else pure()

main :: IO ()
main = do
  t <- getLine
  let count = read t :: Int
  let solves = replicate count (solve readArguments)
   in output solves

{-# LANGUAGE TypeApplications #-}

import Control.Monad
main :: IO ()
main = do
    cases <- (read @Int) <$> getLine
    replicateM_ cases solve

solve :: IO ()
solve = do
    [r, c, x] <- map (read @Int) . words <$> getLine
    let which_row = ((x - 1) `mod` r) + 1
        which_col = (x - 1) `div` r + 1
    print $ (which_row - 1) * c + which_col

{-# LANGUAGE TypeApplications #-}

import Control.Monad
main :: IO ()
main = do
    cases <- (read @Int) <$> getLine
    replicateM_ cases solve

solve :: IO ()
solve = do
    [r, c, x] <- map (read @Int) . words <$> getLine
    let which_row = (x - 1) `mod` c
        which_col = (x - 1) `div` r
    print $ which_row * r + which_col + 1


solve :: [Int] -> String
solve xs = if 2 * maximum xs == sum xs then "Yes" else "No"

testCase = do
    numList <- words `fmap` getLine
    let nums = read `map` numList :: [Int]
    putStrLn $ solve nums
    return 0

repeatN 0 m = return []

repeatN n m = do
    x1 <- m
    x2 <- repeatN (n - 1) m
    return []

main = do
    n <- read `fmap` getLine :: IO Int
    repeatN n testCase

import Control.Monad (forM_)

readInts :: IO [Int]
readInts = fmap (map read . words) getLine

main = do
  n <- readLn :: IO Int
  forM_
    [1 .. n]
    ( \_ -> do
        [x, y, z] <- readInts
        putStrLn $
          if x + y == z || y + z == x || x + z == y
            then "YES"
            else "NO"
    )


import Data.List
import Control.Monad

process :: [[Int]] -> Int
process aps =
  let
    sorted = sort aps
    pos = (map (head . tail) $ filter (\[x,y] -> x > 0) sorted) ++ [0]
    neg = (map (head . tail) . reverse $ filter (\[x,y] -> x < 0) sorted) ++ [0]
  in sum $ zipWith (+) pos neg

main = do
  n <- readLn
  aps <- liftM (map (map read . words) . take n . lines) getContents
  putStrLn . show $ process aps

import Data.List
answer::[(Int,Int)]->Int
answer a = (sumData (getLen lsx0 lsx1) sx0) + (sumData (getLen lsx1 lsx0) sx1)
  where (x0,x1) = partition (\x -> (fst x) < 0) a
        sx0 = sortData (map (\x-> (negate (fst x),snd x)) x0)
        sx1 = sortData x1
        lsx0 = length sx0
        lsx1 = length sx1
getLen::Int -> Int -> Int
getLen l0 l1 | l0 == l1 = l0
             | l0  < l1 = l0
             | otherwise = l1+1
sumData::Int -> [(Int,Int)] -> Int
sumData 0 a = 0
sumData n a = (snd (head a)) + (sumData (n-1) (tail a))
sortData::[(Int,Int)]->[(Int,Int)]
sortData a = sortBy (\x y -> compare (fst x) (fst y)) a
readInput::Int -> IO [(Int,Int)]
readInput 0 = return []
readInput n = do
  winput <- getLine
  let w = words winput
  let x0 = read (w !! 0)::Int
  let x1 = read (w !! 1)::Int
  next <- readInput (n-1)
  return ([(x0,x1)] ++ next)
main = do
  w <- getLine
  let n = read w::Int
  a <- readInput n
  print $ answer a

{-# LANGUAGE TypeApplications #-}

import Control.Monad (replicateM)
import Data.List (partition, sortOn)

main :: IO ()
main = do
  n <- read @ Int <$> getLine
  apples <- replicateM n $ (\[x, y] -> (x, y)) . fmap (read @ Int) . words <$> getLine
  print $ solve n apples

solve :: Int -> [(Int, Int)] -> Int
solve n apples = maximum [goLeftCount, goRightCount]
  where
    (leftApples, rightApples) = partition ((< 0) . fst) . sortOn fst $ apples
    leftCount  = length leftApples
    rightCount = length rightApples

    minSide = minimum [leftCount, rightCount]

    goLeftCount  = gatherApples $ take (minSide + 1) (reverse leftApples) ++ take minSide rightApples
    goRightCount = gatherApples $ take minSide (reverse leftApples) ++ take (minSide + 1) rightApples

gatherApples :: [(Int, Int)] -> Int
gatherApples = sum . fmap snd

import Data.List (sort, sortBy)

main :: IO ()
main = print . solve . map (toTuple . map read . words) . tail . lines =<< getContents

solve :: [(Int, Int)] -> Int
solve xas = sum $ zipWith (\(_, x) (_, y) -> x + y) (sort (filter ((>0) . fst) xas) ++ [(0, 0)]) (sortBy (flip compare) (filter ((<0) . fst) xas) ++ [(0, 0)])

toTuple :: [a] -> (a, a)
toTuple xs = (head xs, xs !! 1)

import Data.List
import Data.Function

main = do
    n <- fmap read getLine
    trees <- mapM (\_ -> fmap ((\[a,b]->(a,b)) . map read . words) getLine) [1..n]
    print $ solve trees

solve :: [(Int, Int)] -> Int
solve trees | l1 == l2 = sum . map snd $ trees
            | l1 < l2 = sum (map snd neg) + sum (map snd (take (l1+1) pos))
            | l1 > l2 = sum (map snd pos) + sum (map snd (take (l2+1) (reverse neg)))
    where sorted = sortBy (compare `on` fst) trees
          (neg, pos) = span (\(a,_) -> a < 0) sorted
          l1 = length neg
          l2 = length pos

import Data.List

furry (a:_:c) = a:furry c
furry (h:_) = [h]
furry _ = []

fuck :: [Int] -> String
fuck (n:s) = let
    li = take (n*2) s
    lu = zip (furry li) $ furry $ tail li
    ze = (0,0)
    in show $ sum $ zipWith (\(_,x) (_,y) -> x + y)
        ((sort [x | x <- lu, x > ze]) ++ [ze])
        ((reverse $ sort [x | x <- lu, x < ze]) ++ [ze])

main = getContents >>= putStrLn . fuck . map read . words

import Data.List

main :: IO ()

furry :: [Int] -> [Int]
furry (a:b:c) = a:furry c
furry (h:t) = [h]
furry [] = []

fuck :: [Int] -> String
fuck (n:s) = let
    li = take (n*2) s
    lu = zip (furry li) $ furry $ tail li
    in show $ sum $ zipWith (\x y -> (snd x) + (snd y))
        ((sort [x | x <- lu, x > (0,0)]) ++ [(0,0)])
        ((reverse $ sort [x | x <- lu, x < (0,0)]) ++ [(0,0)])

main = getContents >>= putStrLn . fuck . map (read::String->Int) . words

import Control.Applicative
import Data.List
import Data.Char

process u1 u2 n | abs(length u1 -length u2)<=1 = (length u1, length u2)
                | length u1> length u2 = (length u2+1, length u2)
                | otherwise = (length u1, length  u1+1)

main= do
	n<- read <$>getLine  :: IO Int
	s<- map (map read). map words. lines <$> getContents ::IO [[Int]]
	let t= sort $ map (\z->(head z,last z)) s
	let u1 =   filter (\z-> fst z>0) t
	let u2 = reverse $ filter (\z->fst z <0) t
	let (v1,v2)=process u1 u2 n
	let w1 = sum $ map snd $ take v1 u1
	let w2= sum $ map snd $ take v2 u2
	print $ w1+w2

import Control.Applicative
import Data.List
import Data.Char

process n u | n==u*2 = (0,n)
            | n>u*2 = (n-u-u-1,n)
	    | otherwise = (0, u+u+1)

main= do
	n<- read <$>getLine  :: IO Int
	s<- map (map read). map words. lines <$> getContents ::IO [[Int]]
	let t= sort $ map (\z->(head z,last z)) s
	let u = length $ filter (\z-> fst z>0) t
	let (v1,v2)=process n u
	print $ sum $ map snd $ take v2  $ drop v1 t

import Control.Monad

process :: [[Int]] -> Int
process aps =
  let
    pos = (map (head . tail) $ filter (\[x,y] -> x > 0) aps) ++ [0]
    neg = (map (head . tail) $ filter (\[x,y] -> x < 0) aps) ++ [0]
  in sum $ zipWith (+) pos neg

main = do
  n <- readLn
  aps <- liftM (map (map read . words) . take n . lines) getContents
  putStrLn . show $ process aps


module Main where

import Control.Monad
import Text.Read
import Data.List

data Pt = Pt { ptX, ptY, ptZ :: !Int } deriving Show

instance Read Pt where
    readPrec = do
        x <- readPrec :: ReadPrec Int
        y <- readPrec :: ReadPrec Int
        z <- readPrec :: ReadPrec Int
        return $ Pt x y z

main = do
    n <- read `liftM` getLine
    pos <- replicateM n $ read `liftM` getLine :: IO [Pt]

    --putStrLn $ show pos

    let o = head pos
        res = foldr (\p (res, ps) ->
                let po = distance p o
                    res' = foldl' (\res q ->
                            let pq = distance p q
                                qo = distance q o
                            in min ( po + pq + qo ) res
                        ) res ps
                in (res', p:ps)
            ) (1e30, []) $ tail pos

    putStrLn $ show ( fst res / 2 )

sqr x = x * x

distance :: Pt -> Pt -> Double
distance a b = sqrt $ fromIntegral $ sqr ( ptX a - ptX b ) + sqr ( ptY a - ptY b ) + sqr ( ptZ a - ptZ b )

import Array
import Data.List
main = interact solve
solve input = output where
  [n]:c:cs = map (map (read::String->Int)) $ map words $ lines input
  output = show $ answer ds / 2
  ds = [(x,dist c x)|x<-cs]
  dist x y = sqrt(
    read ( show $ sum $ map (^2) $ zipWith (-) x y ) ::Double)
  answer [x,y] = distSum x y
  answer (x:xs) = foldl' (min' x) (answer xs) xs
  min' (x,u) a (y,v) = if a<=u+v then a else min a $ distSum (x,u) (y,v)
  distSum (x,u) (y,v) = dist x y + u + v


import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $lines
    >>> drop 1
    >>> map (words >>> map read >>> solve >>> show)
    >>> unlines

solve :: [Int] -> Int
solve [n, s] = s `div` (n `div` 2 + 1)

import Control.Monad

solve :: IO ()
solve = do
    [n, s] <- readMany :: IO [Int]
    print $ s `div` (n `div` 2 + 1)

readMany :: Read a => IO [a]
readMany = map read . words <$> getLine

main :: IO ()
main = readLn >>= flip replicateM_ solve

import qualified Data.ByteString.Lazy.Char8 as P
import Data.ByteString.Builder
import qualified Data.ByteString.Builder.Prim as Prim
import System.IO
import Control.Monad.Trans.State

--import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import Control.Monad
--import Data.List
--import Data.Array.Unboxed

mainFun :: SO
mainFun = do
  ~[t] <- getInts 1
  fmap mconcat $ replicateM t $ do
    ~[n, s] <- getInts 2
    let
      nplaces = div (n + 2) 2
      score = div s nplaces
    pure $ putInts [score]

type SP = State [P.ByteString]
type SO = SP Builder

getNext :: Int -> SP [P.ByteString]
getNext = state . splitAt

getInts :: Int -> SP [Int]
getInts k = do
  vs <- getNext k
  pure [v | Just (v, _) <- map P.readInt vs]

putInts :: [Int] -> Builder
putInts vs = let
  sepPrim = (,) ' ' Prim.>$<
    Prim.liftFixedToBounded Prim.char7 Prim.>*< Prim.intDec
  in  case  vs  of
  [] -> char7 '\n'
  x : xs -> intDec x <> Prim.primMapListBounded sepPrim xs <> char7 '\n'

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  inp <- P.getContents
  let  outp = evalState mainFun $ P.words inp
  P.putStr $ toLazyByteString outp


{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Monad.Trans.State.Strict
import qualified Data.Set as S
import qualified Data.IntMap as M
import Data.Array
import Data.Int

main = B.interact exec
exec = B.pack . evalState app . B.words
app = do
  n <- poi
  m <- poi
  bounty <- p64
  increase <- p64
  damage <- poi
  es <- replicateM n $ liftM3 Enemy poi poi poi
  eus <- replicateM m $ liftM3 (\t i h -> (i, Updt t h)) poi poi poi
  return $ show $ solve n bounty increase damage es eus
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  p64 = fmap to64 poi
  int = maybe undefined fst . B.readInt

data Updt = Updt { ut :: !Int, uh :: !Int } deriving (Show, Eq, Ord)
data Enemy = Enemy { emax :: !Int, estart :: !Int, eregen :: !Int } deriving (Show, Eq, Ord)

hi = 2000000000 :: Int

solve n bounty increase damage es eus0
  | increase > 0 && any (\(e, us) -> emax e <= damage || eregen e == 0 && uh (last us) <= damage) (zip es eus) = -1
  | otherwise = fst $ M.foldlWithKey (\(!r, !a) t (e, l) -> (max r $ (to64 a + to64 e) * (bounty + increase * to64 t), a + e - l)) (0, 0 :: Int) delta
  where
  eus = map S.toAscList $ elems $ accumArray (flip S.insert) S.empty (1, n) $ map (\(i, e) -> (i, Updt 0 (estart e))) (zip [1..] es) ++ eus0
  delta = foldl (\(!delta) (e, us) -> foldl (\(!delta) (u, v) -> let dt = min (ut v - ut u - 1) (if eregen e == 0 then hi else (min damage (emax e) - uh u) `quot` eregen e) in M.insertWith pairAdd (ut u) (1 :: Int, 0 :: Int) $ M.insertWith pairAdd (ut u + dt) (0, 1) delta) delta $ filter ((<= damage) . uh . fst) $ zip us $ tail us ++ [Updt hi 0]) M.empty $ zip es eus

pairAdd (a, b) (c, d) = (a + c, b + d)
to64 x = fromIntegral x :: Int64

{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString.Lazy.Char8 as B
import Control.Monad
import Control.Monad.Trans.State.Lazy
import qualified Data.Set as S
import qualified Data.IntMap as M
import Data.Array
import Data.Int

main = B.interact exec
exec = B.pack . evalState app . B.words
app = do
  n <- poi
  m <- poi
  bounty <- p64
  increase <- p64
  damage <- poi
  es <- replicateM n $ liftM3 Enemy poi poi poi
  eus <- replicateM m $ liftM3 (\t i h -> (i, Updt t h)) poi poi poi
  return $ show $ solve n bounty increase damage es eus
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  p64 = fmap to64 poi
  int = maybe undefined fst . B.readInt

data Updt = Updt { ut :: !Int, uh :: !Int } deriving (Show, Eq, Ord)
data Enemy = Enemy { emax :: !Int, estart :: !Int, eregen :: !Int } deriving (Show, Eq, Ord)

hi = 2000000000 :: Int

solve n bounty increase damage es eus0
  | increase > 0 && any (\(e, us) -> emax e <= damage || eregen e == 0 && uh (last us) <= damage) (zip es eus) = -1
  | otherwise = fst $ M.foldlWithKey (\(!r, !a) t (e, l) -> (max r $ (to64 a + to64 e) * (bounty + increase * to64 t), a + e - l)) (0, 0 :: Int) delta
  where
  eus = map S.toAscList $ elems $ accumArray (flip S.insert) S.empty (1, n) $ map (\(i, e) -> (i, Updt 0 (estart e))) (zip [1..] es) ++ eus0
  delta = foldl (\(!delta) (e, us) -> foldl (\(!delta) (u, v) -> let dt = min (ut v - ut u - 1) (if eregen e == 0 then hi else (min damage (emax e) - uh u) `quot` eregen e) in M.insertWith pairAdd (ut u) (1 :: Int, 0 :: Int) $ M.insertWith pairAdd (ut u + dt) (0, 1) delta) delta $ filter ((<= damage) . uh . fst) $ zip us $ tail us ++ [Updt hi 0]) M.empty $ zip es eus

pairAdd (a, b) (c, d) = (a + c, b + d)
to64 x = fromIntegral x :: Int64

{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Monad.Trans.State.Lazy
import qualified Data.Set as S
import qualified Data.IntMap as M
import Data.Array
import Data.Int

main = B.interact exec
exec = B.pack . evalState app . B.words
app = do
  n <- poi
  m <- poi
  bounty <- p64
  increase <- p64
  damage <- poi
  es <- replicateM n $ liftM3 Enemy poi poi poi
  eus <- replicateM m $ liftM3 (\t i h -> (i, Updt t h)) poi poi poi
  return $ show $ solve n bounty increase damage es eus
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  p64 = fmap to64 poi
  int = maybe undefined fst . B.readInt

data Updt = Updt { ut :: !Int, uh :: !Int } deriving (Show, Eq, Ord)
data Enemy = Enemy { emax :: !Int, estart :: !Int, eregen :: !Int } deriving (Show, Eq, Ord)

hi = 2000000000 :: Int

solve n bounty increase damage es eus0
  | increase > 0 && any (\(e, us) -> emax e <= damage || eregen e == 0 && uh (last us) <= damage) (zip es eus) = -1
  | otherwise = fst $ M.foldlWithKey (\(!r, !a) t (e, l) -> (max r $ (to64 a + to64 e) * (bounty + increase * to64 t), a + e - l)) (0, 0 :: Int) delta
  where
  eus = map S.toAscList $ elems $ accumArray (flip S.insert) S.empty (1, n) $ map (\(i, e) -> (i, Updt 0 (estart e))) (zip [1..] es) ++ eus0
  delta = foldl (\(!delta) (e, us) -> foldl (\(!delta) (u, v) -> let dt = min (ut v - ut u - 1) (if eregen e == 0 then hi else (min damage (emax e) - uh u) `quot` eregen e) in M.insertWith pairAdd (ut u) (1 :: Int, 0 :: Int) $ M.insertWith pairAdd (ut u + dt) (0, 1) delta) delta $ filter ((<= damage) . uh . fst) $ zip us $ tail us ++ [Updt hi 0]) M.empty $ zip es eus

pairAdd (a, b) (c, d) = (a + c, b + d)
to64 x = fromIntegral x :: Int64


{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE ViewPatterns, TupleSections, FlexibleContexts, BangPatterns        #-}

import           Control.Applicative
import           Control.Monad         hiding (forM, forM_, mapM_)
import           Data.Array.IO
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as C
import           Data.Foldable
import           Data.Function
import           Data.Functor
import qualified Data.IntMap.Strict    as M
import qualified Data.Set    as S
import           Data.List             hiding (concat, foldl', product, sum, concatMap)
import           Data.Maybe
import           Data.Traversable
import           Text.Printf           (printf)
import Data.Int
import Control.Arrow

import           Prelude               hiding (concat, product, sum, mapM_, concatMap)

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

-----

-- type UF = IORef (M.HashMap Int (Int, Int))
type UF a = (IOUArray Int Int, IOUArray Int a)

newUF :: MArray IOUArray a IO => Int -> a -> IO (UF a)
newUF n v =
  (,) <$> newListArray (0, n-1) [0..n-1]
      <*> newListArray (0, n-1) (replicate n v)

root :: MArray IOUArray a IO => UF a -> Int -> IO (Int, a)
root (uf, wf) v = do
  p <- readArray uf v
  if p == v
    then (p, ) <$> readArray wf v
    else do
      (r, w) <- root (uf, wf) p
      writeArray uf v r
      return (r, w)

unite :: (MArray IOUArray a IO, Num a) => UF a -> Int -> Int -> IO ()
unite (uf, wf) v w = do
  (r, m) <- root (uf, wf) v
  (s, n) <- root (uf, wf) w
  writeArray uf r s
  writeArray wf s (m+n)

nubOrd :: Ord a => [a] -> [a]
nubOrd = S.toList . S.fromList

main :: IO ()
main = do
  [n, _m] <- getInts
  vs <- getInts
  ns' <- readInts <$> C.getContents

  let ccc [] = []
      ccc (x:y:z) = (x, y) : ccc z

      !es = ccc $ map pred ns'
      -- !g = M.fromListWith (++) [ (f, [t]) | (f, t) <- es ]

  g <- newArray (0, n-1) [] :: IO (IOArray Int [Int])
  for_ es $ \(f, t) -> do
    writeArray g f . (t:) =<< readArray g f
    writeArray g t . (f:) =<< readArray g t

  uf <- newUF n (1 :: Int)
  ss <- newArray (0, n-1) False :: IO (IOUArray Int Bool)

  let ws = sortBy (flip compare) $ zip vs [0..]

      f !acc (v, ix) = do
        us' <- readArray g ix
        us <- filterM (readArray ss) us'
        rs <- forM us $ \u ->
          root uf u

        forM_ us $ \u -> do
          (r1, _n1) <- root uf ix
          (r2, _n2) <- root uf u
          when (r1 /= r2) $ unite uf u ix

        let sums = tail $ scanr (+) 0 ns

            go [] _ !acc = acc
            go (x:xs) (y:ys) !acc = go xs ys (acc + x * y)

            !ns = map (fromIntegral . snd) $ nubOrd rs

        writeArray ss ix True
        return $! acc + v * (sum ns + go ns sums 0)

  ans <- foldM f (0 :: Double) $ map (first fromIntegral) ws

  printf "%.10f\n" (ans / fromIntegral n / fromIntegral (n-1) * 2 :: Double)

{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}

import           Control.Applicative
import           Control.Arrow
import           Control.Monad         hiding (forM, forM_, mapM_)
import           Data.Array.IO
import qualified Data.ByteString.Char8 as C
import           Data.Foldable
import           Data.Function
import           Data.Int
import           Data.List             hiding (concat, concatMap, foldl',
                                        product, sum)
import           Data.Maybe
import qualified Data.Set              as S
import           Data.Traversable
import           Text.Printf           (printf)

import           Prelude               hiding (concat, concatMap, mapM_,
                                        product, sum)

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

-----

type UnionFind = IOArray Int (Int, Int)

newUF :: Int -> IO UnionFind
newUF n = newListArray (0, n-1) $ map (,1) [0..n-1]

root :: UnionFind -> Int -> IO (Int, Int)
root uf v = do
  (p, _) <- readArray uf v
  if p == v
    then readArray uf v
    else do
      (r, w) <- root uf p
      writeArray uf v (r, w)
      return (r, w)

unite :: UnionFind -> Int -> Int -> IO ()
unite uf v w = do
  (r, m) <- root uf v
  (s, n) <- root uf w
  writeArray uf r (s, m+n)
  writeArray uf s (s, m+n)

nubOrd :: Ord a => [a] -> [a]
nubOrd = S.toList . S.fromList

main :: IO ()
main = do
  [n, _m] <- getInts
  vs <- getInts
  ns' <- readInts <$> C.getContents

  let ccc [] = []
      ccc (x:y:z) = (x, y) : ccc z

      !es = ccc $ map pred ns'
      -- !g = M.fromListWith (++) [ (f, [t]) | (f, t) <- es ]

  g <- newArray (0, n-1) [] :: IO (IOArray Int [Int])
  for_ es $ \(f, t) -> do
    writeArray g f . (t:) =<< readArray g f
    writeArray g t . (f:) =<< readArray g t

  uf <- newUF n
  ss <- newArray (0, n-1) False :: IO (IOUArray Int Bool)

  let ws = sortBy (flip compare) $ zip vs [0..]

      f !acc (v, ix) = do
        us' <- readArray g ix
        us <- filterM (readArray ss) us'
        rs <- forM us $ \u ->
          root uf u

        forM_ us $ \u -> do
          (r1, _n1) <- root uf ix
          (r2, _n2) <- root uf u
          when (r1 /= r2) $ unite uf u ix

        let sums = tail $ scanr (+) 0 ns

            go [] _ !acc = acc
            go (x:xs) (y:ys) !acc = go xs ys (acc + x * y)

            !ns = map (fromIntegral . snd) $ nubOrd rs

        writeArray ss ix True
        return $! acc + v * (sum ns + go ns sums 0)

  ans <- foldM f (0 :: Double) $ map (first fromIntegral) ws

  printf "%.10f\n" (ans / fromIntegral n / fromIntegral (n-1) * 2 :: Double)

{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TupleSections         #-}
{-# LANGUAGE TypeFamilies          #-}

import           Control.Applicative
import           Control.Monad         hiding (forM, forM_, mapM_)
import           Data.Array.IO
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as C
import           Data.Function
import           Data.Int
import           Data.List             hiding (concat, concatMap, foldl',
                                        product, sum)
import           Data.Maybe
import qualified Data.Set              as S
import           Text.Printf           (printf)

import           Prelude               hiding (concat, concatMap, mapM_,
                                        product, sum)

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

-----

type UF = IOUArray Int Int

newUF :: Int -> IO UF
newUF n = newListArray (0, n-1) [0..n-1]

root :: UF -> Int -> IO Int
root uf v = do
  p <- readArray uf v
  if p == v
    then readArray uf v
    else do
      r <- root uf p
      writeArray uf v r
      return r

unite :: UF -> Int -> Int -> IO ()
unite uf v w = do
  r <- root uf v
  s <- root uf w
  writeArray uf r s

nubOrd :: Ord a => [a] -> [a]
nubOrd = S.toList . S.fromList

main :: IO ()
main = do
  [n, m] <- getInts
  vs :: UArray Int Int <- listArray (0, n-1) <$> getInts
  es <- replicateM m getInts

  uf <- newUF n
  sz :: IOUArray Int Int <- newArray (0, n) (1 :: Int)

  let f !acc [v, u] = do
        rv <- root uf v
        ru <- root uf u
        if rv == ru
          then return acc
          else do
          unite uf v u
          sv <- readArray sz rv
          su <- readArray sz ru
          rr <- root uf v
          writeArray sz rr $ su + sv
          return $ acc + fromIntegral (min (vs!v) (vs!u)) * fromIntegral su * fromIntegral sv

  ans <- foldM f (0 :: Double)
    $ sortBy (flip compare `on` \[v, u] -> min (vs!v) (vs!u))
    $ map (map pred) es

  printf "%.10f\n" (ans / fromIntegral n / fromIntegral (n-1) * 2 :: Double)

{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies, ViewPatterns, BangPatterns        #-}

import           Control.Applicative
import           Control.Monad hiding (forM, forM_)
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as C
import           Data.Foldable
import           Data.Function
import           Data.Functor
import           Data.Maybe
import           Data.Traversable
import           Text.Printf           (printf)
import Data.List hiding (concat, foldl', sum, product)
import qualified Data.IntMap as M
import Data.IORef
import qualified Data.IntSet as S

import           Prelude hiding (sum, product, concat)

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

-----

type UF = IORef (M.IntMap (Int, Integer))

newUF :: Int -> IO UF
newUF n = newIORef $ M.fromList [ (i, (i, 1)) | i <- [0..n-1] ]

root :: UF -> Int -> IO (Int, Integer)
root ior v = do
  uf <- readIORef ior
  let (p, n) = uf M.! v
  if p == v
    then return (p, n)
    else do
      r <- root ior p
      writeIORef ior $! M.insert v r uf
      return r

unite :: UF -> Int -> Int -> IO ()
unite ior v w = do
  (r, m) <- root ior v
  (s, n) <- root ior w
  modifyIORef' ior $ M.insert s (s, m+n) . M.insert r (s, m+n)

main :: IO ()
main = do
  [n, m] <- getInts
  vs <- getInts
  (concat -> cs) <- replicateM m $ do
    [f, t] <- getInts
    return [(f-1, t-1), (t-1, f-1)]

  let g = M.fromList $ map (\xs -> (fst $ head xs, map snd xs)) $ groupBy ((==) `on` fst) $ sort cs
  uf <- newUF n

  let ws = sortBy (flip compare) $ zip vs [0..]

      f (!ss, !acc) (v, ix) = do
        let us = M.findWithDefault [] ix g
        (map snd . nub -> ns) <- forM (filter (`S.member` ss) us) $ \u ->
          root uf u
        print (v, ix, us, ss, ns)

        for_ (filter (`S.member` ss) us) $ \u -> do
          (r1, n1) <- root uf ix
          (r2, n2) <- root uf u
          when (r1 /= r2) $ unite uf u ix

        let go [] = 0
            go (x:xs) = x * sum xs + go xs

        return (S.insert ix ss, acc + fromIntegral v * (sum ns + go ns))

  (_, ans) <- foldM f (S.empty, 0) ws

  printf "%.10f\n" $
    ((fromIntegral (ans :: Integer) / fromIntegral n / fromIntegral (n-1) * 2) :: Double)

{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE ViewPatterns, TupleSections        #-}

import           Control.Applicative
import           Control.Monad         hiding (forM, forM_, mapM_)
import           Data.Array.IO
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as C
import           Data.Foldable
import           Data.Function
import           Data.Functor
import qualified Data.IntMap.Strict    as M
import qualified Data.IntSet           as S
import           Data.IORef
import           Data.List             hiding (concat, foldl', product, sum)
import           Data.Maybe
import           Data.Traversable
import           Text.Printf           (printf)

import           Prelude               hiding (concat, product, sum, mapM_)

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

-----

-- type UF = IORef (M.HashMap Int (Int, Int))
type UF = (IOUArray Int Int, IOUArray Int Int)

newUF :: Int -> IO UF
newUF n = (,) <$> newListArray (0, n-1) [0..n-1]
              <*> newListArray (0, n-1) (replicate n 1)

root :: UF -> Int -> IO (Int, Int)
root (uf, wf) v = do
  p <- readArray uf v
  if p == v
    then (p, ) <$> readArray wf v
    else do
      (r, w) <- root (uf, wf) p
      writeArray uf v r
      return (r, w)

unite :: UF -> Int -> Int -> IO ()
unite (uf, wf) v w = do
  (r, m) <- root (uf, wf) v
  (s, n) <- root (uf, wf) w
  writeArray uf r s
  writeArray wf s (m+n)

main :: IO ()
main = do
  [n, m] <- getInts
  vs <- getInts
  (concat -> cs) <- replicateM m $ do
    [f, t] <- getInts
    return [(f-1, t-1), (t-1, f-1)]

  let g = M.fromList $ map (\xs -> (fst $ head xs, map snd xs)) $ groupBy ((==) `on` fst) $ sort cs

  uf <- newUF n
  ssr <- newIORef S.empty
  ansr <- newIORef 0

  let ws = sortBy (flip compare) $ zip vs [0..]

      f (v, ix) = do
        ss <- readIORef ssr
        let us = filter (`S.member` ss) $ M.findWithDefault [] ix g
        (map snd . nub -> ns) <- forM us $ \u ->
          root uf u
        -- print (v, ix, us, ss, ns)

        for_ us $ \u -> do
          (r1, _n1) <- root uf ix
          (r2, _n2) <- root uf u
          when (r1 /= r2) $ unite uf u ix

        let go [] = 0
            go (x:xs) = x * sum xs + go xs

        modifyIORef' ssr $ S.insert ix
        modifyIORef' ansr (+ fromIntegral v * fromIntegral (sum ns + go ns))

  mapM_ f ws
  ans <- readIORef ansr

  printf "%.10f\n" (ans / fromIntegral n / fromIntegral (n-1) * 2 :: Double)

{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE ViewPatterns, TupleSections        #-}

import           Control.Applicative
import           Control.Monad         hiding (forM, forM_, mapM_)
import           Data.Array.IO
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as C
import           Data.Foldable
import           Data.Function
import           Data.Functor
import qualified Data.IntMap.Strict    as M
import qualified Data.IntSet           as S
import           Data.IORef
import           Data.List             hiding (concat, foldl', product, sum)
import           Data.Maybe
import           Data.Traversable
import           Text.Printf           (printf)

import           Prelude               hiding (concat, product, sum, mapM_)

-- I/O

readInts :: C.ByteString -> [Int]
readInts = map fst . mapMaybe C.readInt . C.words

getInts :: IO [Int]
getInts = readInts <$> C.getLine

readNums :: Num a => C.ByteString -> [a]
readNums = map (fromIntegral . fst) . mapMaybe C.readInteger . C.words

getNums :: Num a => IO [a]
getNums = readNums <$> C.getLine

-----

-- type UF = IORef (M.HashMap Int (Int, Int))
type UF = (IOUArray Int Int, IOUArray Int Int)

newUF :: Int -> IO UF
newUF n = (,) <$> newListArray (0, n-1) [0..n-1]
              <*> newListArray (0, n-1) (replicate n 1)

root :: UF -> Int -> IO (Int, Int)
root (uf, wf) v = do
  p <- readArray uf v
  if p == v
    then (p, ) <$> readArray wf v
    else do
      (r, w) <- root (uf, wf) p
      writeArray uf v r
      return (r, w)

unite :: UF -> Int -> Int -> IO ()
unite (uf, wf) v w = do
  (r, m) <- root (uf, wf) v
  (s, n) <- root (uf, wf) w
  writeArray uf r s
  writeArray wf s (m+n)

main :: IO ()
main = do
  [n, m] <- getInts
  vs <- getInts
  (concat -> cs) <- replicateM m $ do
    [f, t] <- getInts
    return [(f-1, t-1), (t-1, f-1)]

  let g = M.fromList $ map (\xs -> (fst $ head xs, map snd xs)) $ groupBy ((==) `on` fst) $ sort cs

  uf <- newUF n
  ssr <- newIORef S.empty
  ansr <- newIORef 0

  let ws = sortBy (flip compare) $ zip vs [0..]

      f (v, ix) = do
        ss <- readIORef ssr
        let us = filter (`S.member` ss) $ M.findWithDefault [] ix g
        (map snd . nub -> ns) <- forM us $ \u ->
          root uf u
        -- print (v, ix, us, ss, ns)

        for_ us $ \u -> do
          (r1, _n1) <- root uf ix
          (r2, _n2) <- root uf u
          when (r1 /= r2) $ unite uf u ix

        let go [] = 0
            go (x:xs) = x * sum xs + go xs

        modifyIORef' ssr $ S.insert ix
        modifyIORef' ansr (+ fromIntegral v * fromIntegral (sum ns + go ns))

  mapM_ f ws
  ans <- readIORef ansr

  printf "%.10f\n" (fromIntegral (ans :: Integer) / fromIntegral n / fromIntegral (n-1) * 2 :: Double)