import Data.List
import qualified Data.Map as M
(|>) x f = f x

main = interact solve

solve contents = let
  ls = contents |> lines
  sz = ls |> (!! 0) |> read :: Int
  portions = ls |> (!! 1) |> words |> map read :: [Int]
  in solve' portions sz |> show

solve' portions sz = portions |> sum |> fromIntegral |> (/ (fromIntegral sz))

import Control.Monad

main :: IO ()
main = do
  _ <- getLine
  percentages <- fmap (map read.words) getLine
  let perc_f = map fromIntegral percentages
  print((fromIntegral (sum perc_f)) / (fromIntegral (length perc_f)))

module Main (main)
    where

import Control.Monad (replicateM)

main :: IO ()
main = print . solve . map (map read . words) =<< replicateM 2 getLine
    where solve [[n], xs] = sum xs / n

-- the haskell wiki is amazing!!!
-- https://wiki.haskell.org/Generic_number_type
-- https://wiki.haskell.org/Converting_numbers

-- you have to convert the integers to type Fractional!!!!!!
-- at least this is the way that i found to make it work!!!!

conv lst uu
  | (null lst) = uu
  | otherwise = conv (tail lst) (uu ++ [(read (head lst))::Int])

avg lst sum
  | (null lst) = sum
  | otherwise = avg (tail lst) (sum + (head lst))

main = do
  ww <- getLine
  ff <- getLine
  let aa = conv (words ff) []
      ans = avg aa 0

  putStrLn (show ( realToFrac ans / realToFrac (length aa)))

main :: IO ()
main = do
    n <- readLn :: IO Float
    input <- getLine
    let args = map (\x -> read x :: Float) $ words input
    print $ (sum args)/n

main = do
    getLine
    ps <- fmap (map read . words) getLine
    print $ (fromIntegral $ sum ps) / (fromIntegral $ length ps)

main = do
  getLine
  xs <- fmap (map read . words) getLine
  print $ (sum xs) / (fromIntegral $ length xs)

main = do
    auxN <- getLine
    let n = read auxN :: Double
    auxP <- getLine
    let sumP = read (show (sum (map read $ words auxP :: [Int]))) :: Double
    putStrLn (show (sumP/n))

promedio :: [Double] -> Double
promedio l = sum l / (fromIntegral $ length l)

main = do
    header <- getLine
    numbers <- fmap words getLine
    print $ promedio $ f numbers
        where
            f l = map read l

promedio :: [Double] -> Double
promedio l = (foldr (+) 0 l) / (fromIntegral $ length l)

main = do
    header <- getLine
    numbers <- fmap words getLine
    print $ promedio $ f numbers
        where
            f l = map read l

import Data.List
import Data.Maybe
import Data.Char
import Data.Function
main=   readsolveprint
readsolveprint::IO()
readsolveprint =  interact $ solve.concat.map (map read.words).take 2. lines
solve :: [Integer]->String
solve (x:xs) =  show $ (/fromIntegral x) $ fromIntegral $ sum xs

import Data.List
main=do
	a<-getLine
	t<-fmap (map read.words) getLine
	print $ sum t / genericLength t

solve :: Int -> [Double] -> Double
solve n xs  =  sum xs / (fromIntegral n)

main :: IO ()
main  =  do
    n <- readLn
    xs  <-  getLine >>= return . map read . words
    print $ solve n xs

import Control.Monad

main = readInput >>= putStrLn . show . (\(n, ps) -> sum ps / n)

readInput :: IO (Double, [Double])
readInput = liftM2 toTuple getLine getLine
  where toTuple a b = (read a, map read $ words b)

import Text.Printf
import Data.List
import Data.Functor
import Control.Applicative
import Prelude
import Control.Monad
import Data.Maybe
import Data.Function
import Data.Array
import qualified Data.ByteString.Char8 as B8
import Data.Foldable (foldr')

readInt :: String -> Int
readInt = Prelude.read

solve :: Int -> [Double] -> Double
solve n arr = sum arr / fromIntegral n

main :: IO ()
main = do
    n <- readInt <$> getLine
    arr <- (map (fromIntegral . readInt) . words) <$> getLine
    let answer = solve n arr
    printf "%.5f\n" answer

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

main=do
     n<-  read <$>  getLine::IO Int
     q<-  sum <$> map read <$> words <$>getLine ::IO Int
     print $ (fromIntegral q)/ (fromIntegral n)

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

solve :: [Double] -> Double
solve (n:ps) = sum ps / n
solve _ = undefined

main=getContents>>=print.e.map read.words
e(n:s)=sum s/n

main=getContents>>=print.solve.map read.words
solve(n:s)=sum s/n

main = do
  n <- readLn
  ps <- fmap (map read . words) getLine
  print (sum ps / n)

solve :: [Int] -> Double
solve ps = fromIntegral (sum ps) / fromIntegral (length ps * 100) * 100

main = do
  getLine -- n
  ps <- fmap (map read . words) getLine
  print $ solve ps

main = do
    n <- fmap read getLine
    p <- fmap (map read . words) getLine
    print $ sum p / n

-- Vicfred
-- https://codeforces.com/problemset/problem/200/B

main :: IO ()
main = do
    n <- readLn :: IO Double
    a <- getLine >>= return . map read . words :: IO [Double]
    print (sum a / n)

import Control.Applicative

main = do
  n <- readLn
  x <- sum.map read.words <$> getLine
  print $ x/n

main = do
    x <- getLine
    items <- map parse . words <$> getLine
    print $ (foldl (+) 0.0 items) / (read x) * 100

parse :: String -> Double
parse x = y / 100
    where y = read x :: Double

calc xs = (sum xs / fromIntegral (100 * length xs)) * 100

main = do s <- getLine
          t <- getLine
          putStrLn $ show $ calc $ map (\x -> read x :: Double) $ words t

calc xs = (sum xs / fromIntegral (100 * length xs)) * 100

main = do s <- getLine
          t <- getLine
          putStrLn $ show $ calc $ map (\x -> read x :: Float) $ words t

import Control.Applicative
import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as C

myprint x1 = putStrLn $ intercalate " " $ map show x1

main= do
	n<- read <$> getLine ::IO Double
	s<- map (fst.fromJust.C.readInteger) . C.words <$> C.getLine ::IO [Integer]
	let ss = sum s
	print $ (fromIntegral ss) / n

main = do
  n <- readLn :: IO Double
  a <- getLine >>= return. map read. words :: IO [Double]
  print (sum a / n)

main :: IO ()
main = do
    n <- toRational . (read :: String -> Int) <$> getLine
    print =<< (fromRational :: Rational -> Double) . (* 100) . (/ n) . sum . map ((/ 100) . toRational . (read :: String -> Int)) . words <$> getLine

--ghc 7.10

average :: (Fractional a) => [a] -> a
average a = (sum a) / (fromIntegral (length a))

main = do
    nStr <- getLine
    aStr <- getLine
    let a = map read . words $ aStr :: [Double]
    print $ average a

main=print.slv.map read.words =<< getContents
slv (n:ps) = (sum.take (floor n) $ ps)/n

main = interact $ show.f.map read.words.last.lines
f xs = (*100.0).(/(fromIntegral.length $ xs)).sum.map (*0.01) $ xs

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

solve :: Int -> [Int] -> Float
solve n x = sum (map fromIntegral x) / fromIntegral n

main =
  do all <- BS.getContents
     let Just (n, r1) = readInt all
     let (x, _)  = readMany readInt r1
     print (solve n x)
  where readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
          Just (x, r) -> let (xs, t) = readMany readf r
                         in  (x : xs, t)
          Nothing     -> ([], s)

main :: IO ()
main = do
    n <- toRational . (read :: String -> Int) <$> getLine
    print =<< (fromRational :: Rational -> Double) . (/ n) . sum . map ((/ 100) . toRational . (read :: String -> Int)) . words <$> getLine


import Data.Int (Int64)

process :: [Int64] -> [(Int64,Int64)]
process xs = zip dmins dmaxs
    where
        cmin = head xs
        cmax = last xs
        dl = zipWith (-) xs (2*cmin-cmax:xs)
        dr = zipWith (-) (tail xs ++ [2*cmax-cmin]) xs
        dmins = zipWith min dl dr
        cmid = (cmin + cmax) `div` 2
        dmaxs = map (\x -> if x <= cmid then cmax-x else x-cmin) xs

readInt :: String -> Int64
readInt = read

main :: IO ()
main = do
    n <- fmap readInt getLine
    xs <- fmap (map readInt.words) getLine
    mapM_ (\(l,r) -> putStrLn (show l ++ " " ++ show r)) $ process xs

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

solve :: [Int] -> [[Int]]
solve (n:x) =
    let   x1 = head x
          xn = last x
    in    solve' x1 xn x 0 True
    where solve' :: Int -> Int -> [Int] -> Int -> Bool -> [[Int]]
          solve' x1 xn (x:xs) _    True = ([(head xs) - x, xn - x]:(solve' x1 xn xs x False))
          solve' x1 xn [x]    last _    = [[(x - last), x - x1]]
          solve' x1 xn (x:xs) last _    = ([(min (x - last) ((head xs) - x)), (max (x - x1) (xn - x))]:(solve' x1 xn xs x False))

output :: [[Int]] -> IO()
output [[a, b]] = output' [a, b]
output (x:xs)   = do output' x
                     output xs

output' :: [Int] -> IO()
output' [a, b] = putStrLn ((show a) ++ " " ++ (show b))

main = do
  n <- readLn
  xs <- fmap (map read . words) getLine

  let
    h = head xs
    l = last xs

    r = [(xs!!1 - h, l - h)] ++
        (zipWith3 f xs (tail xs) (tail $ tail xs)) ++
        [(xs!!(n-1)-xs!!(n-2), l-h)]
      where
        f a b c = (min (b-a) (c-b), max (l-b) (b-h))

  putStrLn $ unlines $ map (\(x, y) -> show x ++ " " ++ show y) r

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Bits
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>>= \n-> (solve.(++) [read n].concat =<< forM [1 ..1] ( \i -> map (fst.fromJust.C.readInteger).C.words <$> C.getLine))
solve (x:xs) = let m=Map.fromList $ zip [1..x] xs in forM_ [1..x] $ \i -> putStrLn $ unwords $ map show (slv1 i m)
    where
    slv1 a m | a==1      = [m Map.! 2 - m Map.! 1, m Map.! x - m Map.! 1]
             | a==x      = [m Map.! x - m Map.! (x-1), m Map.! x - m Map.! 1]
             | otherwise = [minimum [m Map.! a - m Map.! (a-1), m  Map.! (a+1) - m Map.! a], maximum [m Map.! x - m Map.! a, m Map.! a - m Map.! 1]]

import Control.Arrow ((&&&), (***))
import Data.Functor ((<$>))

main :: IO ()
main = mapM_ (putStrLn . uncurry (++) . (show *** (' ':) . show)) =<< solve <$> (tail <$> map read <$> words <$> getContents)

solve :: [Integer] -> [(Integer, Integer)]
solve xs = zip (uncurry (zipWith min) $ (id &&& tail) $ zipWith (-) (xs ++ [10000000000]) (-10000000000 : xs)) [ max (x - head xs) (last xs - x) | x <- xs ]

import Control.Monad (forM_)
main = do
  getLine
  cs <- fmap (map read . words) getLine :: IO [Int]
  let h = head cs
      t = last cs
      toL = zipWith (-) cs (h : cs) :: [Int]
      toR = zipWith (-) (tail cs ++ [t]) cs :: [Int]
  forM_ (zip3 cs toL toR) $ \(c,l,r) ->
    putStrLn $ unwords $ map show [min' l r, max' (c-h) (t-c)]
min' 0 a = a
min' a 0 = a
min' a b = min a b
max' 0 a = a
max' a 0 = a
max' a b = max a b

main = do
    _ <- getLine
    xs <- fmap (map read . words) getLine
    let sol = solve xs
    mapM_ (putStrLn . (\(a,b) -> show a ++ " " ++ show b)) sol

solve :: [Int] -> [(Int, Int)]
solve xs@(x:y:ys) = (y-x, maxv-x) : f x (y:ys)
    where minv = head xs
          maxv = last xs
          f a (b:c:cs) = (min (b-a) (c-b), max (b-minv) (maxv-b)) : f b (c:cs)
          f a [b] = [(b-a, b-minv)]

import System.IO

solve1 :: [Int] -> [Int]
solve1 [x,y] = [ abs $ x-y ]
solve1 (x:y:z:xs) = min (abs $ x-y) (abs $ y-z) : solve1(y:z:xs)

solve2 :: [Int] -> Int -> Int -> [Int]
solve2 (x:xs) y z = max (abs $ x-y) (abs $ x-z) : solve2 xs y z

main = do
	s <- getLine
	let n = read s :: Int
	s <- getLine
	let xs = map read ( words s ) :: [Int]

	let mns = abs( head xs - (head.tail) xs ) : solve1 xs
	let mxs = solve2 xs (head xs) (last xs)

	let ans = zip (map show mns) (map show mxs)
	str_printer ans

str_printer :: [(String,String)] -> IO ()
str_printer [] = return ()
str_printer (x:xs) = do putStr $ (fst x) ++ " "; putStrLn $ snd x ; str_printer xs

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe

mymax s1 s2 a = max (a-s1) (s2-a)
mymin a b c = min (b-a) (c-b)
myprint a b =   intercalate " " $ map show [a,b]

main=do
	getLine
	s<-map (fst.fromJust.C.readInt). C.words<$> C.getLine::IO [Int]
	let s1 = head s
	let s2 = last s
	let maxs = map (mymax s1 s2) s
	let mins1 =   (zipWith3 (\a b c ->mymin a b c) s (tail s) (tail (tail s)))
	let mins2 = ((head (tail s))-(head s)): mins1
	let mins = mins2 ++[ last s- (last (init s))]
	putStrLn $ intercalate "\n" $ zipWith (myprint) mins maxs

module Main where

import Control.Applicative

solve :: [Int] -> Int -> Int -> [(Int, Int)]
solve (x' : x : [x'']) a b = [minMax x x' x'' a b]
solve (x' : x : x'' : xs) a b =
    minMax x x' x'' a b : solve (x : x'' : xs) a b

minMax :: Int -> Int -> Int -> Int -> Int -> (Int, Int)
minMax x x' x'' a b =
    let min' = min (dist x x') (dist x x'')
        max' = max (dist x a) (dist x b)
     in (min', max')

edgeHead :: [Int] -> (Int, Int)
edgeHead xs@(x:x':_) =
    let min' = dist x x'
        max' = dist (head xs) (last xs)
     in (min', max')

edgeTail :: [Int] -> (Int, Int)
edgeTail xs =
    let min' = dist (last xs) (last (init xs))
        max' = dist (head xs) (last xs)
     in (min', max')

dist :: Int -> Int -> Int
dist a b
    | a <= 0 && b <= 0 = abs (abs b - abs a)
    | a >= 0 && b <= 0 = a + abs b
    | a <= 0 && b >= 0 = b + abs a
    | a >= 0 && b >= 0 = abs (b - a)

showTuple :: (Int, Int) -> String
showTuple (a, b) = show a ++ " " ++ show b

main :: IO ()
main = do
    _ <- getLine
    xs <- (map (\s -> read s::Int) . words) <$> getLine
    if length xs == 2
       then do
            putStrLn $ showTuple $ edgeHead xs
            putStrLn $ showTuple $ edgeTail xs
       else do
            putStrLn $ showTuple $ edgeHead xs
            mapM_ (putStrLn . showTuple) (solve xs (head xs) (last xs))
            putStrLn $ showTuple $ edgeTail xs

import Data.List
import Data.Int

main :: IO ()
main = interact $ showMatrix . solve . getList

getList :: (Read a) => String -> [a]
getList = tail . fmap read . words

showMatrix :: (Show a) => [[a]] -> String
showMatrix = unlines . fmap (unwords . fmap show)

solve :: [Int64] -> [[Int64]]
solve xs = transpose [minList, maxList]
    where minList = zipWith3 (\a b c -> min (b - a) (c - b))
                        (-3 * 10 ^ (9::Int64) : xs) xs (tail xs ++ [3*10^(9::Int64)])
          maxList = fmap (\x -> max (lst - x) (x - frst)) xs
            where frst = head xs
                  lst = last xs

import Control.Arrow ((&&&), (***))
import Data.Functor ((<$>))

main :: IO ()
main = mapM_ (putStrLn . uncurry (++) . (show *** (' ':) . show)) =<< solve <$> (tail <$> map read <$> words <$> getContents)

solve :: [Integer] -> [(Integer, Integer)]
solve xs = zip (uncurry (zipWith min) $ (id &&& tail) $ zipWith (-) (xs ++ [last xs + abs (head xs)]) ((head xs - abs (last xs)) : xs)) [ max (x - head xs) (last xs - x) | x <- xs ]

import Control.Arrow ((&&&), (***))
import Data.Functor ((<$>))

main :: IO ()
main = mapM_ (putStrLn . uncurry (++) . (show *** (' ':) . show)) =<< solve <$> (tail <$> map read <$> words <$> getContents)

solve :: [Int] -> [(Int, Int)]
solve xs = zip (uncurry (zipWith min) $ (id &&& tail) $ zipWith (-) (xs ++ [maxBound]) (minBound : xs)) [ max (x - head xs) (last xs - x) | x <- xs ]

import Data.Int (Int64)

process :: [Int64] -> [(Int64,Int64)]
process xs = zip dmins dmaxs
    where
        dl = zipWith (-) xs (minBound:xs)
        dr = zipWith (-) (tail xs ++ [maxBound]) xs
        dmins = zipWith min dl dr
        cmin = head xs
        cmax = last xs
        cmid = (cmin + cmax) `div` 2
        dmaxs = map (\x -> if x <= cmid then cmax-x else x-cmin) xs

readInt :: String -> Int64
readInt = read

main :: IO ()
main = do
    n <- fmap readInt getLine
    xs <- fmap (map readInt.words) getLine
    mapM_ (\(l,r) -> putStrLn (show l ++ " " ++ show r)) $ process xs

process :: [Int] -> [(Int,Int)]
process xs = zip dmins dmaxs
    where
        dl = zipWith (-) xs (minBound:xs)
        dr = zipWith (-) (tail xs ++ [maxBound]) xs
        dmins = zipWith min dl dr
        cmin = head xs
        cmax = last xs
        cmid = (cmin + cmax) `div` 2
        dmaxs = map (\x -> if x <= cmid then cmax-x else x-cmin) xs

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    n <- fmap readInt getLine
    xs <- fmap (map readInt.words) getLine
    mapM_ (\(l,r) -> putStrLn (show l ++ " " ++ show r)) $ process xs

import Data.List

main :: IO ()
main = interact $ showMatrix . solve . getList

getList :: (Read a) => String -> [a]
getList = tail . fmap read . words

showMatrix :: (Show a) => [[a]] -> String
showMatrix = unlines . fmap (unwords . fmap show)

solve :: [Int] -> [[Int]]
solve xs = transpose [minList, maxList]
    where minList = zipWith3 (\a b c -> min (b - a) (c - b)) (minBound : xs) xs (tail xs ++ [maxBound])
          maxList = fmap (\x -> max (lst - x) (x - frst)) xs
            where frst = head xs
                  lst = last xs

import Data.List
import Data.Int

main :: IO ()
main = interact $ showMatrix . solve . getList

getList :: (Read a) => String -> [a]
getList = tail . fmap read . words

showMatrix :: (Show a) => [[a]] -> String
showMatrix = unlines . fmap (unwords . fmap show)

solve :: [Int64] -> [[Int64]]
solve xs = transpose [minList, maxList]
    where minList = zipWith3 (\a b c -> min (b - a) (c - b)) (minBound : xs) xs (tail xs ++ [maxBound])
          maxList = fmap (\x -> max (lst - x) (x - frst)) xs
            where frst = head xs
                  lst = last xs


{-# LANGUAGE BangPatterns #-}

import Data.Array.ST
import Data.Array.Unboxed
import Data.List
import Control.Applicative
import Control.Monad

main = do
  [n,k] <- map read.words <$> getLine
  arr <- listArray ((0,0),(1,n-1)).concat.lines <$> getContents
  putStrLn $ solve n k arr

solve :: Int -> Int -> UArray (Int,Int) Char -> String
solve n k arr = bfs 0 [(0,0)] visited0
   where
     visited0 = listArray ((0,0),(1,n-1)) $ True : repeat False
     bfs !water xs visited
       | null next = "NO"
       | any isGoal next = "YES"
       | otherwise = bfs (water+1) next (update next visited)
      where
        isGoal (x,y) = y>=n-1
        next = nub $ xs >>= childs
        jump (0,y) = (1,y+k)
        jump (1,y) = (0,y+k)
        childs (x0,y0) = do
          (x,y) <- [(x0,y0+1),(x0,y0-1),jump(x0,y0)]
          guard $ water < y
          if y > n-1
             then return (x,y)
             else do
               guard $ not (visited!(x,y)) && arr!(x,y) == '-'
               return (x,y)

update :: [(Int,Int)] -> UArray (Int,Int) Bool -> UArray (Int,Int) Bool
update [] arr = arr
update xs arr = runSTUArray $ do
  a <- unsafeThaw arr
  go a xs
  return a
   where
     go a [] = return ()
     go a (x:xs) = do
       writeArray a x True
       go a xs

{-# LANGUAGE BangPatterns #-}
import qualified Data.ByteString.Char8 as B
import qualified Data.IntSet as S
import Control.Applicative ((<$>))
import Data.Sequence as Seq ((|>),ViewL(..),viewl,singleton,length)

getTwoLines = sequence [B.getLine,B.getLine]

main = do
  [h,k] <- map read . words <$> getLine :: IO [Int]
  xs <- getTwoLines
  let (f,valid,q,vs) = (done h, validNext xs h k
                       ,singleton ((0,0),0),S.empty)
      cst = bfs f valid q vs :: Int
  putStrLn $ if blocked h k xs || cst > h then "NO" else "YES"

blocked h k b = cnt 0 0 == k
  where cnt i c
          | i == h || c == k = c
          | all (=='X') [b?(0,i),b?(1,i)] = cnt (i+1) (c+1)
          | otherwise = cnt (i+1) 0

[b1,_] ? (0,j) = B.index b1 j
[_,b2] ? (1,j) = B.index b2 j

validNext b h k vs ((i,j),t) = filter valid next
    where next = zip [(1-i,j+k),(i,j+1), (i,j-1)] $ repeat (t+1)
          valid ((x,y),iter) =
            iter <= y && (not $ visited vs (x,y)) && (y >= h || b?(x,y) /= 'X')

done h ((_,j),_) = j >= h

visited vs (i,j) = S.member (i*100000+j) vs
mark vs (i,j) = S.insert (i*100000+j) vs

bfs done next q vs =
  case viewl q of
    a@((i,j),_) :< qs ->
      if reallyDone then 0 else bfs done next nq $ mark vs (i,j)
      where nxt= if visited vs (i,j) then [] else next vs a
            reallyDone = done a || any done nxt
            nq = foldl (|>) qs nxt
    EmptyL  -> maxBound


{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.List

main = getLine >>= flip replicateM_ test . read

test = do
    (x1:y1:x2:y2:_) <- map read . words <$> getLine
    print $ (x2 - x1) * (y2 - y1) + 1

import           Control.Arrow
import           Control.Monad.State
import           Data.Array
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Lazy.Char8 as C
import           Data.Char
import           Data.Function
import           Data.List
import           Data.Map                   (Map, (!))
import qualified Data.Map                   as M
import           Data.Maybe
import           Data.Ord
import           Data.Ratio
import           Data.Set                   (Set)
import qualified Data.Set                   as S
import           Text.Printf

-- import Text.Parsec
-- import Text.Parsec.Expr
-- import Text.Parsec.Language (emptyDef)
-- import Text.Parsec.String
-- import qualified Text.Parsec.Token as T

infixl 0 >$>
(>$>) = flip ($)

main = C.interact $
  C.lines >>> drop 1 >>> map (C.words >>> map readInt >>> solve >>> show >>> C.pack) >>> C.unlines

readInt = C.readInt >>> fromJust >>> fst

solve :: [Int] -> Integer
solve [x1,y1,x2,y2] = n*m + 1
  where
    n = fromIntegral (x2-x1)
    m = fromIntegral (y2-y1)

solve :: [[Integer]] -> [Integer]
solve [] = []
solve (x:xs) = (count x) : (solve xs)

count :: [Integer] -> Integer
count [x1,y1,x2,y2]
	| x1 > x2 		= 		0
	| y1 > y2 		= 		0
	| x1 == x2		= 		1
	| y1 == y2 		= 		1
	| otherwise		= 		a * b - (a + b - 1) + 1
	where
		a = x2 - x1 + 1
		b = y2 - y1 + 1

main = do
	t' <- getLine
	let t = read t' :: Int
	fin <- getContents
	let input = map (map read) (map words (take t (lines fin))) :: [[Integer]]
	let result = solve input
	mapM print result

import Data.List

_split acc "" [] = reverse acc
_split acc curr [] = reverse $ curr : acc
_split acc curr (ch:rest)
  | (any (== ch) [' ', '\n']) = _split (reverse curr : acc) "" rest
  | otherwise = _split acc (ch:curr) rest

split = _split [] ""

main = do
  input <- getContents
  let (_:tokens) = split input
      vals = read <$> tokens :: [Integer]
      res = unfoldr (\ls ->
        case ls of
          []  -> Nothing
          x1:y1:x2:y2:rest  ->
            let area = (x2-x1) * (y2-y1)
            in Just (1 + area, rest)
        ) vals
  sequence_ $ putStrLn . show <$> res

import Data.List (sort, foldl')
import Data.Traversable (for)
import Control.Applicative (liftA2)

solveCase :: ((Integer, Integer), (Integer, Integer)) -> Integer
solveCase ((p,q), (x,y)) = 1 + (x-p)*(y-q)

main :: IO ()
main = do
  t <- read <$> getLine :: IO Int
  _ <- for [1..t] $ \_ -> do
    nums <- (map read . words) <$> getLine :: IO [Integer]
    case  nums  of
      [p,q,x,y] -> print (solveCase ((p,q),(x,y)))
      _ -> putStrLn "bad input format"
  return ()

type Point = (Integer, Integer)

solve :: Integer -> Integer -> Integer -> Integer -> Integer
solve x1 y1 x2 y2 = 1 + ((x2-x1)*(y2-y1))

main :: IO ()
main = interact (unlines . map parse . tail . lines)

parse :: String -> String
parse input = show $ solve x1 y1 x2 y2
  where [x1, y1, x2, y2] = map (\x -> read x :: Integer) $ words input

import           Control.Arrow
import           Control.Monad.State
import           Data.Array
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Lazy.Char8 as C
import           Data.Char
import           Data.Function
import           Data.List
import           Data.Map                   (Map, (!))
import qualified Data.Map                   as M
import           Data.Maybe
import           Data.Ord
import           Data.Ratio
import           Data.Set                   (Set)
import qualified Data.Set                   as S
import           Text.Printf

-- import Text.Parsec
-- import Text.Parsec.Expr
-- import Text.Parsec.Language (emptyDef)
-- import Text.Parsec.String
-- import qualified Text.Parsec.Token as T

infixl 0 >$>
(>$>) = flip ($)

main = C.interact $
  C.lines >>> drop 1 >>> map (C.words >>> map readInt >>> solve >>> show >>> C.pack) >>> C.unlines

readInt = C.readInt >>> fromJust >>> fst

solve [x1,y1,x2,y2] = n*m + 1
  where
    n = (x2-x1)
    m = (y2-y1)

solve :: [[Int]] -> [Int]
solve [] = []
solve (x:xs) = (count x) : (solve xs)

count :: [Int] -> Int
count [x1,y1,x2,y2]
	| x1 > x2 		= 		0
	| y1 > y2 		= 		0
	| x1 == x2		= 		1
	| y1 == y2 		= 		1
	| otherwise		= 		max (x2 - x1) (y2 - y1) + 1

main = do
	t' <- getLine
	let t = read t' :: Int
	fin <- getContents
	let input = map (map read) (map words (take t (lines fin))) :: [[Int]]
	let result = solve input
	mapM print result

solve :: [[Int]] -> [Int]
solve [] = []
solve (x:xs) = (count x) : (solve xs)

count :: [Int] -> Int
count [x1,y1,x2,y2]
	| x1 > x2 		= 		0
	| y1 > y2 		= 		0
	| x1 == x2		= 		1
	| y1 == y2 		= 		1
	| otherwise		= 		a * b - (a + b - 1) + 1
	where
		a = x2 - x1 + 1
		b = y2 - y1 + 1

main = do
	t' <- getLine
	let t = read t' :: Int
	fin <- getContents
	let input = map (map read) (map words (take t (lines fin))) :: [[Int]]
	let result = solve input
	mapM print result


{-# LANGUAGE OverloadedStrings #-}

import Data.Array.IArray
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Monoid
import Control.Monad
import Debug.Trace
import Text.Printf

kmpJumpBack :: B.ByteString -> Array Int Int
kmpJumpBack str = arr
    where len    = B.length str
          arr    = listArray (0, len) $ [f i $ f'(i-1) | i <- [0..len]]
          f'     = (arr !)
          str' n = B.head $ B.drop (n-1) str
          f i k
            | i <= 1               = 0
            | str'(k+1) == str'(i) = k+1
            | k == 0               = 0
            | otherwise            = f i $ f' k

solve :: Int -> Int -> B.ByteString -> B.ByteString -> Maybe String
solve n alph str acc = f' 0 0
    where len = B.length str
          arr :: Array (Int, Int) (Maybe String)
          arr = listArray ((0,0), (n,len-1)) $
                   [f pos st | pos <- [0..n], st <- [0..len-1]]
          f'  = curry (arr !)

          acc' :: Int -> Int
          acc' pos = if pos < len then 0
                     else ord (B.head $ B.drop (pos - len) acc) - ord '0'

          jumpBack = (kmpJumpBack str !)
          str' n = B.head $ B.drop (n-1) str
          ch n = chr $ ord 'a' + n

          jump st i
             | str'(st+1) == ch i
                = if st + 1 == len
                     then (1, jumpBack(st+1))
                     else (0, st + 1)
             | st == 0    = (0,0)
             | otherwise  = jump (jumpBack st) i

          f pos st
           | pos == n   = Just []
           | otherwise  = getFirst $ mconcat [First $ tryJump i | i <- [0..alph-1]]
              where
                tryJump :: Int -> Maybe String
                tryJump i = do
                    let (a, st') = jump st i

                    guard $ a == acc'(pos+1)

                    ret <- f' (pos+1) st'
                    return $ chr (i + ord 'a'):ret

main = do
   [n, alph] <- (map read . words) `fmap` getLine
   [str]     <- B.words `fmap` B.getLine
   [acc]     <- B.words `fmap` B.getLine

   case solve n alph str acc of
      Just ans -> putStrLn ans
      Nothing  -> putStrLn "No solution"

{-# LANGUAGE OverloadedStrings #-}

import Data.Array.IArray
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Monoid
import Control.Monad
import Debug.Trace
import Text.Printf

kmpJumpBack :: B.ByteString -> Array Int Int
kmpJumpBack str = arr
    where len    = B.length str
          arr    = listArray (0, len) $ [f i $ f'(i-1) | i <- [0..len]]
          f'     = (arr !)
          str' n = B.head $ B.drop (n-1) str
          f i k
            | i <= 1               = 0
            | str'(k+1) == str'(i) = k+1
            | k == 0               = 0
            | otherwise            = f i $ f' k

jumpTable :: B.ByteString -> Int -> Array (Int, Int) (Int, Int)
jumpTable str alph = arr
    where len    = B.length str
          arr    = listArray ((0,0), (len-1, alph-1)) $
                     [f st i | st <- [0..len-1], i <- [0..alph-1]]
          f'     = curry (arr !)
          str' n = B.head $ B.drop (n-1) str
          ch x   = chr $ ord 'a' + x
          f st i
             | str'(st+1) == ch i
                = if st + 1 == len
                     then (1, jumpBack(st+1))
                     else (0, st + 1)
             | st == 0    = (0,0)
             | otherwise  = f (jumpBack st) i

          jumpBack = (kmpJumpBack str !)

solve :: Int -> Int -> B.ByteString -> B.ByteString -> Maybe String
solve n alph str acc = f' 0 0
    where len = B.length str
          arr :: Array (Int, Int) (Maybe String)
          arr = listArray ((0,0), (n,len-1)) $
                   [f pos st | pos <- [0..n], st <- [0..len-1]]
          f'  = curry (arr !)

          acc' :: Int -> Int
          acc' pos = if pos < len then 0
                     else ord (B.head $ B.drop (pos - len) acc) - ord '0'

          jumpTable' = jumpTable str alph
          jump = curry (jumpTable'!)

          f pos st
           | pos == n   = Just []
           | otherwise  = getFirst $ mconcat [First $ tryJump i | i <- [0..alph-1]]
              where
                tryJump :: Int -> Maybe String
                tryJump i = do
                    let (a, st') = jump st i

                    guard $ a == acc'(pos+1)

                    ret <- f' (pos+1) st'
                    return $ chr (i + ord 'a'):ret

main = do
   [n, alph] <- (map read . words) `fmap` getLine
   [str]     <- B.words `fmap` B.getLine
   [acc]     <- B.words `fmap` B.getLine

   case solve n alph str acc of
      Just ans -> putStrLn ans
      Nothing  -> putStrLn "No solution"

{-# LANGUAGE OverloadedStrings #-}

import Data.Array.IArray
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Monoid
import Control.Monad
import Debug.Trace
import Text.Printf

kmpJumpBack :: B.ByteString -> Array Int Int
kmpJumpBack str = arr
    where len    = B.length str
          arr    = listArray (0, len) $ [f i $ f'(i-1) | i <- [0..len]]
          f'     = (arr !)
          str' n = B.head $ B.drop (n-1) str
          f i k
            | i <= 1               = 0
            | str'(k+1) == str'(i) = k+1
            | k == 0               = 0
            | otherwise            = f i $ f' k

jumpTable :: B.ByteString -> Int -> Array (Int, Int) (Int, Int)
jumpTable str alph = arr
    where len    = B.length str
          arr    = listArray ((0,0), (len-1, alph-1)) $
                     [f st i | st <- [0..len-1], i <- [0..alph-1]]
          f'     = curry (arr !)
          str' n = B.head $ B.drop (n-1) str
          ch x   = chr $ ord 'a' + x
          f st i
             | str'(st+1) == ch i
                = if st + 1 == len
                     then (1, jumpBack(st+1))
                     else (0, st + 1)
             | st == 0    = (0,0)
             | otherwise  = f (jumpBack st) i

          jumpBack = (kmpJumpBack str !)

solve :: Int -> Int -> B.ByteString -> B.ByteString -> Maybe String
solve n alph str acc = f' 0 0
    where len = B.length str
          arr :: Array (Int, Int) (Maybe String)
          arr = listArray ((0,0), (n,len-1)) $
                   [f pos st | pos <- [0..n], st <- [0..len-1]]
          f'  = curry (arr !)

          acc' :: Int -> Int
          acc' pos = if pos < len then 0
                     else ord (B.head $ B.drop (pos - len) acc) - ord '0'

          jumpTable' = jumpTable str alph
          jump = curry (jumpTable'!)

          f pos st
           | pos == n   = Just []
           | otherwise  = getFirst $ mconcat [First $ tryJump i | i <- [0..alph-1]]
              where
                tryJump :: Int -> Maybe String
                tryJump i = do
                    let (a, st') = jump st i

                    guard $ a == acc'(pos+1)

                    ret <- f' (pos+1) st'
                    return $ chr (i + ord 'a'):ret

main = do
   [n, alph] <- (map read . words) `fmap` getLine
   str       <- B.getLine
   acc       <- B.getLine

   printf "%d %d %d %d\n" n alph (length $ B.unpack str) (length $ B.unpack acc)

   case solve n alph str acc of
      Just ans -> putStrLn ans
      Nothing  -> putStrLn "No solution"

{-# LANGUAGE OverloadedStrings #-}

import Data.Array.IArray
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Monoid
import Control.Monad
import Debug.Trace

kmpJumpBack :: B.ByteString -> Array Int Int
kmpJumpBack str = arr
    where len    = B.length str
          arr    = listArray (0, len) $ [f i $ f'(i-1) | i <- [0..len]]
          f'     = (arr !)
          str' n = B.head $ B.drop (n-1) str
          f i k
            | i <= 1               = 0
            | str'(k+1) == str'(i) = k+1
            | k == 0               = 0
            | otherwise            = f i $ f' k

jumpTable :: B.ByteString -> Int -> Array (Int, Int) (Int, Int)
jumpTable str alph = arr
    where len    = B.length str
          arr    = listArray ((0,0), (len-1, alph-1)) $
                     [f st i | st <- [0..len-1], i <- [0..alph-1]]
          f'     = curry (arr !)
          str' n = B.head $ B.drop (n-1) str
          ch x   = chr $ ord 'a' + x
          f st i
             | str'(st+1) == ch i
                = if st + 1 == len
                     then (1, jumpBack(st+1))
                     else (0, st + 1)
             | st == 0    = (0,0)
             | otherwise  = f (jumpBack st) i

          jumpBack = (kmpJumpBack str !)

solve :: Int -> Int -> B.ByteString -> B.ByteString -> Maybe String
solve n alph str acc = f' 0 0
    where len = B.length str
          arr :: Array (Int, Int) (Maybe String)
          arr = listArray ((0,0), (n,len-1)) $
                   [f pos st | pos <- [0..n], st <- [0..len-1]]
          f'  = curry (arr !)

          acc' :: Int -> Int
          acc' pos = if pos < len then 0
                     else ord (B.head $ B.drop (pos - len) acc) - ord '0'

          jumpTable' = jumpTable str alph
          jump = curry (jumpTable'!)

          f pos st
           | pos == n   = Just []
           | otherwise  = getFirst $ mconcat [First $ tryJump i | i <- [0..alph-1]]
              where
                tryJump :: Int -> Maybe String
                tryJump i = do
                    let (a, st') = jump st i

                    guard $ a == acc'(pos+1)

                    ret <- f' (pos+1) st'
                    return $ chr (i + ord 'a'):ret

main = do
   [n, alph] <- (map read . words) `fmap` getLine
   str       <- B.getLine
   acc       <- B.getLine

   case solve n alph str acc of
      Just ans -> putStrLn ans
      Nothing  -> putStrLn "No solution"


solve :: (Integer, Integer, Integer) -> Integer
solve (h, c, t)
  | 2 * t <= h + c     = 2
  | otherwise  = let
    n = 2 * t - h - c
    d = h - c
    q = quot d (2 * n)
    v1 = 2*q + 1
    v2 = v1 + 2
    in  if  (v1 + v2) * d <= v1 * v2 * 2 * n
         -- 1 / v1 - n / d <= n / d - 1 / v2
      then  v1  else  v2

parse :: String -> [(Integer, Integer, Integer)]
parse inp = [(h, c, t) | [h, c, t] <- map (map read . words) . tail $ lines inp]

main :: IO ()
main = interact (unlines . map (show . solve) . parse)

import Data.Int
import Data.Ratio

main :: IO ()
main = interact (unlines . map (show . solve . parse). tail . lines)

parse :: String -> (Int64, Int64, Int64)
parse s = (h, c, t)
  where
    [h, c, t] = map read . words $ s

solve :: (Int64, Int64, Int64) -> Int64
solve (h, c, t)
  | 2 * t <= h + c = 2
  | f n <= f (n + 1) = 2 * n + 1
  | otherwise = 2 * (n + 1) + 1
  where
    n = div (h - t) (2 * t - (h + c))
    f k = abs (fromIntegral t - ((k + 1) * h + k * c) % (2 * k + 1))

{-# LANGUAGE MultiWayIf #-}

import Data.List
import Data.Ratio

_split acc "" [] = reverse acc
_split acc curr [] = reverse $ curr : acc
_split acc curr (ch:rest)
  | (any (== ch) [' ', '\n']) = _split (reverse curr : acc) "" rest
  | otherwise = _split acc (ch:curr) rest

split = _split [] ""

oddRoundDown n
  | (n `mod` 2 == 0) = if n >= 2 then n - 1 else n + 1
  | otherwise = n

oddRoundUp n
  | (n `mod` 2 == 0) = n + 1
  | otherwise = n

mixTemp :: Rational -> Rational -> Integer -> Rational
mixTemp c h numCups =
  let numH = ((numCups + 1) `div` 2) % 1
      numC = (numCups `div` 2) % 1
  in (numC * c + numH * h) / (numC + numH)

main = do
  input <- getContents
  let (_:vals) = read <$> split input :: [Integer]
      res = unfoldr (\ls ->
        case ls of
          []  -> Nothing
          hInt:cInt:tgtInt:rest  -> Just (res, rest) where
            res = let
              h = hInt % 1
              c = cInt % 1
              tgt = tgtInt % 1
              avg = (c+h) / 2
              in if
              | tgt <= avg  -> 2
              | otherwise   ->
                  let diff = tgt - avg
                      exact = (h - avg) / diff
                      num = numerator exact
                      denom = denominator exact
                      near1 = oddRoundDown $ num `div` denom
                      near2 = oddRoundUp $ (num + denom - 1) `div` denom
                      temp1 = mixTemp c h near1
                      temp2 = mixTemp c h near2
                  in  if abs (temp1 - tgt) <= abs (temp2 - tgt) then near1 else near2
        ) vals
  sequence_ (putStrLn . show <$> res)

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.List
import Data.Ord
import Data.Ratio

main = getLine >>= flip replicateM_ test . read

test = do
    (h:c:t:_) <- map read . words <$> getLine
    if t == h
        then print 1
        else if t <= (c + h) `div` 2
                 then print 2
                 else do
                     let x = (t - h) `div` (c + h - 2 * t)
                     print $
                         ((+ 1) . (* 2)) $
                         minimumBy
                             (comparing (\x -> abs $ fromInteger t - (x * c + (x + 1) * h) % (2 * x + 1)))
                             [max (x - 5) 0 .. x + 5]

import Data.Ratio

main :: IO ()
main = interact (unlines . map (show . solve . parse). tail . lines)

parse :: String -> (Int, Int, Int)
parse s = (h, c, t)
  where
    [h, c, t] = map read . words $ s

solve :: (Int, Int, Int) -> Int
solve (h, c, t)
  | 2 * t <= h + c = 2
  | f n <= f (n + 1) = 2 * n + 1
  | otherwise = 2 * (n + 1) + 1
  where
    n = div (h - t) (2 * t - (h + c))
    f k = abs (fromIntegral t - ((k + 1) * h + k * c) % (2 * k + 1))

import Data.Ratio

main :: IO ()
main = interact (unlines . map (show . solve . parse). tail . lines)

parse :: String -> (Int, Int, Int)
parse s = (h, c, t)
  where
    [h, c, t] = map read . words $ s

solve :: (Int, Int, Int) -> Int
solve (h, c, t)
  | 2 * t <= h + c = 2
  | f n < f (n + 1) = 2 * n + 1
  | otherwise = 2 * (n + 1) + 1
  where
    n = div (h - t) (2 * t - (h + c))
    f k = abs (fromIntegral t - ((k + 1) * h + k * c) % (2 * k + 1))


import Control.Monad (replicateM_)

main :: IO ()
main = do
	n <- getLine
	replicateM_ (read n) $ do
		zm <- fmap (read . head . tail . words) getLine
		print (zm*2)

solve :: Int -> IO ()
solve 0 = return ()
solve t = do
    line <- getLine
    let [_, b] = map (\x -> read x :: Int) $ words line
    print $ 2 * b
    solve $ t - 1

main :: IO ()
main = do
    line <- getLine
    let t = read line :: Int
    solve t

main = interact $ unlines . map (show . solve) . pairs . map read . tail . words
pairs (a:b:xs) = (a,b) : pairs xs
pairs [] = []
solve (_,x) = 2*x

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import Data.Array
import qualified Data.Map as M

onecase = do
		[a,x]<-map read <$> words <$> getLine::IO [Int]
		print $ x+ x

main = do
		n<-read <$> getLine ::IO Int
		replicateM_ n onecase

process :: Int -> Int -> Int
process n x = 2*x

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    t <- fmap readInt getLine
    ts <- fmap (map (map readInt.words).lines) getContents
    mapM_ print $ map (\[n,x] -> process n x) ts

import Data.Char (ord)
import Control.Monad (replicateM_)

parseInt :: String -> Int
parseInt = foldl (\acc c -> acc * 10 + ord c - 48) 0

solve :: [Int] -> Int
solve [_, x] = x * 2

main :: IO ()
main = do
  testNum <- fmap parseInt getLine
  replicateM_ testNum (print . solve . map parseInt . words =<< getLine)

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.ST

import Data.Char
import Data.List
import Data.Maybe

import qualified Data.ByteString.Char8 as B

import Data.Array.ST.Safe
import Data.STRef

-- import Debug.Trace
import Text.Printf

readInt = readLn :: IO Int
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readNInts = readInt >>= flip replicateM readInt

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

main = do
	t <- readInt
	replicateM t $ do
		[ x, n ] <- readInts
		print $ n * 2

main = interact $ unlines . map (show . solve) . pairs . map read . tail . words
pairs (a:b:xs) = (a,b) : pairs xs
pairs [] = []
solve (_,x) = s !! (x-1)
s = go 0 [1..] where
  go i xs | null r = xs
          | otherwise = l ++ go (i+1) (tail r)
    where (l,r) = splitAt i xs


import Control.Monad
import Control.Monad.Trans.State
import Control.Monad.Trans.Class

import qualified Data.ByteString.Lazy.Char8 as P

import Data.List
import Data.Int

type SIO = StateT P.ByteString IO

readLine :: SIO P.ByteString
readLine = do
  (out, next) <- P.break (<= '\r') <$> get
  put . P.tail . snd $ P.break (== '\n') next
  pure out

readInts :: SIO [Int]
readInts = do
  currLine <- readLine
  pure [x | Just (x, _) <- P.readInt <$> P.words currLine]

readInt64s :: SIO [Int64]
readInt64s = do
  currLine <- readLine
  pure [fromIntegral x | Just (x, _) <- P.readInteger <$> P.words currLine]

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [t] <- readInts
  replicateM_ t $ do
    [_n, k] <- readInt64s
    x1 : x <- readInt64s
    let
      w = foldl' gcd 0 $ map (x1 -) x
    lift . P.putStrLn . P.pack $ if  mod (k-x1) w == 0
      then  "YES"
      else  "NO"

module Main where

import           Control.Monad                  ( foldM
                                                , replicateM
                                                , replicateM_
                                                )
import           Data.Array                     ( listArray )
import           Data.Foldable                  ( maximumBy )
import           Data.Int                       ( Int64 )
import qualified Data.Map                      as Map

solve :: Int64 -> [Int64] -> Bool
solve _ []       = False
solve k [x     ] = k == x
solve k (x : xs) = k == x || not (null xs') && (k - x) `mod` foldl1 gcd xs' == 0 where xs' = filter (/= 0) (subtract x <$> xs)

main :: IO ()
main = readLn >>= flip replicateM_ go
  where
    go = do
        k <- (!! 1) . fmap read . words <$> getLine
        r <- solve k . fmap read . words <$> getLine
        if r then putStrLn "YES" else putStrLn "NO"

import Control.Monad (replicateM_)
import Data.Functor ((<&>))

main :: IO ()
main = do
    let getList = getLine <&> words <&> map read
    [tests_n] <- getList
    replicateM_ tests_n $ do
        [n, k] <- getList
        xs <- getList
        let output = if canGetK k xs then "YES" else "NO"
        putStrLn output

canGetK :: Integral int => int -> [int] -> Bool
canGetK k [] = False
canGetK k (x:xs) = step `divides` (k - x)
    where
        relative_xs = map (subtract x) xs
        step = foldr gcd 0 relative_xs

divides :: (Integral int) => int -> int -> Bool
divides 0 x = x == 0
divides d x = mod x d == 0

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.Bool
import Data.List

main :: IO ()
main = getLine >>= flip replicateM_ test . read

test :: IO ()
test = do
  [_, k] <- map read . words <$> getLine :: IO [Integer]
  xs <- map read . words <$> getLine :: IO [Integer]
  let d = foldl gcd 0 (zipWith (-) <*> tail $ sort xs)
  putStrLn $ bool "NO" "YES" $ k `mod` d == head xs `mod` d

module Main where

import           Control.Monad                  ( foldM
                                                , replicateM
                                                , replicateM_
                                                )
import           Data.Array                     ( listArray )
import           Data.Foldable                  ( maximumBy )
import qualified Data.Map                      as Map

solve :: Int -> [Int] -> Bool
solve _ []       = False
solve k [x     ] = k == x
solve k (x : xs) = k == x || not (null xs') && (k - x) `mod` foldl1 gcd xs' == 0 where xs' = filter (/= 0) (subtract x <$> xs)

main :: IO ()
main = readLn >>= flip replicateM_ go
  where
    go = do
        k <- (!! 1) . fmap read . words <$> getLine
        r <- solve k . fmap read . words <$> getLine
        if r then putStrLn "YES" else putStrLn "NO"

module Main where

import           Control.Monad                  ( foldM
                                                , replicateM
                                                , replicateM_
                                                )
import           Data.Array                     ( listArray )
import           Data.Foldable                  ( maximumBy )
import           Data.List
import qualified Data.Map                      as Map

-- >>> solve 1 $ sort [1,2]
-- True

solve :: Int -> [Int] -> Bool
solve _ []       = False
solve k [x     ] = k == x
solve k (x : xs) = k == x || not (null xs') && k - x `mod` foldl1 gcd xs' == 0 where xs' = filter (/= 0) (subtract x <$> xs)

main :: IO ()
main = readLn >>= flip replicateM_ go
  where
    go = do
        k <- (!! 1) . fmap read . words <$> getLine
        r <- solve k . fmap read . words <$> getLine
        if r then putStrLn "YES" else putStrLn "NO"


import Data.List
import Control.Monad
import Control.Monad.ST
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.Array.ST as STArray
import qualified Data.Array.Unboxed as UArray
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.Map.Strict as Map
-- import Data.Map.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' == ord ' ' || c' == ord '\r' -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [(Id, Int)]
data ChainNode = ChainNode {
    nodeValue :: Int
  , nodeIndex :: Id
  , nodeNeighA :: Maybe ChainNode
  , nodeNeighB :: Maybe ChainNode
  }

chainItems (Chain _ xs) = xs

groupId :: Group -> Id
groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode i x) | (i, x) <- zip [0..] xs]
  buildNode i x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode {
        nodeValue = x
      , nodeIndex = i
      , nodeNeighA = neighbour a
      , nodeNeighB = neighbour b
      }
  traceItems :: Group -> ChainNode -> [(Id, Int)]
  traceItems g chain@(ChainNode { nodeValue = x, nodeIndex = i, nodeNeighA = na, nodeNeighB = nb })
    | g == G1 = if isNothing na || (nodeValue na' == x) then [item] else item : traceItems G2 na'
    | g == G2 = if isNothing nb || (nodeValue nb' == x) then [item] else item : traceItems G1 nb'
    where
      Just na' = na
      Just nb' = nb
      item = (i, x)
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node -> let
    na = nodeNeighA node
    nb = nodeNeighB node
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceItems G1 node]
      else Just [Chain G2 $ traceItems G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g items) =
  if length items `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    (i0, x0) = head items
    (i1, x1) = last items

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

getGroupMap :: Int -> [(Id, Group)] -> [Maybe Group]
getGroupMap n xs = map t . UArray.elems $ gs where
  t (-1) = Nothing
  t 0 = Just G1
  t 1 = Just G2

  gs :: UArray.UArray Int Id
  gs = STArray.runSTUArray $ do
    gs <- STArray.newArray (0, n - 1) (-1)
    forM_ xs $ \(i, g) -> STArray.writeArray gs i (groupId g)
    return gs

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = getGroupMap (length xs) . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(i, g) | (i, x) <- chainItems chain])
      sequence $ groupMap

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  case solve a b xs of
    No -> putStrLn "NO"
    Yes gs -> putStrLn "YES" >> putStrLn (unwords . map (show . groupId) $ gs)

import Data.List
import Control.Monad
import Control.Monad.ST
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.Array.ST as STArray
import qualified Data.Array as Array
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.Map.Strict as Map
-- import Data.Map.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' == ord ' ' || c' == ord '\r' -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [(Id, Int)]
data ChainNode = ChainNode {
    nodeValue :: Int
  , nodeIndex :: Id
  , nodeNeighA :: Maybe ChainNode
  , nodeNeighB :: Maybe ChainNode
  }

chainItems (Chain _ xs) = xs

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode i x) | (i, x) <- zip [0..] xs]
  buildNode i x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode {
        nodeValue = x
      , nodeIndex = i
      , nodeNeighA = neighbour a
      , nodeNeighB = neighbour b
      }
  traceItems :: Group -> ChainNode -> [(Id, Int)]
  traceItems g chain@(ChainNode { nodeValue = x, nodeIndex = i, nodeNeighA = na, nodeNeighB = nb })
    | g == G1 = if isNothing na || (nodeValue na' == x) then [item] else item : traceItems G2 na'
    | g == G2 = if isNothing nb || (nodeValue nb' == x) then [item] else item : traceItems G1 nb'
    where
      Just na' = na
      Just nb' = nb
      item = (i, x)
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node -> let
    na = nodeNeighA node
    nb = nodeNeighB node
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceItems G1 node]
      else Just [Chain G2 $ traceItems G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g items) =
  if length items `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    (i0, x0) = head items
    (i1, x1) = last items

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

getGroupMap :: Int -> [(Id, Group)] -> [Maybe Group]
getGroupMap n xs = Array.elems gs where
  gs :: Array.Array Int (Maybe Group)
  gs = STArray.runSTArray $ do
    gs <- STArray.newArray (0, n - 1) Nothing
    forM_ xs $ \(i, g) -> STArray.writeArray gs i (Just g)
    return gs

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = getGroupMap (length xs) . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(i, g) | (i, x) <- chainItems chain])
      sequence $ groupMap

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  case solve a b xs of
    No -> putStrLn "NO"
    Yes gs -> putStrLn "YES" >> putStrLn (unwords . map (show . groupId) $ gs)

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' == ord ' ' || c' == ord '\r' -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print $ solve a b xs

import Data.List
import Control.Monad
import Data.Maybe
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Map ((!))

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in  liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- return . map read . words =<< getLine
  print $ solve a b xs

{-# LANGUAGE MultiParamTypeClasses,FlexibleContexts,FlexibleInstances,TypeSynonymInstances,BangPatterns,RankNTypes,TupleSections #-}
import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Control.Arrow
import Debug.Trace
import Text.Printf

import Data.Int
import Data.Bits
import Data.Maybe
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B

readInt = fromJust . fmap fst . B.readInt
readInts = map readInt . B.words <$> B.getLine
readIntPair = l2p . map readInt . take 2 . B.words <$> B.getLine
readLns :: Read a => IO [a]
readLns = map read . words <$> getLine
cmpFst (a,_) (b,_) = compare a b
cmpSnd (_,a) (_,b) = compare a b
cmpLen a b = length a `compare` length b
swap (a,b) = (b,a)
l2p (a:b:_) = (a,b)
p2l (a,b) = [a,b]
itof :: Int -> Double
itof = fromIntegral
defaultArray :: (IArray a e,Ix i) => e -> (i,i) -> [(i,e)] -> a i e
defaultArray = accumArray $ curry snd
flatten :: [(a,[(b,c)])] -> [((a,b),c)]
flatten = (=<<) $ uncurry $ fmap . first . (,)
stepM_ :: Monad m => a -> (a -> Bool) -> (a -> a) -> (a -> m ()) -> m ()
stepM_ i judge incr step = sub i where
    sub i | judge i = step i >> sub (incr i) | otherwise = return ()
thru :: Monad m => (v -> m ()) -> v -> m v
thru m v = m v >> return v
inf = maxBound `div` 2 :: Int

data Tree a = Null | Fork a (Tree a) (Tree a) deriving(Show)

empty :: Tree a
empty = Null

isEmpty :: Tree a -> Bool
isEmpty Null = True
isEmpty _ = False

minElem :: Tree a -> a
minElem (Fork a _ _) = a
minElem Null = error "min for empty queue is not supported"

deleteMin :: (Ord a) => Tree a -> Tree a
deleteMin (Fork _ a b) = merge a b

insert :: (Ord a) => a -> Tree a -> Tree a
insert x a = merge (Fork x Null Null) a

merge :: (Ord a) => Tree a -> Tree a -> Tree a
merge a Null = a
merge Null b = b
merge a b | minElem a <= minElem b = join a b
          | otherwise = join b a
    where
    join (Fork x a b) c = Fork x b (merge a c)

solve :: Int -> Int -> [Int] -> Maybe (S.Set Int,S.Set Int)
solve a b ps
    | a == b = guard (all (f.(a-)) ps) *> pure (S.fromList ps,S.empty)
    | otherwise = greedy queue S.empty S.empty s where
    s = S.fromList ps
    f p = p `S.member` s
    score p = length $ filter id $ [f (a - p),f (b - p)]
    queue :: Tree (Int,Int)
    queue = foldl (flip insert) Main.empty [ (k,p) | p <- ps, let k = score p ]
    greedy queue !as !bs !s
        | isEmpty queue = return (as, bs)
        | used = greedy (deleteMin queue) as bs s
        | k == 0 = Nothing
        | not $ S.member b' s = greedy (g (b - a') queue) as' bs  s'
        | not $ S.member a' s = greedy (g (a - b') queue) as  bs' s' where
           a' = a - p
           b' = b - p
           s' = S.delete p $ S.delete c $ s
           c  = if not $ S.member b' s then a' else b'
           as' = S.insert p $ S.insert a' as
           bs' = S.insert p $ S.insert b' bs
           (k,p) = minElem queue
           used = S.member p as || S.member p bs || k == 2
           g x queue | f x = insert (score x-1,x)$deleteMin queue
                     | otherwise = deleteMin queue

main = do
    [_,a,b] <- readInts
    ps <- readInts
    case solve a b ps of
        Nothing -> putStrLn "NO"
        Just (as,bs) -> do
            putStrLn "YES"
            let f p = if S.member p as then "0" else "1"
            putStrLn $ unwords $ map f ps

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' `elem` map ord " \t" -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print xs
  print $ solve a b xs

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' `elem` map ord " \n" -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print xs
  print $ solve a b xs

import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Map ((!))
import Debug.Trace

traceShow' x = traceShow x x

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Link = Link Group [Id] deriving Show
type GroupMap = Map.Map Id Group

instance Show Answer where
  show No = "No"
  show (Yes gs) = "YES\n" ++ unwords (map show' gs) where
    show' G1 = "0"
    show' G2 = "1"

otherGroup G1 = G2
otherGroup G2 = G1

putGroup :: Group -> GroupMap -> [Id] -> GroupMap
putGroup g = foldr (flip Map.insert g)

groupMapToAnswer :: [Int] -> Maybe GroupMap -> Answer
groupMapToAnswer _ Nothing = No
groupMapToAnswer xs (Just groupMap) =
  if Map.size groupMap == length xs then Yes $ Map.elems groupMap
  else No

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = groupMapToAnswer xs $
      -- flip applyLinks initGroupMap $ (traceShow' $ findLinks Set.empty xs)
      flip applyLinks initGroupMap $ findLinks Set.empty xs
    where
      n = length xs
      initGroupMap = Map.empty
      values = Map.fromList . zip [0..] $ xs
      idMap = Map.fromList $ zip xs [0..]

      findLinks :: Set.Set Int -> [Int] -> [Link]
      findLinks used [] = []
      findLinks used (x:xs) = let
        y = a - x
        z = b - x
        inIdMap = (`Map.member` idMap)
        notUsed = not . (`Set.member` used)
        f1 = inIdMap y && notUsed y
        f2 = inIdMap z && notUsed z
        mLink = if (not f1 && not f2) then []
          else if (f1 || f2) && (not (f1 && f2)) then
            if f1 then [Link G1 $ traceIds a x]
            else [Link G2 $ traceIds b x]
          else []
        used' = case mLink of
          [] -> used
          [(Link _ ids)] -> Set.union used . Set.fromList . map (values!) $ ids
        in mLink ++ findLinks used' xs

      traceIds :: Int -> Int -> [Int]
      traceIds t x =
        if not (x `Map.member` idMap) then []
        else (idMap!x) : if x + x == t then [] else traceIds (a + b - t) (t - x)
        -- else (idMap!x) : traceIds (a + b - t) (t - x)

      applyLinks :: [Link] -> GroupMap -> Maybe GroupMap
      applyLinks [] g = Just g
      applyLinks (x:xs) g = applyLink x g >>= applyLinks xs

      applyLink :: Link -> GroupMap -> Maybe GroupMap
      applyLink link@(Link g ids) groupMap = let
        x0 = values ! head ids
        x1 = values ! last ids
        in if length ids `mod` 2 == 0 then
          -- put all ids to group g
          Just $ putGroup g groupMap ids
        else if g == G1 && x0 * 2 == b || g == G2 && x0 * 2 == a then
          -- put all ids to group other than g
          Just $ putGroup (otherGroup g) groupMap ids
        else if g == G1 && x1 * 2 == a || g == G2 && x1 * 2 == b then
          -- put all ids to group g
          Just $ putGroup g groupMap ids
        else
          Nothing

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- return . map read . words =<< getLine
  print $ solve a b xs

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' `elem` map ord " \n\r\t" -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print xs
  print $ solve a b xs

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> case x of
      Nothing -> []
      Just x' -> [x']
    Just (c, cs)
      | c' == ord ' ' -> (case x of Nothing -> id; Just x' -> (x':)) $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print $ solve a b xs

import Data.List
import Data.Char
import Control.Monad
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' 0 where
  readInts' x s = case B.uncons s of
    Nothing -> []
    Just (c, cs)
      | c' == ord ' ' -> x : readInts' x cs
      | otherwise -> readInts' (x * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print $ solve a b xs

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' `elem` map ord " \r" -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print xs
  print $ solve a b xs

import Data.List
import Control.Monad
import Control.Monad.ST
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.Array.ST as STArray
import qualified Data.Array as Array
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.Map.Strict as Map
-- import Data.Map.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> addX []
    Just (c, cs)
      | c' == ord ' ' || c' == ord '\r' -> addX $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where c' = fromIntegral c
    where addX = (case x of Nothing -> id; Just x' -> (x':))

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [(Id, Int)]
data ChainNode = ChainNode {
    nodeValue :: Int
  , nodeIndex :: Id
  , nodeNeighA :: Maybe ChainNode
  , nodeNeighB :: Maybe ChainNode
  }

chainItems (Chain _ xs) = xs

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode i x) | (i, x) <- zip [0..] xs]
  buildNode i x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode {
        nodeValue = x
      , nodeIndex = i
      , nodeNeighA = neighbour a
      , nodeNeighB = neighbour b
      }
  traceItems :: Group -> ChainNode -> [(Id, Int)]
  traceItems g chain@(ChainNode { nodeValue = x, nodeIndex = i, nodeNeighA = na, nodeNeighB = nb })
    | g == G1 = if isNothing na || (nodeValue na' == x) then [item] else item : traceItems G2 na'
    | g == G2 = if isNothing nb || (nodeValue nb' == x) then [item] else item : traceItems G1 nb'
    where
      Just na' = na
      Just nb' = nb
      item = (i, x)
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node -> let
    na = nodeNeighA node
    nb = nodeNeighB node
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceItems G1 node]
      else Just [Chain G2 $ traceItems G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g items) =
  if length items `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    (i0, x0) = head items
    (i1, x1) = last items

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

getGroupMap :: Int -> [(Id, Group)] -> [Maybe Group]
getGroupMap n xs = Array.elems gs where
  gs :: Array.Array Int (Maybe Group)
  gs = STArray.runSTArray $ do
    gs <- STArray.newArray (0, n - 1) Nothing
    forM_ xs $ \(i, g) -> STArray.writeArray gs i (Just g)
    return gs

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = getGroupMap (length xs) . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(i, g) | (i, x) <- chainItems chain])
      sequence $ groupMap

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  case solve a b xs of
    No -> putStrLn "No"
    Yes gs -> putStrLn "YES" >> putStrLn (unwords . map (show . groupId) $ gs)

import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Map ((!))
import Debug.Trace

traceShow' x = traceShow x x

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Link = Link Group [Id] deriving Show
type GroupMap = Map.Map Id Group

instance Show Answer where
  show No = "No"
  show (Yes gs) = "YES\n" ++ unwords (map show' gs) where
    show' G1 = "0"
    show' G2 = "1"

otherGroup G1 = G2
otherGroup G2 = G1

putGroup :: Group -> GroupMap -> [Id] -> GroupMap
putGroup g = foldr (flip Map.insert g)

groupMapToAnswer :: Maybe GroupMap -> Answer
groupMapToAnswer Nothing = No
groupMapToAnswer (Just groupMap) = Yes $ Map.elems groupMap

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = groupMapToAnswer $
      flip applyLinks initGroupMap . checkLinks =<< sequence (findLinks Set.empty xs)
    where
      n = length xs
      initGroupMap = Map.empty
      values = Map.fromList . zip [0..] $ xs
      idMap = Map.fromList $ zip xs [0..]

      checkLinks :: [Link] -> [Link]
      checkLinks links =
        if (sum . map (\(Link g ids) -> length ids) $ links) == n
        then links
        else error $ "check link failed" ++ show links

      findLinks :: Set.Set Int -> [Int] -> [Maybe Link]
      findLinks used [] = []
      findLinks used (x:xs) = let
        y = a - x
        z = b - x
        inIdMap = (`Map.member` idMap)
        notUsed = not . (`Set.member` used)
        f1 = inIdMap y && notUsed y
        f2 = inIdMap z && notUsed z
        mLink = if (not f1 && not f2) then [Nothing]
          else if (f1 || f2) && (not (f1 && f2)) then
            if f1 then [Just $ Link G1 $ traceIds a x]
            else [Just $ Link G2 $ traceIds b x]
          else []
        used' = case mLink of
          [] -> used
          [Nothing] -> used
          [Just (Link _ ids)] -> Set.union used . Set.fromList . map (values!) $ ids
        in mLink ++ findLinks used' xs

      traceIds :: Int -> Int -> [Int]
      traceIds t x =
        if not (x `Map.member` idMap) then []
        else (idMap!x) : if x + x == t then [] else traceIds (a + b - t) (t - x)
        -- else (idMap!x) : traceIds (a + b - t) (t - x)

      applyLinks :: [Link] -> GroupMap -> Maybe GroupMap
      applyLinks [] g = Just g
      applyLinks (x:xs) g = applyLink x g >>= applyLinks xs

      applyLink :: Link -> GroupMap -> Maybe GroupMap
      applyLink link@(Link g ids) groupMap = let
        x0 = values ! head ids
        x1 = values ! last ids
        in if length ids `mod` 2 == 0 then
          -- put all ids to group g
          Just $ putGroup g groupMap ids
        else if g == G1 && x0 * 2 == b || g == G2 && x0 * 2 == a then
          -- put all ids to group other than g
          Just $ putGroup (otherGroup g) groupMap ids
        else if g == G1 && x1 * 2 == a || g == G2 && x1 * 2 == b then
          -- put all ids to group g
          Just $ putGroup g groupMap ids
        else
          Nothing

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- return . map read . words =<< getLine
  print $ solve a b xs

import Data.List
import Control.Monad
import Data.Char
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' Nothing where
  readInts' :: Maybe Int -> B.ByteString -> [Int]
  readInts' x s = case B.uncons s of
    Nothing -> case x of
      Nothing -> []
      Just x' -> [x']
    Just (c, cs)
      | c' == ord ' ' -> (case x of Nothing -> id; Just x' -> (x':)) $ readInts' Nothing cs
      | otherwise -> readInts' (Just $ (maybe 0 id x) * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print xs
  print $ solve a b xs

import Data.List
import Data.Char
import Control.Monad
import Data.Maybe
import Debug.Trace
import qualified Data.ByteString as B
import qualified Data.Set as Set

-- import qualified Data.HashMap.Lazy as HashMap
-- import Data.HashMap.Lazy ((!))

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

-- Utils

readInts :: IO [Int]
readInts = B.getLine >>= return . readInts' 0 . (\s -> traceShow (B.length s) s) where
  readInts' x s = case B.uncons s of
    Nothing -> []
    Just (c, cs)
      | c' == ord ' ' -> x : readInts' x cs
      | otherwise -> readInts' (x * 10 + c' - ord '0') cs
      where
        c' = fromIntegral c

-- End of Utils

data Group = G1 | G2 deriving (Show, Eq)
data Answer = No | Yes [Group]
type Id = Int
data Chain = Chain Group [Id]
data ChainNode = ChainNode Int (Maybe ChainNode) (Maybe ChainNode)

instance Show Answer where
  show No = "NO"
  show (Yes gs) = "YES\n" ++ unwords (map (show . groupId) gs)

nodeValue (ChainNode x _ _) = x
chainValues (Chain _ values) = values

groupId G1 = 0
groupId G2 = 1

otherGroup G1 = G2
otherGroup G2 = G1

findChains :: Int -> Int -> [Int] -> Maybe [Chain]
findChains a b xs = let
  nodes = Map.fromList [(x, buildNode x) | x <- xs]
  buildNode x = let
    neighbour t = Map.lookup (t - x) nodes
    in ChainNode x (neighbour a) (neighbour b)
  traceValues :: Group -> ChainNode -> [Int]
  traceValues g (ChainNode x na nb)
      | g == G1 = if isNothing na || (nodeValue na' == x) then [x] else x : traceValues G2 na'
      | g == G2 = if isNothing nb || (nodeValue nb' == x) then [x] else x : traceValues G1 nb'
      where
        Just na' = na
        Just nb' = nb
  in liftM concat . sequence . flip map (Map.elems nodes) $ \node@(ChainNode x na nb) -> let
    Just (ChainNode y _ _) = na
    Just (ChainNode z _ _) = nb
    in if isNothing na && isNothing nb then Nothing
      else if isJust na && isJust nb then Just []
      else if isJust na then Just [Chain G1 $ traceValues G1 node]
      else Just [Chain G2 $ traceValues G2 node]

markChain :: Int -> Int -> Chain -> Maybe Group
markChain a b (Chain g values) =
  if length values `mod` 2 == 0 then Just g
  else if x0 + x0 == [b, a]!!groupId g then Just (otherGroup g)
  else if x1 + x1 == [a, b]!!groupId g then Just g
  else Nothing
  where
    x0 = head values
    x1 = last values

solveSame :: Int -> [Int] -> Answer
solveSame a xs = let
  xsSet = Set.fromList xs
  haveAns = all (`Set.member` xsSet) . map (a -) $ xs
  in if haveAns then Yes $ map (const G1) xs else No

solve :: Int -> Int -> [Int] -> Answer
solve a b xs
  | a == b = solveSame a xs
  | otherwise = maybe No Yes $ do
      chains <- findChains a b xs
      groups <- sequence . map (markChain a b) $ chains
      let groupMap = Map.fromList . flip concatMap (zip groups chains) $ (
            \(g, chain) -> [(x, g) | x <- chainValues chain])
      sequence $ map (`Map.lookup` groupMap) xs

main = do
  [n, a, b] <- return . map read . words =<< getLine
  xs <- readInts
  print $ solve a b xs


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

solve :: [Double] -> Double
solve a = let b = map fst $ filter (odd . snd)  $ zip a [0..]
          in  5.0 + sum b / fromIntegral (length b)

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

solve :: [Double] -> Double
solve a = let b = map fst $ filter (odd . snd)  $ zip a [0..]
          in  5.0 + sum b / (fromIntegral (length b))