import List

getWords :: IO [String]
getWords = getLine >>= return . words

reads :: Read a => IO [a]
reads = getWords >>= return . (map read)

solve :: (Int, Int) -> [Int] -> Int
solve (a, b) hs  =  hb' - hb
  where
    hs'  =  sort hs
    hb   =  hs' !! (b - 1)
    hb'  =  hs' !! b

main :: IO ()
main  =  do
    [n, a, b] <- Main.reads
    hs <- Main.reads
    print $ solve (a, b) hs

import Data.List

main::IO ()
main = do
  cs <- getContents
  (putStr .show.solve.parseIn) cs

parseIn::String->(Int,[Int])
parseIn s =  (a, reverse $ sort $ map read (words hs))
    where
      [ss,hs] = lines s
      [_,a,_] = map read (words ss)

solve::(Int,[Int])->Int
solve (a,hs) = he!!(a-1)-tt
    where
    (he,(tt:_)) = splitAt a hs

import Data.List
main=putStrLn.show.slv.(map (\x -> map read $ words x)).(take 2).lines=<<getContents
slv [al,bl] = s!!(a-1) - s!!(a)
    where
        n = head al :: Int
        a = head.tail $ al
        b = head.tail.tail $ al
        s = reverse.sort $ bl

import Data.List
solve chores a _ =
      let (v, p) = splitAt a (reverse $ sort chores)
      in (last v) - (head p)

main = do
  [n, a, b] <- (getLine >>= (return . map (read::String->Int) . words))
  chores <- (getLine >>= (return . map (read::String->Int) . words))
  print $ solve chores a b

import Data.List
import Control.Applicative

main = do
  [_, _, b] <- map read <$> (words <$> getLine)
  h         <- sort <$> map read <$> (words <$> getLine)
  print $ h !! b - h !! (b - 1)

import Data.List

main = do
    [n,a,b] <- return . map (read :: String->Int) . words =<< getLine
    hs <- return . sort . map (read :: String->Int) . words =<< getLine
    let p = take b hs
        q = drop b hs
        r = if head q /= last p
            then head q - last p
            else 0
    print r

import Data.List

solve :: (Int, Int) -> [Int] -> Int
solve (a, b) sp  = x2 - x1
	where
	   sp' = sort sp
	   x2 = sp' !! b
	   x1 = sp' !! (b - 1)

main :: IO ()
main  =  do
    [n, a, b] <- (getLine >>= (return . map (read::String->Int) . words))
    sp <- (getLine >>= (return . map (read::String->Int) . words))
    print $ solve (a, b) sp

import List
s(n:a:b:t)=(\(x:y:_)->y-x).drop(b-1).sort$t
main=interact$show.s.map read.words

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import Data.Array

main = do
		[n,x,y]<- map read <$> words <$> getLine ::IO [Int]
		a<-reverse <$> sort <$> map read <$> words <$> getLine ::IO [Int]
		let x1 = last $ take x a
		let x2 = head $ drop x a
		print $ x1-x2

import Data.List

main = do
    [n,a,b] <- return . map (read :: String->Int) . words =<< getLine
    hs <- return . group . sort . map (read :: String->Int) . words =<< getLine
    let p = concat $ take b hs
        q = concat $ drop b hs
        r = if length p == b
            then head q - last p
            else 0
    print r


import Data.Function
import Data.List

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

solve :: Int -> [Int] -> [Int]
solve n xs
  | ok xs = f n xs
  | otherwise = [-1]

ok :: [Int] -> Bool
ok xs = not $ any (\(a,b) -> a>b) z
  where
    z = zip xs [1..]

f :: Int -> [Int] -> [Int]
f n xs = g n 0 zzzs rest
  where
    (vals,inds) = unzip $ map head $ groupBy ((==) `on` fst) $ zip xs [0..]
    zzzs = zip vals (tail inds)
    rest = h xs [0..]
    g m c [] (r:rs)
      | m == c = []
      | otherwise = r : g n (c+1) [] rs
    g m c ((v,i):vis) (r:rs)
      | i == c = v : g m (c+1) vis (r:rs)
      | otherwise = r :g m (c+1) ((v,i):vis) rs
    g _ _ _ [] = undefined

h :: [Int] -> [Int] -> [Int]
h (l:ls) (x:xss)
  | l == x = h ls xss
  | l > x = x : h (l:ls) xss
  | l < x = h ls (x:xss)
h [] xss = xss
h _ [] = undefined
h (_:_) (_:_) = undefined

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}

main = do
  getLine
  as <- map read . words <$> getLine
  putStrLn $ unwords . map show $ reverse $ solve (repeat (10 ^ 6)) $ reverse as

solve (q : qs) [0] = [q]
solve (q : qs) [_] = [0]
solve (q : qs) (a : a' : as)
  | a == a' = q : solve qs (a' : as)
  | otherwise = a' : solve ([a' + 1 .. a - 1] ++ q : qs) (a' : as)

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}

main = do
  getLine
  as <- map read . words <$> getLine
  putStrLn $ unwords . map show $ reverse $ solve (repeat (10 ^ 6)) $ reverse as

solve (q : qs) [0] = [q]
solve (q : qs) [_] = [0]
solve (q : qs) (a : a' : as)
  | a == a' = q : solve qs (a' : as)
  | otherwise = a' : solve ([a' + 1 .. a - 1] ++ qs) (a' : as)


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

process _ [] = putStr ""
process x (a:as) | elem a x = do
				putStrLn "YES"
				process x as
		 | otherwise = do
				putStrLn "NO"
				process (a:x) as

main = do
		n<- read <$> getLine::IO Int
		a<- replicateM n getLine
		process [] a

import Data.Set (empty,insert,member)

presence :: (Ord a) => [a] -> [Bool]
presence = tail.fst.unzip.scanl (\(_,set) x -> (member x set, insert x set)) (False,empty)

readInt :: String -> Int
readInt = read

showBool :: Bool -> String
showBool False = "NO"
showBool True = "YES"

main = do
    n <- fmap readInt getLine
    s <- fmap words getContents
    putStrLn.unlines.map showBool.presence $ s

-- {-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
module Main where
import qualified Data.ByteString.Lazy.Char8 as B
import Control.Monad
import Control.Monad.Trans.State.Lazy
import Data.List
import Data.Bool

main = putStr . evalState app . B.words =<< B.getContents

app = do
  n <- poi
  xs <- replicateM n pop
  return $ unlines $ map (bool "NO" "YES") $ solve xs
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  int = maybe undefined fst . B.readInt

solve = ap (zipWith elem) inits

solve2 :: [String] -> [String] -> [String]
solve2 _ [] = []
solve2 seen words@(w:ws) =
  if elem w seen
  then "YES" : (solve2 seen ws)
  else "NO" : (solve2 (w:seen) ws)

solve words = solve2 [] words

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

main = do
  (n:_) <- readInts
  c <- getContents
  putStrLn (unlines $ solve (take n $ lines c))

f ::String-> [String]->String
f str []="NO"
f str (x:xs)
    |str==x="YES"
    |otherwise=f str xs

f2::[String]->[String]->[String]
f2 _ []=[]
f2 xs (y:ys)=(f y xs):(f2 (y:xs) ys)

main = do
       e<-getLine
       es<-getContents
       let xs=lines es
       mapM_ putStrLn $ f2 [] xs

import Control.Applicative
import Control.Monad
import qualified Data.Set as S
import Data.List

main = sol . lines <$> (getLine *> getContents) >>= mapM_ putStrLn

sol = snd . foldl' f (S.empty,[])

f (a,ss) s = if S.member s a
    then (a,ss++["YES"])
    else (S.insert s a, ss++["NO"])

import Control.Monad
import Data.List
import Data.Maybe
import Data.Char

import qualified Data.ByteString.Char8 as B
readInt = readLn :: IO Int
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

possessedDiaryBefore :: [String] -> [String]
possessedDiaryBefore = fst . foldl (\(a, b) c -> if elem c b
                                                 then (a ++ ["YES"], b)
                                                 else (a ++ ["NO"],  b ++ [c])) ([], [])

main :: IO ()
main = do
    n <- readInt
    xs <- replicateM n $ getLine
    mapM_ putStrLn $ possessedDiaryBefore xs

import Control.Monad( replicateM)

main = do
    n_ <- readLn::IO Int
    names_ <- replicateM n_ getLine
    let
	proc []		    = []
	proc (n:ames)
	  | n `elem` ames   = True:(proc ames)
	  | otherwise	    = False:(proc ames)

	putStrLn' True = putStrLn "Yes"
	putStrLn' False = putStrLn "No"

    mapM_ putStrLn' . reverse . proc $ reverse names_

import Data.Set
run set ans (x:xs) | x `member` set = run set ("YES":ans) xs
                   | otherwise = run (x `insert` set) ("NO":ans) xs
run _ ans [] = reverse ans
main = getLine >> getContents >>= putStr . unlines . run empty [] . lines


module Main where

import List
import Numeric
import Array
import Monad

--readSeq readSpec 0 str = ([], str)
{-
readSeq readSpec n str = (x:xs, s)
                 where
                   (x,stail) = head (readSigned readSpec str)
                   (xs, s) = readSeq readSpec (n-1) stail
-}
--readDecs = readSeq readDec
--readFloats = readSeq readFloat

readSeqAll readSpec str = map (fst . head . readSigned readSpec) (words str)
readDecsAll = readSeqAll readDec
--readFloatsAll = readSeqAll readFloat

readStrings::Int -> IO [String]
readStrings 0 =
    do
      return([])
readStrings n =
    do
      l <- getLine
      tail <- readStrings (n-1)
      return(l : tail)

findMax arr = maximum [ 2*((i2-i+1)+(j2-j+1))
                        | i <- range(1,n),
                        j <- range(1,m),
                        i2 <- range(i,n),
                        j2 <- range(j,m),
                        sum [arr!(ii,jj) | ii <- range(i,i2), jj <- range(j,j2)] == 0]
              where
                ((1,1),(n,m)) = bounds arr

charToInt x =
    case x of
      '0' -> 0
      '1' -> 1

map2 f l = map (\x-> map f x) l

makeArray (n,m) lst = array ((1,1),(n,m))
                      [ ((i,j), (lst !! (i-1)) !! (j-1))
                        | i <- range(1,n), j <- range(1,m)]

main =
    do
      sz <- getLine
      let [n,m] = readDecsAll sz
      strs <- readStrings n
      putStrLn (show (findMax (makeArray (n,m) (map2 charToInt strs))))

import Control.Monad (liftM, replicateM)
import Data.Array (Array, (!), array, bounds)
import Data.List (intercalate)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

prints :: Show a => [a] -> IO ()
prints  =  putStrLn . intercalate " " . map show

type Map  =  Array (Int, Int) Char

solve :: Map -> Int
solve map  =  maximum
  [2 * (c-a+1 + d-b+1) | a <- [1..n], b <- [1..m], c <- [a..n], d <- [b..m], empty a b c d]
  where
    (n, m)  =  snd $ bounds map
    empty a b c d  =  and ['0' == map ! (i, j) | i <- [a..c], j <- [b..d]]

readMap :: Int -> Int -> IO Map
readMap n m  =  do
    lines  <-  replicateM n getLine
    return $ array ((1, 1), (n, m)) [((i+1, j+1), lines !! i !! j) | i <- [0..n-1], j <- [0..m-1]]

main :: IO ()
main  =  do
    [n, m]  <-  reads
    map  <-  readMap n m
    print $ solve map


import Data.Char (ord)
import Data.List

main :: IO ()
main = print . solve . parse =<< getContents

parse = map parseLine . tail . lines
        where parseLine l = let [x1, y1, x2, y2] = map read . words $ l
                            in  (x1, y1, x2, y2)

solve :: [(Int, Int, Int, Int)] -> Int
solve = sum . map (\(x1,y1,x2,y2) -> (x2-x1+1)*(y2-y1+1))

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import Text.Printf

readInt = ( readLn :: IO Int )
readInts = map ( read :: String -> Int ) . words <$> getLine

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

main = do
	n <- readInt
	res <- replicateM n $ do
		[ x1, y1, x2, y2 ] <- readInts
		return $ ( x2 - x1 + 1 ) * ( y2 - y1 + 1 )
	print $ sum res

-- http://codeforces.com/problemset/problem/552/A

getLines :: Int -> IO [String]
getLines 0 = return []
getLines n = do
	x <- getLine
	xs <- getLines (n-1)
	return (x:xs)

--------------------------------

type Coord = (Int, Int)
type Rect = (Coord, Coord)

toRect :: [Int] -> Rect
toRect (x1:y1:x2:y2:[]) = ((x1, y1), (x2, y2))

getArea :: Rect -> Int
getArea ((x1, y1), (x2, y2)) = (x2 - x1 + 1) * (y2 - y1 + 1)

--------------------------------

main :: IO()
main = do

	n <- fmap read $ getLine :: IO Int
	rects <-
		fmap (map toRect) $
		fmap (map . map $ read) $
		fmap (map words) $
		getLines n :: IO [Rect]

	let
		result = sum $ map getArea rects

	putStrLn $ show result

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns, OverloadedStrings #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  n <- readLn
  rs <- replicateM n getInts

  print $ sum $ map (\[x1, y1, x2, y2] -> (x2-x1+1)*(y2-y1+1)) rs

import Control.Exception.Base
import Data.Functor

readInts :: IO [Int]
readInts = map read <$> words <$> getContents

main = readInts >>= putStrLn . show . solve

solve :: [Int] -> Int
solve xs = assert (length xs `mod` 4 == 1) $ solve' $ tail xs

solve' :: [Int] -> Int
solve' [] = 0
solve' (x1:y1:x2:y2:ps) = (x2-x1+1) * (y2-y1+1) + solve' ps

main = interact $ show . f . map read . tail . words
f [] = 0
f (x:y:xx:yy:s) = (xx-x+1)*(yy-y+1) + f s

module Main where

import Control.Applicative

parseRect :: String -> [Int]
parseRect = map read . words

area :: [Int] -> Int
area [l, b, r, t] = (r - l + 1) * (t - b + 1)
area _ = 0

main :: IO ()
main = do
  _ <- getLine
  rects <- lines <$> getContents
  let areas = map (area . parseRect) rects
  print (sum areas)

answer::[[Int]] -> Int
answer a = sum (map rectsize a)
rectsize::[Int] -> Int
rectsize a = ((a !! 2)-(a !! 0)+1)* ((a !! 3)-(a !! 1)+1)
readInput::Int -> IO [[Int]]
readInput 0 = return []
readInput n = do
  w <- getLine
  let a = [read c::Int|c <- (words w)]
  next <- readInput(n-1)
  return ([a] ++ next)
main = do
  w <- getLine
  let n = read w::Int
  a <- readInput n
  print $ answer a

-- Codeforces 552A

import qualified Data.Map.Strict as Map

-- use Data.Map to storage the matrix

main :: IO ()
main = do
    n <- fmap read getLine :: IO Int
    getContents >>= print . sum . Map.elems . solve Map.empty . map read . words

solve :: Map.Map (Int, Int) Int -> [Int] -> Map.Map (Int, Int) Int
solve m [] = m
solve m (x1:y1:x2:y2:xs) = solve newm xs where
    newm = foldl (\mm loc -> Map.insertWith (+) loc 1 mm) m [(i, j) | i <- [x1..x2], j <- [y1..y2]]

main = interact solve

solve input = show answer
    where
        numbers = (map read . words) input
        n = head numbers
        rects = (four_by_four.tail) numbers
        answer = foldr folder 0 rects
        folder (x1,y1,x2,y2) ccount = ccount + ((y2-y1+1)*(x2-x1+1))

four_by_four [] = []
four_by_four (x1:y1:x2:y2:xs) = (x1,y1,x2,y2):(four_by_four xs)

import Data.Functor ((<$>))

main :: IO ()
main = print =<< solve <$> (map (map read <$> words) <$> tail <$> lines <$> getContents)

solve :: [[Int]] -> Int
solve = sum . map area

area :: [Int] -> Int
area [x1, y1, x2, y2] = (x2 - x1 + 1) * (y2 - y1 + 1)
area _ = undefined

import Control.Applicative

main = do
    getLine
    inp <- lines <$> getContents
    print $ sum $ do
        x <- inp
        let l = map read . words $ x :: [Int]
        return $ (l!!2 - l!!0 + 1) * (l!!3 - l!!1 + 1)

import Control.Applicative

process [] n = n
process ([a,b,c,d]:as) n = process as (n+(c-a+1)*(d-b+1))

main=do
	getLine
	a<- map (map read). map words . lines <$> getContents ::IO [[Int]]
	print $ process a 0

main :: IO()
main = print . solve . parse =<< getContents

parse = map parseLine . tail . lines
	where parseLine l = let [x1,y1,x2,y2] = map read . words $ l
			    in (x1,y1,x2,y2)

solve = sum . map area
	where area (x1,y1,x2,y2) = (x2 - x1 + 1) * (y2 - y1 +1)

main :: IO ()
main = print . solve . parse =<< getContents

parse = map parseLine . tail . lines
        where parseLine l = let [x1, y1, x2, y2] = map read . words $ l
                            in  (x1, y1, x2, y2)

solve = sum . map area
        where area (x1, y1, x2, y2) = (x2 - x1 + 1) * (y2 - y1 + 1)

{-# OPTIONS_GHC -rtsopts -O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MultiWayIf #-}

import Data.Array.ST
import Data.STRef
import Control.Monad.ST
import Control.Monad
import Control.Applicative
import qualified Data.List as L
import qualified Data.Set as S
import qualified Data.Map as M
import qualified Data.Function as F
import Debug.Trace

import qualified Numeric as N

readInt :: String -> Int
readInt ('-':s) = case N.readDec s of [(n, "")] -> -n
readInt s = case N.readDec s of [(n, "")] -> n

readFloat :: String -> Double
readFloat ('-':s) = case N.readFloat s of [(x, "")] -> -x
readFloat s = case N.readFloat s of [(x, "")] -> x

getInts :: IO [Int]
getInts = map readInt . words <$> getLine

main = do
  [n] <- getInts
  res <- sum <$> (forM [1..n] $ \_ -> do
    [a, b, c, d] <- getInts
    return $ ((c - a + 1) * (d - b + 1)))
  print res

parseInput input = zs where
    xs = tail $ lines input
    ys = map words xs
    zs = [map read y :: [Int] | y <- ys]

area [a, b, c, d] = (c - a + 1) * (d - b + 1)
solve xs = sum $ map area xs

main = do
    input <- getContents
    let xs = parseInput input
    print $ solve xs


module Main where

import Control.Monad (forM_, liftM2)
import qualified Data.ByteString.Char8 as C
import Data.Foldable (toList)
import Data.List (sort, transpose)
import Data.Maybe (fromJust)
import Data.Sequence (chunksOf, fromList)

solve :: Int -> C.ByteString -> String
solve i xs = concat (replicate mini "01") ++ replicate left more
  where
    [x, y] = map (fst . fromJust . C.readInt) (C.words xs)
    mini = min x y
    left = abs $ x - y
    more = if x > y then '0' else '1'

main :: IO ()
main = do
  n <- read <$> getLine
  mapM_ (\x -> putStrLn . solve x =<< C.getLine) [1 .. n]

--
-- Michael V. Antosha
-- 2022
-- Michael.Antosha@gmail.com
--

{-# language Safe                      #-}
{-# language ImportQualifiedPost       #-}
{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}
{-# options_ghc -Werror                          #-}

{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

import Prelude               hiding (getLine)
import Control.Arrow
import Control.Monad
import Data.ByteString.Char8 qualified as B
import Data.Char
import Data.List             qualified as L

ri = readInts <$> B.getLine :: IO [Int]
  where
    readInts  = L.unfoldr ((second dropSpace <$>) . B.readInt) . dropSpace
    dropSpace = B.dropWhile isSpace

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        [a,b] <- ri
        return (a,b)
    mapM_ (putStrLn.solve) tcs

solve (a,b) = ans
  where
    ans | (a < b)    = rr a "01" ++ r (b-a) '1'
        | otherwise  = rr b "01" ++ r (a-b) '0'

    r = replicate
    rr n = concat . r n

import Control.Monad (forM_, join)
import Data.Functor.Compose

main :: IO ()
main = do
    t <- readLn :: IO Integer
    forM_ [1..t] $ const solve

readArray :: Read a => IO [a]
readArray =
    fmap read <$> (words <$> getLine)

solve :: IO ()
solve = do
    [x, y] <- readArray :: IO [Int]
    putStrLn $ str x y '0'
    return ()

str :: Int -> Int -> Char -> String
str x y c
    | x + y == 0        = ""
    | c == '0' && y > 0 = "1" ++ str x (y-1) '1'
    | c == '0'          = "0" ++ str (x-1) y '1'
    | c == '1' && x > 0 = "0" ++ str (x-1) y '0'
    | c == '1'          = "1" ++ str x (y-1) '0'
    | otherwise = undefined

import Control.Monad (replicateM_)
main = read <$> getLine >>= flip replicateM_ solve
solve = map read . words <$> getLine >>= \[a,b]->
    putStrLn $ take (2 * min a b) (cycle "01") ++
        if a < b then replicate (b - a) '1' else replicate (a - b) '0'

import Control.Monad (replicateM_)
import Data.List (sort)
main = read <$> getLine >>= flip replicateM_ solve
solve = sort . map read . words <$> getLine >>= \[a',b']->
    let [(a,aa),(b,bb)] = sort [(a','0'),(b','1')] in
        let (d,m) = b `divMod` a in
            putStrLn . (++(replicate m bb)) . take (d*a+a) . cycle $
                replicate d bb ++ [aa]

import Control.Monad (replicateM_)
import Data.Tuple
data Unordered a = Unordered a
instance Eq (Unordered a) where
    (==) _ _ = True
instance Ord (Unordered a) where
    compare _ _ = EQ
deunorder (Unordered x) = x
main = read <$> getLine >>= flip replicateM_ solve
solve = map (read::String->Int) . words <$> getLine >>= \ [a,b] ->
    putStrLn . deunorder . snd . minimum . map (key creep) $ create a b
key f x = (f x, Unordered x)
creep :: String -> Int
creep = maximum . map (abs . uncurry (-)) . scanr (\c (a, b) -> if c == '0' then (a+1,b) else (a, b+1)) (0,0)
create :: Int -> Int -> [String]
create 0 b = [replicate b '1']
create a 0 = [replicate a '0']
create a b = (map ('0':) $ create (a - 1) b) ++ (map ('1':) . create a $ b - 1)

--
-- Michael V. Antosha
-- 2022
-- Michael.Antosha@gmail.com
--

{-# language Safe                      #-}
{-# language ImportQualifiedPost       #-}
{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}
{-# options_ghc -Werror                          #-}

{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

import Prelude               hiding (getLine)
import Control.Arrow
import Control.Monad
import Data.ByteString.Char8 qualified as B
import Data.Char
import Data.List             qualified as L

ri = readInts <$> B.getLine :: IO [Int]
  where
    readInts  = L.unfoldr ((second dropSpace <$>) . B.readInt) . dropSpace
    dropSpace = B.dropWhile isSpace

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        [a,b] <- ri
        return (a,b)
    mapM_ (putStrLn.solve) tcs

solve (a,b) = ans
  where
    ans | (a < b)    = rr a "01" ++ r (b-a) '0'
        | otherwise  = rr b "01" ++ r (a-b) '1'

    r = replicate
    rr n = concat . r n

--
-- Michael V. Antosha
-- 2022
-- Michael.Antosha@gmail.com
--

{-# language Safe                      #-}
{-# language ImportQualifiedPost       #-}
{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}
{-# options_ghc -Werror                          #-}

{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

import Prelude               hiding (getLine)
import Control.Arrow
import Control.Monad
import Data.ByteString.Char8 qualified as B
import Data.Char
import Data.List             qualified as L

ri = readInts <$> B.getLine :: IO [Int]
  where
    readInts  = L.unfoldr ((second dropSpace <$>) . B.readInt) . dropSpace
    dropSpace = B.dropWhile isSpace

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        [a,b] <- ri
        return (a,b)
    mapM_ (putStrLn.show.solve) tcs

solve (a,b) = (a,b,ans)
  where
    ans | (a < b)    = r a "01" ++ r (b-a) "1"
        | otherwise  = r b "01" ++ r (a-b) "0"

    r n = concat . replicate n

import Control.Monad (forM_, join)
import Data.Functor.Compose

main :: IO ()
main = do
    t <- readLn :: IO Integer
    forM_ [1..t] $ const solve

readArray :: Read a => IO [a]
readArray =
    fmap read <$> (words <$> getLine)

solve :: IO ()
solve = do
    [x, y] <- readArray :: IO [Int]
    putStrLn $ str x y '0'
    return ()

str :: Int -> Int -> Char -> String
str x y c
    | x + y == 0        = ""
    | c == '0' && y > 0 = "1" ++ str x (y-1) '1'
    | c == '0'          = "0" ++ str (x-1) y '1'
    | c == '1' && x > 0 = "0" ++ str (x-1) y '0'
    | c == '1'          = "0" ++ str x (y-1) '0'
    | otherwise = undefined

import Control.Monad (forM_, join)
import Data.Functor.Compose

main :: IO ()
main = do
    t <- readLn :: IO Integer
    forM_ [1..t] $ const solve

readArray :: Read a => IO [a]
readArray =
    fmap read <$> (words <$> getLine)

solve :: IO ()
solve = do
    [x, y] <- readArray :: IO [Int]
    print $ str x y '0'
    return ()

str :: Int -> Int -> Char -> String
str x y c
    | x + y == 0        = ""
    | c == '0' && y > 0 = "1" ++ str x (y-1) '1'
    | c == '0'          = "0" ++ str (x-1) y '1'
    | c == '1' && x > 0 = "0" ++ str (x-1) y '0'
    | c == '1'          = "0" ++ str x (y-1) '0'
    | otherwise = undefined

module Main where

import Control.Monad (forM_, liftM2)
import qualified Data.ByteString.Char8 as C
import Data.Foldable (toList)
import Data.List (sort, transpose)
import Data.Maybe (fromJust)
import Data.Sequence (chunksOf, fromList)

solve :: Int -> C.ByteString -> String
solve i xs = concat (replicate common "01") ++ replicate mini more
  where
    [x, y] = map (fst . fromJust . C.readInt) (C.words xs)
    common = abs $ x - y
    mini = min x y
    more = if x > y then '0' else '1'

main :: IO ()
main = do
  n <- read <$> getLine
  mapM_ (\x -> putStrLn . solve x =<< C.getLine) [1 .. n]


import Data.Array
import Control.Monad (replicateM_)

main = read <$> getLine >>= flip replicateM_ scase
scase = read <$> getLine >>=
        \n -> map read . words <$> getLine >>=
        print . maxVal . listArray (1, n)

maxVal :: Array Int Int -> Int
maxVal a = maximum [rotateWhole a, rotateStart a, rotateEnd a]

rotateWhole a = let (1, n) = bounds a
                    f a m i = if (m > (a!i - a!(i+1))) then m else  a!i - a!(i+1)
                in
                  foldl (f a) (a!n - a!1) [1..(n-1)]

rotateStart a = let (1, n) = bounds a in
                  (foldl max (a!1) (elems a)) - (a!1)

rotateEnd a = let (1, n) = bounds a in
                (a!n) - (foldl min (a!n) (elems a))

{-# LANGUAGE
  ScopedTypeVariables,
  BangPatterns
#-}

import qualified Data.ByteString.Char8 as C
import Data.List
import Data.Maybe
import Control.Monad
import Control.Monad.Writer
import Data.Semigroup
import Data.Coerce

tryall :: [Int] -> Writer (Max Int) ()
tryall as = do
  let a1 = head as
      an = last as
      apply = tell . coerce
  mapM_ (apply . (an -)) as
  mapM_ (apply . (subtract a1)) as
  mapM_ apply $ zipWith (-) as (tail as)

solve :: [Int] -> Int
solve = coerce . execWriter . tryall

main = do
  [!t] <- readInts
  replicateM_ t $ do
    _ <- readInts
    readInts >>= print . solve

readInts :: IO [Int]
readInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine


main :: IO ()
main = do
    getLine
    answers <- map (solve . read) <$> lines <$> getContents
    mapM_ print answers

solve :: Int -> Int
solve x = (x - 1) `div` 2

import Control.Monad (replicateM_)

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

solve :: Int -> Int
solve = flip div 2 . subtract 1

solve :: Int -> Int
solve n = (n-1) `div` 2

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

import Control.Monad (forM_)

main = do
	n <- fmap read getLine
	forM_ [1..n] $ \i -> do
		k <- fmap read getLine
		print ((k-1) `div` 2)

testCases :: Integer -> IO ()
testCases 0 = return ()
testCases t = do
    (n:_) <- readInts
    print $ if odd n then n `div` 2 else n `div` 2 - 1
    testCases (t-1)

main :: IO ()
main = do
    (t:_) <- readInts
    testCases t

readInts :: IO [Integer]
readInts = map read . words <$> getLine

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import Data.Array

onecase= do
		a<- read <$>  getLine ::IO Int
		print $ div (a-1)  2

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

process :: Int -> Int
process n = (n-1) `div` 2

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    t <- fmap readInt getLine
    ns <- fmap (map readInt.words) getContents
    mapM_ (print.process) ns

module Main where

import Control.Monad
import Data.List

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
        x <- readLn
        print $ (x - 1) `div` 2


import Data.Array.IO
import Control.Monad
import Data.IORef
import Data.Tuple
import Data.List
import Data.Char
import qualified Data.ByteString.Lazy as B

parse :: B.ByteString -> [Int]
parse s = do
    q <- B.splitWith (\x-> x == 13 || x == 10 || x == 32) s
    if B.length q == 0 then
        []
    else
        return $ foldl' (\a b-> a * 10 + (fromIntegral b) - (ord '0')) 0 (B.unpack q)

readNum :: IORef [a] -> IO a
readNum nums = do
    x:xs <- readIORef nums
    writeIORef nums xs
    return x

traverse :: IOArray Int (Bool, [Int]) -> IORef [Int] -> Int -> Int -> Bool -> Bool -> IO ()
traverse g res current parent prevFlag prevPrevFlag = do
    (flag, edges) <- readArray g current
    let shouldChange = flag /= prevPrevFlag
    if shouldChange then do
        resc <- readIORef res
        writeIORef res (current:resc)
    else return ()
    forM_ edges $ \next -> do
        if next == parent then return ()
        else traverse g res next current flag prevFlag

main :: IO ()
main = do
    nums0 <- B.getContents
    nums <- newIORef $ parse nums0
    n <- readNum nums
    g <- newListArray (1, n) (replicate n (False, []))
    forM_ [1..(n-1)] $ \i-> do
        a <- readNum nums
        b <- readNum nums
        (af, aa) <- readArray g a
        writeArray g a (af, (b:aa))
        (bf, bb) <- readArray g b
        writeArray g b (bf, (a:bb))
    forM_ [1..n] $ \i-> do
        a <- readNum nums
        (af, aa) <- readArray g i
        writeArray g i (a > 0, aa)
    forM_ [1..n] $ \i-> do
        a <- readNum nums
        (af, aa) <- readArray g i
        writeArray g i (af /= (a > 0), aa)
    z <- readIORef nums
    res <- newIORef []
    traverse g res 1 (-1) False False
    resc <- readIORef res
    putStrLn $ show $ length resc
    forM_ resc $ putStrLn.show

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, ViewPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray, runSTArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs = case B.readInt bs of Just (n,_) -> n
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

main :: IO ()
main = do
  n <- readLn
  (uvs, rest) <- splitAt(2*(n-1)).map readInt.B.words <$> B.getContents
  let (start, goal) = splitAt n $ map (1==) rest
  let nodes = solve n (mkTree . mkGraph (0,n-1) $ parse uvs)
                      (listArray (0,n-1) start)
                      (listArray (0,n-1) goal)
  print $ length nodes
  putStr.unlines $ map (show.(1+)) nodes

type Vertex = Int
type Edge = (Vertex, Vertex)
type Graph = Array Vertex [Vertex]
type Path = [Vertex]

mkGraph :: (Int, Int) -> [Edge] -> Graph
mkGraph bnd edges = unsafeAccumArray (flip (:)) [] bnd edges

mkTree :: Graph -> Graph
mkTree gr = runSTArray $ do
  vis  <- newArray (bounds gr) False :: ST s (STUArray s Vertex Bool)
  tree <- newArray (bounds gr) []    :: ST s (STArray s Vertex [Vertex])
  let visit v = do
        forM_ (unsafeAt gr v) $ \nv -> do
          visited <- unsafeRead vis nv
          unless visited $ do
            unsafeWrite vis nv True
            unsafeModify tree v (nv:)
            visit nv
  unsafeWrite vis 0 True
  visit 0
  return tree

parse (u:v:uvs) = (u-1,v-1) : (v-1,u-1) : parse uvs
parse _ = []

solve :: Int -> Graph -> UArray Int Bool -> UArray Int Bool -> [Vertex]
solve n gr start goal = runST $ do
  res <- newSTRef [] :: ST s (STRef s [Vertex])
  let go0 !flip0 !flip1 v
        | (flip0 /= unsafeAt start v) == unsafeAt goal v = mapM_ (go1 flip0 flip1) $ unsafeAt gr v
        | otherwise = do
            modifySTRef res (v:)
            mapM_ (go1 (not flip0) flip1) $ unsafeAt gr v
      go1 !flip0 !flip1 v
        | (flip1 /= unsafeAt start v) == unsafeAt goal v = mapM_ (go0 flip0 flip1) $ unsafeAt gr v
        | otherwise =  do
            modifySTRef res (v:)
            mapM_ (go0 flip0 (not flip1)) $ unsafeAt gr v
  go0 False False 0
  reverse <$> readSTRef res

{-# 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.List
import Data.Int
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 ()
inf = maxBound `div` 2 :: Int

newtype State s a = State { runState :: s -> (a,s) }

instance Functor (State s) where
    fmap f m = State $ runState m >>> first f

instance Monad (State s) where
    return x = State $ \s -> (x,s)
    x >>= f  = State $ \s ->
        let (a,s') = runState x s in runState (f a) s'

instance Applicative (State s) where
    pure = return
    (<*>) = ap

get :: State s s
get = State $ id &&& id
put :: s -> State s ()
put = State . const . ((),)
modify :: (s -> s) -> State s ()
modify = State . (.) ((,) ())
evalState :: State s a -> s -> a
evalState = (.) fst . runState
execState :: State s a -> s -> s
execState = (.) snd . runState

main = do
    n <- readLn
    (_es,_init:_goal:_) <- splitAt (n-1) . B.lines <$> B.getContents
    let es = map (B.words >>> map readInt >>> l2p) _es
        init = map readInt $ B.words _init
        goal = map readInt $ B.words _goal
    let l = solve n es init goal
    print $ length l
    putStr $ unlines $ map show l

i2b 0 = False
i2b 1 = True
xor False False = False
xor False True = True
xor True False = True
xor True True = False

solve :: Int -> [(Int,Int)] -> [Int] -> [Int] -> [Int]
solve n es init goal = execState (go True False False (-1) 1) []
    where
    g :: Array Int [Int]
    g = accumArray (flip (:)) [] (1,n) $ es ++ map swap es
    ia :: UArray Int Bool
    ia = listArray (1,n) (map i2b init)
    ga :: UArray Int Bool
    ga = listArray (1,n) (map i2b goal)
    go :: Bool -> Bool -> Bool -> Int -> Int -> State [Int] ()
    go True ep op prev v = do
        let ns = [ u | u <- g ! v, u /= prev ]
        let b = (ia ! v) `xor` ep
        if b == ga ! v then
            forM_ ns $ go False ep op v
        else do
            modify (v:)
            forM_ ns $ go False (not ep) op v
    go False ep op prev v = do
        let ns = [ u | u <- g ! v, u /= prev ]
        let b = (ia ! v) `xor` op
        if b == ga ! v then
            forM_ ns $ go True ep op v
        else do
            modify (v:)
            forM_ ns $ go True ep (not op) v


import Control.Monad

main :: IO ()
main = do
    t <- read <$> getLine
    replicateM_ t $ do
        n <- read <$> getLine
        s <- getLine
        print $ solve n s

solve :: Int -> String -> Int
solve n s
        | ('>' `notElem` s) || ('<' `notElem` s) = n
        | otherwise = snd $ foldl (\(p, c) x -> (x, fromEnum ('-' `elem` [p, x]) + c)) (last s, 0) s

import Control.Monad

main = do
  numTests <- read <$> getLine
  replicateM numTests $ do
    _ <- getLine
    str <- getLine
    putStrLn $ show $ solve str

solve :: String -> Int
solve str =
  if elem '<' str && elem '>' str
    then countLines str
    else length str

countLines :: String -> Int
countLines str = countLines' str False - (if hs == '-' && ls == '-' then 1 else 0)
  where
    hs = head str
    ls = last str
    countLines' [] _ = 0
    countLines' (x:xs) streak =
      if x == '-'
        then if streak
               then 1 + countLines' xs True
               else 2 + countLines' xs True
        else 0 + countLines' xs False

data Belt = Clockwise | AntiClockwise | Off deriving (Eq)

nNonReturnableRooms :: Bool -> Bool -> [Belt] -> Int
nNonReturnableRooms _ _ [] = 0
nNonReturnableRooms _ _ (h:[]) = 0
nNonReturnableRooms clockPath counterPath (a:b:t)
  | a == Clockwise && b == AntiClockwise = 1 + nNonReturnableRooms clockPath counterPath rest
  | a == AntiClockwise && b == Clockwise = 1 + nNonReturnableRooms clockPath counterPath rest
  | a == Clockwise && b == Clockwise && (not clockPath) = 1 + nNonReturnableRooms clockPath counterPath rest
  | a == AntiClockwise && b == AntiClockwise && (not counterPath) = 1 + nNonReturnableRooms clockPath counterPath rest
  | otherwise = nNonReturnableRooms clockPath counterPath rest
  where rest = (b:t)

nReturnableRooms :: [Belt] -> Int
nReturnableRooms belts = l - (nNonReturnableRooms clockPath counterPath ((belts !! (l-1)):belts))
    where clockPath = all (\belt -> belt == Clockwise || belt == Off) belts
          counterPath = all (\belt -> belt == AntiClockwise || belt == Off) belts
          l = (length belts)

readBelts :: String -> [Belt]
readBelts "" = []
readBelts (n:t)
  | n == '>' = (Clockwise:readBelts t)
  | n == '<' = (AntiClockwise:readBelts t)
  | n == '-' = (Off:readBelts t)

dropEven :: [a] -> [a]
dropEven [] = []
dropEven (a:b:t) = (b:(dropEven t))

main :: IO()
main = do
    interact $ unlines . map (show . nReturnableRooms . readBelts) . dropEven . tail . lines

import Control.Monad

solve :: [Char] -> Int
solve str = let
  w = [p | p <- str, p /= '-']
  canRot = case  w  of
    [] -> True
    (p : ps) -> all (== p) ps
  in  case  str  of
  _ | canRot  -> length str
  [] -> error "n < 2"
  (p : ps) -> length [() | (x, y) <- zip str $ ps ++ [p], x == '-' || y == '-']

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _n <- getLine
    s <- getLine
    print $ solve s

import Control.Monad

solve :: [Char] -> Int
solve str = case  str  of
  [] -> 0
  (p:_) | all (== p) str  -> length str
  (p:ps) -> length [() | (x, y) <- zip str $ ps ++ [p], x == '-' || y == '-']

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _n <- getLine
    s <- getLine
    print $ solve s


import Data.List
main = getLine >> getLine >>= print . (*pi) . foldr1 (-) . reverse . sort . map ((^2) . read) . words

import List
main = do getLine; getLine >>= print . (pi*) . foldl1 subtract . sort . map ((**2) . read) . words

import Data.List
fu xs = reverse $ sort xs
fun xs x i | null xs = x*3.141592653589793 | i `mod` 2 == 0 = fun (tail xs) (x+y*y) (i+1)| otherwise = fun (tail xs) (x-y*y ) (i+1)
 where y = head xs
solve s = fun (fu $ tail s) 0 2
main = interact $ show . solve . map read . words

import Data.List
solve :: Int -> [Int] -> Double
solve n xs = pi * fromIntegral (square_sum xs')
    where xs' | even (length xs) = sort xs
              | otherwise = 0 : (sort xs)
          square_sum [] = 0
          square_sum (x:y:ys) = (x + y) * (y - x) + square_sum ys

main = do
  n <- readLn
  getLine >>=
       (\str -> putStrLn $
                (show . solve n . map read . words) str)

import Control.Applicative
import Data.List

solve [] = 0
solve a = pi*(last a)^2 - solve (init a)

main = getLine >> do
    rs <- map read . words <$> getLine
    print $ solve (sort rs)

import List

readWords :: IO [String]
readWords = getLine >>= return . words

reads :: Read a => IO [a]
reads = readWords >>= return . (map read)

solve :: [Double] -> Double
solve xs  =  sum $ zipWith (\r sgn -> sgn * r * r * pi) (reverse $ sort xs) (cycle [1, -1])

main :: IO ()
main  =  getLine >> Main.reads >>= print . solve

module Main where
import Data.List
main :: IO ()
main = do
    _ <- readLn :: IO Int
    l <- getLine
    let radii = reverse (sort (map (read :: String -> Int) (words l)))
        radiiSquared = map (^2) radii
        radiiSquaredWithIndex = zipWith (,) [0..] radiiSquared
        evenRadii = [r | (i,r) <- radiiSquaredWithIndex, even i]
        oddRadii = [r | (i,r) <- radiiSquaredWithIndex, odd i]
        piCoeff = sum evenRadii - sum oddRadii
        redArea = (fromIntegral piCoeff :: Float) * pi
    putStrLn (show redArea)

import Data.List
import Control.Applicative
import Text.Printf

main = getLine >> do
  rs <- map read . words <$> getLine
  printf "%.5f" $ solve $ sortBy (flip compare) rs

solve :: [Int] -> Double
solve rs = (pi*) $ fromIntegral $ sum $ zipWith (#) rs $ cycle [1,-1]
   where r#n=r*r*n

import List
s(_:r)=pi*sum(zipWith(*)(cycle[1,-1])$reverse$sort r)
main=interact$show.s.map((^2).read).words

import Data.List
main = do
  n <- readLn
  a <- getLine >>= return. Data.List.sort. map read. words
  let nth = ((0:a) !!)
  print. (*pi). sum $ [(nth i)^2 - (nth (i - 1))^2 | i <- [n, n - 2..1]]

import           Data.List

neg x = if even x then 1 else -1

enumerate :: Integral a => [a] -> [(a, a)]
enumerate = zip [0..]

main :: IO ()
main = do
  n <- (read :: String -> Int) <$> getLine
  let folder a (i, r) = a + neg i * r^2
  print =<< (*pi) . fromIntegral . foldl folder 0 . enumerate . sortBy (flip compare) . map (read :: String -> Int) . words <$> getLine

import Prelude hiding (readList)

import System.IO

import Data.Word
import Data.List
import Data.ByteString.Lazy       as B  hiding (map, reverse)
import Data.ByteString.Lazy.Char8 as BC hiding (map, reverse)

main = do
    hSetBuffering stdin LineBuffering
    first           <- fmap BC.pack System.IO.getLine
    second          <- fmap BC.pack System.IO.getLine
    let Just ( n,_) =  readNum                   first
    let Just (rs,_) =  readList (fromIntegral n) second
    print $ sum $ map (uncurry square) $ toPairs $ reverse $ sort $ 0:rs

toPairs :: [a] -> [(a,a)]
toPairs      []  = []
toPairs   (_:[]) = []
toPairs (a:b:ts) = (a,b):(toPairs ts)

square  :: Word16 -> Word16 -> Float
square a b = if a < b -- на всякий случай
             then square' b a
             else square' a b
    where
        square' a b = pi * ab1 * ab2
        ab1 = fromIntegral $ a - b
        ab2 = fromIntegral $ a + b

readList :: Word8 -> ByteString -> Maybe ([Word16], ByteString)
readList n source = readList' n [] source
    where
        readList' 0 acc source = return (acc, source)
        readList' i acc source = do
            (curr, source) <- readNum  source
            readList' (i-1) (curr:acc) source

readNum  ::          ByteString -> Maybe ( Word16  , ByteString)
readNum  source = fmap convert $ readInt source
    where
        convert (a,b) = (fromIntegral a, trim b)
        trim source = case BC.uncons source of
            Just (c, trimmed) | c == '\n' || c == '\r' || c == ' '
                -> trim trimmed
            _   -> source

import Data.List
import Control.Monad

solve :: [Double] -> Double
solve [] = 0
solve [red] = red ^ 2
solve (red:blue:rest) = red ^ 2 - blue ^ 2 + solve rest

main = do
  _ <- getLine
  rows <- getLine >>= (return . map (read::String->Double) . words)
  print . (* pi) . solve . reverse . sort $ rows

import Data.List
main = getLine >> getLine >>= print . (*pi) . foldr1 (-) . sort . map ((^2) . read) . words

import List
s(_:r)=pi*sum(zipWith(*)(cycle[1,-1])r)
main=interact$show.s.reverse.sort.map((^2).read).words

import           Data.List

neg x = if even x then 1 else -1

enumerate :: Integral a => [a] -> [(a, a)]
enumerate = zip [0..]

main :: IO ()
main = do
  n <- (read :: String -> Int) <$> getLine
  let folder a (i, r) = a + neg i * r^2
  print =<< (*pi) . fromIntegral . foldl folder 0 . enumerate . sort . map (read :: String -> Int) . words <$> getLine


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

main = do
  t <- readLn :: IO Int

  replicateM_ t $ do
    a <- readLn :: IO Int
    print $ 2 ^ (popCount a)

-- Codeforces 1064B
import Data.Bits
import Data.List

counter :: Integer -> Integer
counter 0 = 1
counter 1 = 2
counter x = if x `mod` 2 == 0
            then 1 * counter (x `div` 2)
            else 2 * counter (x `div` 2)

main :: IO ()
main = do
    input <- getLine
    let n = read input :: Int
    inputs <- getContents
    let numbers = map (read :: String -> Integer) (lines inputs)
    mapM_ (print . counter) (take n numbers)


{-# LANGUAGE BangPatterns #-}
import System.IO
import Control.Applicative
import Control.Monad
import Data.Char
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Function (fix)
import Data.Array (Array, array, (!))
import Data.Array.ST
import Data.Array.Unboxed
import Data.ByteString.Lazy.Builder
import Data.ByteString.Lazy.Builder.ASCII
import Control.Monad.ST as ST
import qualified Data.Set as Set
import qualified Data.Map.Strict as Map
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

construct :: (BS.ByteString -> Maybe (a, BS.ByteString)) -> BS.ByteString -> [a]
construct reader str
    | BS.null str = []
    | isSpace (BS8.head str) = construct reader (BS8.tail str)
    | otherwise = let Just (i, other) = reader str in i : construct reader other

getInts :: IO [Int]
getInts = construct BS8.readInt <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = construct BS8.readInteger <$> BS.getLine

printInts :: [Int] -> IO ()
printInts = putStrLn . intercalate " " . map show

solve' :: [(Int, Int)] -> Int
solve' xs = runST $ do
  let n = length xs + 5
  dp <- newArray (0, n) 0 :: ST s (STUArray s Int Int)
  h <- newArray (0, n) 1 :: ST s (STUArray s Int Int)
  forM_ xs $ \(p, i) -> do
    idp <- readArray dp i
    ih <- readArray h i
    writeArray dp i $ max idp ih
    pdp <- readArray dp p
    ph <- readArray h p
    writeArray h p $ max ph (ih + 1)
    writeArray dp p $ pdp + max ih idp
  readArray dp 1

solve xs = solve' (reverse $ zip (0:xs) [1..])

main = do
  _ <- getInts
  xs <- getInts
  print $ solve xs

{-# LANGUAGE BangPatterns #-}
import System.IO
import Control.Applicative
import Control.Monad
import Data.Char
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Function (fix)
import Data.Array (Array, array, (!))
import Data.Array.ST
import Data.Array.Unboxed
import Data.ByteString.Lazy.Builder
import Data.ByteString.Lazy.Builder.ASCII
import Control.Monad.ST as ST
import qualified Data.Set as Set
import qualified Data.Map.Strict as Map
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

construct :: (BS.ByteString -> Maybe (a, BS.ByteString)) -> BS.ByteString -> [a]
construct reader str
    | BS.null str = []
    | isSpace (BS8.head str) = construct reader (BS8.tail str)
    | otherwise = let Just (i, other) = reader str in i : construct reader other

getInts :: IO [Int]
getInts = construct BS8.readInt <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = construct BS8.readInteger <$> BS.getLine

printInts :: [Int] -> IO ()
printInts = putStrLn . intercalate " " . map show

solve' ((p, i):xs) dp h = if i == 1 then idp else solve' xs dp' h'
  where ih = h Map.! i
        idp = max (dp Map.! i) ih
        dp' = Map.adjust (+idp) p dp
        h' = Map.adjust (\ph -> max ph $ ih + 1) p h

solve xs = solve' (reverse $ zip (0:xs) [1..]) dp h
  where dp = Map.fromList (zip [0..length xs + 1] $ repeat 0)
        h = Map.fromList (zip [0..length xs + 1] $ repeat 1)

main = do
  _ <- getInts
  xs <- getInts
  print $ solve xs

{-# LANGUAGE BangPatterns #-}
import System.IO
import Control.Applicative
import Control.Monad
import Data.Char
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Function (fix)
import Data.Array (Array, array, (!))
import Data.Array.ST
import Data.Array.Unboxed
import Data.ByteString.Lazy.Builder
import Data.ByteString.Lazy.Builder.ASCII
import Control.Monad.ST as ST
import qualified Data.Set as Set
import qualified Data.Map.Strict as Map
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

construct :: (BS.ByteString -> Maybe (a, BS.ByteString)) -> BS.ByteString -> [a]
construct reader str
    | BS.null str = []
    | isSpace (BS8.head str) = construct reader (BS8.tail str)
    | otherwise = let Just (i, other) = reader str in i : construct reader other

getInts :: IO [Int]
getInts = construct BS8.readInt <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = construct BS8.readInteger <$> BS.getLine

printInts :: [Int] -> IO ()
printInts = putStrLn . intercalate " " . map show

solve' :: [(Int, Int)] -> Int
solve' xs = runST $ do
  let n = length xs + 5
  dp <- newArray (0, n) 0 :: ST s (STUArray s Int Int)
  h <- newArray (0, n) 1 :: ST s (STUArray s Int Int)
  forM_ xs $ \(p, i) -> do
    idp <- readArray dp i
    ih <- readArray h i
    pdp <- readArray dp p
    ph <- readArray h p
    writeArray h p $ max ph (ih + 1)
    writeArray dp p $ pdp + max ih idp
  readArray dp 1

solve xs = solve' (reverse $ zip (0:xs) [1..])

main = do
  _ <- getInts
  xs <- getInts
  print $ solve xs


import Debug.Trace
import qualified Data.Map as M
import qualified Data.Foldable as F

count _ [] m = m
--count a b c | trace ("count " ++ show a ++ " " ++ show b ++ " " ++ show c) False = undefined
count n (x:xs) m
  | x >= n = M.insertWith (+) (x-n) 1 $ count (n+1) xs m
  | otherwise = count (n+1) xs m

solve n xs =
  let former = count 1 (take ((n+1)`div`2) xs) M.empty
      all = count 1 (take (n`div`2) $ reverse xs) former
  in if M.null all then 0 else n - (F.maximum all)

main = do
  n <- readLn
  xstr <- getLine
  let xs = map read $ words xstr
  print $ solve n xs

import Data.List
buildSeq n = let left = [1..((n + 1) `div` 2)] in
	left ++ ((if n `mod` 2 == 1 then tail else id) $ reverse left)

main = do { nl <- getLine
	; s <- getLine
	; let {n = read nl; ll = map read $ words s}
	; let seq1 = buildSeq n
	; print (n - (head $reverse $ sort $ map length $ filter (\(h:_) -> h >= 0 ) $
		group $ sort $ zipWith (\x y -> x - y) ll seq1))
}

import Data.List
main = interact (show.solve.map read.words)
solve (n:vs) = n-(maximum.map length.group.sort.filter(>=0)) differ where
 d = div n 2
 base = if mod n 2==0 then [1..d]++[d,d-1..1] else [1..d]++[d+1]++[d,d-1..1]
 differ = zipWith (-) vs base

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

import Data.Char
import Data.List
import qualified Data.Map as M
-- import Data.Map ((!))
import qualified Data.IntMap as IM
-- import Data.IntMap ((!))

import Data.Array
import Data.Function

import Debug.Trace

import Numeric

debug = flip trace
-- debug = const

int :: IO Int
int = read <$> getLine

ints :: IO [Int]
ints = map read . words <$> getLine

double :: IO Double
double = read <$> getLine

doubles :: IO [Double]
doubles = read <$> getLine

sum' = foldl' (+) 0
product' = foldl' (*) 1

fi = fromIntegral

solve :: Int -> [Int] -> Array Int Int
solve n lst =
  let arr0 = listArray (0, 10^5 - 1) (repeat 0)
      lst0 = zipWith (-) lst (beautiful n 0)
  in  foldl' vote arr0 lst0

solve2 :: Int -> [Int] -> [(Int, Int)]
solve2 n lst =
  let lst0 = zipWith (-) lst (beautiful n 0)
      gps  = map (head &&& length) (group (sort (filter (> 0) lst0)))
  in  gps

vote :: Array Int Int -> Int -> Array Int Int
vote arr vt
  | inRange (bounds arr) vt =
    let nextVal = (arr ! vt) + 1
    in  nextVal `seq` arr // [(vt, nextVal)]
  | otherwise = arr

beautiful n a0
  | odd n = [a0 .. at] ++ reverse [a0 .. at - 1]
  | otherwise  = [a0 .. at-1] ++ reverse [a0 .. at - 1]
  where at = a0 + n `div` 2

difference a b = length (filter id (zipWith (/=) a b))

main = do
  n <- int
  lst <- ints
  let val = fst (maximumBy (compare `on` snd) (assocs (solve n lst)))
      val2 = fst (maximumBy (compare `on` snd) (solve2 n lst))
--  print (solve n lst)
--  print val
--  print (difference (beautiful n val) lst)
--  print val2
  print (difference (beautiful n val2) lst)

module Main where

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

main = do
    n <- read `liftM` getLine
    tree <- ( map read . words ) `liftM` getLine
    let tree' = map (\(t, c) -> t - c) $ zip tree ( [1 .. n`div`2] ++ reverse [1 .. (n+1)`div`2] )
        countArray = runSTArray $ do
            countArray <- newArray (0, 10^5) 0 :: ST s (STArray s Int Int)
            forM_ tree' $ \t ->
                if t >= 0
                    then readArray countArray t >>= writeArray countArray t . (1+)
                    else return ()
            return countArray
        fixN = maximum $ elems countArray

    putStrLn $ show ( n - fixN )

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

import Data.Char
import Data.List
import qualified Data.Map as M
-- import Data.Map ((!))
import qualified Data.IntMap as IM
-- import Data.IntMap ((!))

import Data.Array
import Data.Function

import Debug.Trace

import Numeric

debug = flip trace
-- debug = const

int :: IO Int
int = read <$> getLine

ints :: IO [Int]
ints = map read . words <$> getLine

double :: IO Double
double = read <$> getLine

doubles :: IO [Double]
doubles = read <$> getLine

sum' = foldl' (+) 0
product' = foldl' (*) 1

fi = fromIntegral

solve :: Int -> [Int] -> Array Int Int
solve n lst =
  let arr0 = listArray (0, 10^5 - 1) (repeat 0)
      lst0 = zipWith (-) lst (beautiful n 0)
  in  foldl' vote arr0 lst0

solve2 :: Int -> [Int] -> [(Int, Int)]
solve2 n lst =
  let lst0 = zipWith (-) lst (beautiful n 0)
      gps  = map (head &&& length) (group (sort (filter (> 0) lst0)))
  in  gps

vote :: Array Int Int -> Int -> Array Int Int
vote arr vt
  | inRange (bounds arr) vt =
    let nextVal = (arr ! vt) + 1
    in  nextVal `seq` arr // [(vt, nextVal)]
  | otherwise = arr

beautiful n a0
  | odd n = [a0 .. at] ++ reverse [a0 .. at - 1]
  | otherwise  = [a0 .. at-1] ++ reverse [a0 .. at - 1]
  where at = a0 + n `div` 2

difference a b = length (filter id (zipWith (/=) a b))

main = do
  n <- int
  lst <- ints
  let val = fst (maximumBy (compare `on` snd) (assocs (solve n lst)))
      val2 = fst (maximumBy (compare `on` snd) (solve2 n lst))
--  print (solve n lst)
--  print val
--  print (difference (beautiful n val) lst)
  print val2
  print (difference (beautiful n val2) lst)

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

import Data.Char
import Data.List
import qualified Data.Map as M
-- import Data.Map ((!))
import qualified Data.IntMap as IM
-- import Data.IntMap ((!))

import Data.Array
import Data.Function

import Debug.Trace

import Numeric

debug = flip trace
-- debug = const

int :: IO Int
int = read <$> getLine

ints :: IO [Int]
ints = map read . words <$> getLine

double :: IO Double
double = read <$> getLine

doubles :: IO [Double]
doubles = read <$> getLine

sum' = foldl' (+) 0
product' = foldl' (*) 1

fi = fromIntegral

solve :: Int -> [Int] -> Array Int Int
solve n lst =
  let arr0 = listArray (0, 10^5 - 1) (repeat 0)
      lst0 = zipWith (-) lst (beautiful n 0)
  in  foldl' vote arr0 lst0

solve2 :: Int -> [Int] -> [(Int, Int)]
solve2 n lst =
  let lst0 = zipWith (-) lst (beautiful n 0)
      gps  = map (head &&& length) (group (sort lst0))
  in  gps

vote :: Array Int Int -> Int -> Array Int Int
vote arr vt
  | inRange (bounds arr) vt =
    let nextVal = (arr ! vt) + 1
    in  nextVal `seq` arr // [(vt, nextVal)]
  | otherwise = arr

beautiful n a0
  | odd n = [a0 .. at] ++ reverse [a0 .. at - 1]
  | otherwise  = [a0 .. at-1] ++ reverse [a0 .. at - 1]
  where at = a0 + n `div` 2

difference a b = length (filter id (zipWith (/=) a b))

main = do
  n <- int
  lst <- ints
  let val = fst (maximumBy (compare `on` snd) (assocs (solve n lst)))
      val2 = fst (maximumBy (compare `on` snd) (solve2 n lst))
--  print (solve n lst)
--  print val
--  print (difference (beautiful n val) lst)
--  print val2
  print (difference (beautiful n val2) lst)

import Debug.Trace
import qualified Data.Map as M
import qualified Data.Foldable as F

count _ [] m = m
--count a b c | trace ("count " ++ show a ++ " " ++ show b ++ " " ++ show c) False = undefined
count n (x:xs) m
  | x >= n = M.insertWith (+) (x-n) 1 $ count (n+1) xs m
  | otherwise = count (n+1) xs m

solve n xs =
  let former = count 1 (take ((n+1)`div`2) xs) M.empty
      all = count 1 (take (n`div`2) xs) former
  in if M.null all then 0 else n - (F.maximum all)

main = do
  n <- readLn
  xstr <- getLine
  let xs = map read $ words xstr
  print $ solve n xs

module Main where

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

main = do
    n <- read `liftM` getLine
    tree <- ( map read . words ) `liftM` getLine
    let tree' = map (\(t, c) -> t - c) $ zip tree ( [1 .. n`div`2] ++ reverse [1 .. (n+1)`div`2] )
        countArray = runSTArray $ do
            countArray <- newArray (-10^5, 10^5) 0 :: ST s (STArray s Int Int)
            forM_ tree' $ \t ->
                readArray countArray t >>= writeArray countArray t . (1+)
            return countArray
        fixN = maximum $ elems countArray

    putStrLn $ show ( n - fixN )


import Data.List
import Control.Monad

nubOrd :: Eq a => [a] -> [a]
nubOrd = foldr removeDuplicates []
  where
    removeDuplicates :: Eq a => a -> [a] -> [a]
    removeDuplicates curr acc
      | null acc = curr:acc
      | head acc == curr = acc
      | otherwise = curr:acc

main :: IO ()
main = do
  t <- read <$> getLine :: IO Int
  replicateM_ t $ do
    [_,r] <- map read . words <$> getLine :: IO [Int]
    monsters <- map read . words <$> getLine :: IO [Int]
    let sorted = nubOrd $ sort monsters
    print $ foldr (minimumShots r) 0 sorted

  where
    minimumShots :: Int -> Int -> Int -> Int
    minimumShots rad curr shots
      | rad * shots < curr = shots + 1
      | otherwise = shots

{-# 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
readInteger = readLn :: IO Integer
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readIntegers = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine

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

modifyArray a i f = writeArray a i =<< f <$> readArray a i

main = readInt >>= flip replicateM solve >>= mapM_ print

solve = do
	[ n, r ] <- readInts
	xs <- reverse . map head . group . sort <$> readInts
	return $ solve' 0 r xs

solve' _ _ [] = 0
solve' d r (x:xs)
	| x <= d * r = 0
	| otherwise = 1 + solve' ( succ d ) r xs

{-# LANGUAGE BangPatterns #-}

import Data.List
import Data.Set (Set)
import qualified Data.Set as Set
import Control.Monad

main = do
  t <- read <$> getLine :: IO Int
  replicateM_ t $ do
    line <- words <$> getLine
    let [n,r] = read <$> line :: [Int]
    line <- words <$> getLine
    let pos = Set.toDescList . Set.fromList $ (read :: String -> Int) <$> line
    let solve :: [Int] -> Int -> Int -> Int
        solve [] !_ !acc = acc
        solve (x:xs) dist acc
          | x <= dist = acc
          | otherwise = solve xs (dist + r) (acc + 1)
    print $ solve pos 0 0

{-# 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
readInteger = readLn :: IO Integer
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readIntegers = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine

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

modifyArray a i f = writeArray a i =<< f <$> readArray a i

main = readInt >>= flip replicateM solve >>= mapM_ print

solve = do
	[ n, r ] <- readInts
	xs <- reverse . map head . group . sort <$> readInts
	return $ solve' 0 xs

solve' _ [] = 0
solve' d (x:xs)
	| x <= d = 0
	| otherwise = 1 + solve' ( succ d ) xs


import qualified Data.ByteString.Char8 as B
import Data.List
import Data.Maybe

main = do
	tmp <- B.getContents
	let
		n:remain = map (fromIntegral.fst.fromJust.B.readInteger) $ B.words tmp
		get_tpl [] = []
		get_tpl (x:y:xs) = (x, 100-y) : (get_tpl xs)
		cmp (a,b) (c,d) = if (100-b)*a*d > (100-d)*c*b then
							LT else
								if (100-b)*a*d < (100-d)*c*b then GT else EQ
		songs = sortBy cmp $ get_tpl remain
		p_sum = sum $ map (\a -> snd a) songs
		ans = foldl (\var song -> ((fst var)+(fst song)+ (100 - (snd song))/100.0*(fst song)*((snd var)-(snd song))/100.0 , (snd var)-(snd song))) (0,p_sum) songs
	print $ fst ans

import Data.Int
import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as C

pairs :: [a] -> [(a, a)]
pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main :: IO ()
main = do
  (_:input) <- fmap (map (fst . fromJust . C.readInt) . C.words) C.getContents
  let a = pairs $ map fromIntegral input
      cmp (l1, p1) (l2, p2) = compare (l2 * p2 * (100 - p1)) (l1 * p1 * (100 - p2))
      b = sortBy cmp $ a
      gao (e, s) (l, p) = e `seq` s `seq` (e + l * p, s + l * 10000 + e * (100 - p))
      ans = snd $ foldl' gao (0, 0) b
  print $ (fromIntegral :: Int64 -> Double) ans / 10000

{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import qualified Data.ByteString.Char8 as B
import Data.List

toPairs (x:y:xys) = (x,y) : toPairs xys
toPairs _ = []

readInteger :: B.ByteString -> Integer
readInteger bs = case B.readInteger bs of Just (n,_) -> n

comp (l1,p1) (l2,p2) = compare (l2*p2*(100-p1)) $  l1*p1*(100-p2)

main :: IO ()
main = do
  _ <- getLine
  lps <- sortBy comp.toPairs.map readInteger.B.words <$> B.getContents
  let ans = 10000 * sum [l|(l,_)<-lps]
          + sum(zipWith (*) [l*p|(l,p)<-lps] (scanr1(+)[100-p|(_,p)<-tail lps]))
  putStrLn $ showAns ans

showAns 0 = "0"
showAns n
  | n>=10000 = let (xs,ys) = splitAt 4.reverse $ show n in reverse ys++"."++reverse xs
  | otherwise = "0."++ reverse(take 4(reverse (show n)++"0000"))

{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import qualified Data.ByteString.Char8 as B
import Data.List
import Data.Maybe
import Data.Ord

toPairs (x:y:xys) = (x,y) : toPairs xys
toPairs _ = []

readInteger :: B.ByteString -> Integer
readInteger bs = case B.readInteger bs of Just (n,_) -> n

comp = comparing $ uncurry (*)

main :: IO ()
main = do
  n <- readLn :: IO Int
  lps <- sortBy(flip comp).toPairs.map readInteger.B.words <$> B.getContents
  let ans = 10000 * sum [l|(l,_)<-lps]
          + sum(zipWith (*) [l*p|(l,p)<-lps] (scanr1(+)[100-p|(_,p)<-tail lps]))
  putStrLn $ showAns ans

showAns 0 = "0"
showAns n
  | n>=10000 = let (xs,ys) = splitAt 4.reverse $ show n in reverse ys++"."++reverse xs
  | otherwise = "0."++ reverse(take 4(reverse (show n)++"0000"))

import qualified Data.ByteString.Char8 as B
import Data.List
import Data.Maybe

main = do
	tmp <- B.getContents
	let
		n:remain = map (fromIntegral.fst.fromJust.B.readInteger) $ B.words tmp
		get_tpl [] = []
		get_tpl (x:y:xs) = (x, (100 - y)/100.0) : (get_tpl xs)
		cmp (a,b) (c,d) = if a*d > c*b then
							LT else
								if a*d < c*b then GT else EQ
		songs = sortBy cmp $ get_tpl remain
		p_sum = sum $ map (\a -> snd a) songs
		ans = foldl (\var song -> ((fst var)+(fst song)+(1.0 - (snd song))*(fst song)*((snd var)-(snd song)) , (snd var)-(snd song))) (0,p_sum) songs
	print $ fst ans


--
-- Michael V. Antosha
-- 2021
-- Michael.Antosha@gmail.com
--

{-# language NoMonomorphismRestriction #-}

{-# options_ghc -Weverything                     #-}

{-# options_ghc -Wno-safe                        #-}     -- no warning if inferred as safe
{-# options_ghc -Wno-all-missed-specialisations  #-}     -- "Could not specialise..." when -O1
{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

-- (ghc -Werror -O1 -rtsopts -o a.out *.hs  &&  set -o pipefail  &&  cat -- input.txt | ./a.out > temp.txt  &&  diff -sdu -- output.txt temp.txt)  &&  echo OKAY  ||  echo ERROR

import Prelude
import Control.Monad

main :: IO ()
main = do
    t <- readLn
    tcs <- replicateM t readLn  :: IO [Integer]

    (putStr . unlines . map solve) tcs

----------------------------------------------------------------------

solve n = show (0 - (n-1)) ++ " " ++ show n ++ " "

test [] = []
test ints =
  let n = head ints
    in ((show (1-n)) ++ " " ++ (show n) ++ "\n") : (test $ tail ints)
main = do
  ip <- getContents
  let ints = map read $ tail $ words ip
    in putStr $ concat $ test ints

test [] = ""
test ints =
  let n = head ints
    in (show (1-n)) ++ " " ++ (show n) ++ "\n" ++ (test $ tail ints)
main = do
  ip <- getContents
  let ints = map read $ tail $ words ip
    in putStr $ test ints

test [] = do putStr ""
test ints = do
  let n = head ints
  putStr $ show (1-n)
  putStr " "
  print n
  test $ tail ints
main = do
  ip <- getContents
  let ints = map read $ tail $ words ip
    in test ints

test [] = putStrLn ""
test ints = do
  let n = head ints
    in putStrLn ((show (1-n)) ++ " " ++ (show n))
  test $ tail ints
main = do
  ip <- getContents
  let ints = map read $ tail $ words ip
    in test ints

{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -O3 #-}
import           Control.Arrow             ((>>>))
import           Control.Monad             as M (guard, liftM, liftM3,
                                                 replicateM, replicateM_, when)
import qualified Control.Monad.ST          as ST
import qualified Control.Monad.Trans       as MS
import           Control.Monad.Trans.Maybe (MaybeT (runMaybeT))
import qualified Data.Array                as A (elems)
import qualified Data.Array.IArray         as IA
import qualified Data.Array.MArray         as MA
import qualified Data.Array.ST             as STA
import qualified Data.Array.Unboxed        as UA
import qualified Data.ByteString.Char8     as B8
import qualified Data.Foldable             as F
import           Data.Functor              ((<&>))
import           Data.Int                  (Int64)
import           Data.Ix                   (Ix (range))
import           Data.List                 (sort)
import           Data.Maybe                (fromJust, isJust)
import qualified Data.Traversable          as T
import           Data.Word                 (Word32)
import           Debug.Trace               as D
import           System.IO
import           Text.Printf               (printf)

readInt :: B8.ByteString -> Int
readInt = B8.readInt >>> fromJust >>> fst

readInt64 :: B8.ByteString -> Int64
readInt64 = B8.readInteger >>> fromJust >>> fst >>> fromInteger

updateArray :: (MA.MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
updateArray arr i func =
    MA.readArray arr i >>=
    MA.writeArray arr i <$> func

main :: IO ()
main = do
    test <- B8.getLine <&> readInt
    replicateM_ test $ do
        n <- B8.getLine <&> readInt64
        printf "%lld %lld\n" (-n + 1) n

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative

type Input =  Integer
data Output = Output Integer Integer

solve :: Input -> Output
solve ( x) = Output (-x+1) x

output :: Output -> Bu.Builder
output (Output a b) = Bu.integerDec a <> Bu.string7 " " <> Bu.integerDec b <>  Bu.string7 "\n"

tcio :: StateT [B8.ByteString] Identity Bu.Builder
tcio = do
  ~[n] <- getIntegrals 1
  return $ output $ solve n

main :: IO ()
main = do
  raw <- B8.getContents
  let bytestrings = B8.words raw
  let output = flip evalState bytestrings $ do
        ~[nTc] <- getIntegrals 1
        let answers = replicateM nTc tcio
        fmap mconcat answers
  B8.putStr $ Bu.toLazyByteString output

getNext :: [B8.ByteString] -> (B8.ByteString, [B8.ByteString])
getNext [] =  (B8.take 0 (B8.pack "."),[])
getNext (w:ws) =  (w,ws)

getIntegral :: Num t => B8.ByteString -> t
getIntegral x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> StateT [B8.ByteString] Identity [t]
getIntegrals n = replicateM n $ getIntegral <$> state getNext `const` B8.putStrLn

import Control.Monad
import Data.List
import Data.Int

calc :: Int64 -> [Int64]
calc n = [-n+1,n]

main = do
  line <- getLine
  let t = read line :: Int
  forM_ [1..t] $ \_ -> do
    line <- getLine
    let n = read line :: Int64
    putStrLn $ intercalate " " $ map show $ calc n

{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -O3 #-}
import           Control.Arrow             ((>>>))
import           Control.Monad             as M (guard, liftM, liftM3,
                                                 replicateM, when, replicateM_)
import qualified Control.Monad.ST          as ST
import qualified Control.Monad.Trans       as MS
import           Control.Monad.Trans.Maybe (MaybeT (runMaybeT))
import qualified Data.Array                as A (elems)
import qualified Data.Array.IArray         as IA
import qualified Data.Array.MArray         as MA
import qualified Data.Array.ST             as STA
import qualified Data.Array.Unboxed        as UA
import qualified Data.ByteString.Char8     as B8
import qualified Data.Foldable             as F
import           Data.Functor              ((<&>))
import           Data.Int                  (Int64)
import           Data.Ix                   (Ix (range))
import           Data.List                 (sort)
import           Data.Maybe                (fromJust, isJust)
import qualified Data.Traversable          as T
import           Data.Word                 (Word32)
import           Debug.Trace               as D
import           System.IO
import           Text.Printf               (printf)

readInt :: B8.ByteString -> Int
readInt = B8.readInt >>> fromJust >>> fst

readInt64 :: B8.ByteString -> Int64
readInt64 = B8.readInteger >>> fromJust >>> fst >>> fromInteger

updateArray :: (MA.MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
updateArray arr i func =
    MA.readArray arr i >>=
    MA.writeArray arr i <$> func

main :: IO ()
main = do
    hSetBuffering stdout NoBuffering

    test <- B8.getLine <&> readInt
    replicateM_ test $ do
        n <- B8.getLine <&> readInt64
        printf "%lld %lld\n" (n - 1) n

--
-- Michael V. Antosha
-- 2021
-- Michael.Antosha@gmail.com
--

{-# language NoMonomorphismRestriction #-}

{-# options_ghc -Weverything                     #-}

{-# options_ghc -Wno-safe                        #-}     -- no warning if inferred as safe
{-# options_ghc -Wno-all-missed-specialisations  #-}     -- "Could not specialise..." when -O1
{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

-- (ghc -Werror -O1 -rtsopts -o a.out *.hs  &&  set -o pipefail  &&  cat -- input.txt | ./a.out > temp.txt  &&  diff -sdu -- output.txt temp.txt)  &&  echo OKAY  ||  echo ERROR

import Prelude
import Control.Monad

main :: IO ()
main = do
    t <- readLn
    tcs <- replicateM t readLn  :: IO [Integer]

    (putStr . unlines . map solve) tcs

----------------------------------------------------------------------

solve n = show n ++ " " ++ show n


{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}

import Data.List

import Control.Monad
import Control.Applicative

import Numeric

data Cartesian = Cartesian !Int !Int deriving (Eq, Ord)
data Polar = Polar !Int !Double deriving (Eq, Ord)

polar :: Cartesian -> Polar
polar (Cartesian x y) = Polar (x^2 + y^2) (atan2 (fromIntegral y) (fromIntegral x))

fix :: Double -> Double
fix = until (> -pi) (+ 2*pi) . until (<= pi) (subtract $ 2*pi)

(~>) :: Cartesian -> Cartesian -> Cartesian
(Cartesian x0 y0) ~> (Cartesian x1 y1) = Cartesian (x1 - x0) (y1 - y0)

solve :: [Cartesian]  -> Int
solve [] = 0
solve xs0 = maximum $ map solve' xs0
    where
        solve' x = go 0.0 0.0 xs
            where
                (Polar _ α):xs = sortBy (flip compare) $ map (polar . (x ~>)) xs0

                go _ _ [] = 0
                go minA maxA (Polar r2 β:xs') =
                    let γ = fix $ β - α
                        minA' = min minA γ
                        maxA' = max maxA γ
                    in if maxA' - minA' >= pi / 3 then r2 else go minA' maxA' xs'

main :: IO ()
main = do
    n <- readLn
    xs <- replicateM n $ do { [x, y] <- map read . words <$> getLine; return $ Cartesian x y }
    putStrLn $ showFFloat Nothing (sqrt (fromIntegral $ solve xs) / 2) ""

{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}

import Data.List

import Control.Monad
import Control.Applicative

import Numeric

data C = C !Int !Int deriving (Eq, Ord)
data P = P !Int !Double deriving (Eq, Ord)

polar :: C -> P
polar (C x y) = P (x^2 + y^2) (atan2 (fromIntegral y) (fromIntegral x))

fix :: Double -> Double
fix = until (> -pi) (+ 2*pi) . until (<= pi) (subtract $ 2*pi)

(~>) :: C -> C -> C
C x0 y0 ~> C x1 y1 = C (x1 - x0) (y1 - y0)

solve :: [C]  -> Int
solve [] = 0
solve xs0 = maximum $ map solve' xs0
    where
        solve' x = go 0.0 0.0 xs
            where
                P _ α:xs = sortBy (flip compare) $ map (polar . (x ~>)) xs0

                go _ _ [] = 0
                go minA maxA (P r2 β:xs') =
                    let γ = fix $ β - α
                        minA' = min minA γ
                        maxA' = max maxA γ
                    in if maxA' - minA' >= pi / 3 then r2 else go minA' maxA' xs'

main :: IO ()
main = do
    n <- readLn
    xs <- replicateM n $ do { [x, y] <- map read . words <$> getLine; return $ C x y }
    putStrLn $ showFFloat Nothing (sqrt (fromIntegral $ solve xs) / 2) ""

{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Data.List

import Control.Monad
import Control.Applicative

import Numeric

data C = C !Int !Int deriving (Eq, Ord)
data P = P !Int !Double deriving (Eq, Ord)

polar :: C -> P
polar (C x y) = P (x^2 + y^2) (atan2 (fromIntegral y) (fromIntegral x))

fix :: Double -> Double
fix = until (> -pi) (+ 2*pi) . until (<= pi) (subtract $ 2*pi)

(~>) :: C -> C -> C
C x0 y0 ~> C x1 y1 = C (x1 - x0) (y1 - y0)

solve :: [C]  -> Int
solve [] = 0
solve xs0 = maximum $ map solve' xs0
    where
        solve' x = go 0.0 0.0 xs
            where
                P _ α:xs = sortBy (flip compare) $ map (polar . (x ~>)) xs0

                go _ _ [] = 0
                go minA maxA (P r2 β:xs') =
                    let γ = fix $ β - α
                        minA' = min minA γ
                        maxA' = max maxA γ
                    in if maxA' - minA' >= pi / 3 then r2 else go minA' maxA' xs'

main :: IO ()
main = do
    n <- readLn
    xs <- replicateM n $ do { [x, y] <- map read . words <$> getLine; return $ C x y }
    putStrLn $ showFFloat Nothing (sqrt (fromIntegral $ solve xs) / 2) ""


{-# LANGUAGE OverloadedStrings, FunctionalDependencies, FlexibleContexts,
    ConstraintKinds, BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Array
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Char
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid
import Data.Ratio
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable
import System.IO

------

type Hopper r = State [r]

type HopperT r = StateT [r]

runHopper :: Hopper r a -> [r] -> (a, [r])
runHopper = runState

evalHopper :: Hopper r a -> [r] -> a
evalHopper = evalState

runHopperT :: HopperT r m a -> [r] -> m (a, [r])
runHopperT = runStateT

evalHopperT :: Monad m => HopperT r m a -> [r] -> m a
evalHopperT = evalStateT

type MonadHopper r m = MonadState [r] m

pop :: MonadHopper r m => m (Maybe r)
pop = state f
  where
    f :: [r] -> (Maybe r, [r])
    f [] = (Nothing, [])
    f (q:qs) = (Just q, qs)

popJust :: MonadHopper r m => m r
popJust = fromJust <$> pop

------

popInt :: MonadHopper B.ByteString m => m Int
popInt = fst . fromJust . B.readInt <$> popJust

popInts :: MonadHopper B.ByteString m => m [Int]
popInts = map (fst . fromJust . B.readInt) . B.words <$> popJust

popInteger :: MonadHopper B.ByteString m => m Integer
popInteger = fst . fromJust . B.readInteger <$> popJust

popIntegers :: MonadHopper B.ByteString m => m [Integer]
popIntegers = map (fst . fromJust . B.readInteger) . B.words <$> popJust

------

dropCR :: B.ByteString -> B.ByteString
dropCR s | not (B.null s) && B.last s == '\r' = B.init s
         | otherwise = s

bshow :: Show a => a -> B.ByteString
bshow = B.pack . show

main = B.interact go
  where
    go :: B.ByteString -> B.ByteString
    go = evalHopper run . map dropCR . B.lines

------

run :: Hopper B.ByteString B.ByteString
run = do
  t <- popInt
  B.unlines <$> replicateM t run1

run1 = bshow . solve <$> popInt

solve n | cs ! 0 > n = 0
        | cs ! (sz - 1) <= n = sz
        | otherwise = go 0 (sz - 1)
  where
    go lb ub | ub - lb == 1 = ub
             | otherwise = let mid = (lb + ub) `quot` 2 in
                             case cs ! mid <= n of
                               True -> go mid ub
                               False -> go lb mid

cs = let l = takeWhile (\c -> c <= 10 ^ 9) $
             map (\k -> (2 * k + 1) ^ 2 `quot` 2 + 1) [1..] in
       listArray (0, length l - 1) l

sz = snd (bounds cs) + 1

import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as C

getInts :: IO [Int]
getInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine

main :: IO ()
main = do
  [t] <- getInts
  replicateM_ t $ do
    [n] <- getInts
    C.putStrLn $ C.pack $ show $ floor $ 0.5 * (sqrt (2.0 * fromIntegral n - 1) - 1) + 1e-10

-- https://codeforces.com/problemset/problem/1487/D

module Main where

import qualified Data.Map as M

main :: IO ()
main = getInt >>= \numTestCase -> driver numTestCase

driver :: Int -> IO ()
driver 0 = return ()
driver n =
  getInt >>= \x ->
    let ans = solve x
     in print ans >> driver (n - 1)

getInt :: IO Int
getInt = getLine >>= \x -> return (readInt x)

readInt :: String -> Int
readInt x = read x :: Int

perfectSquares :: Int -> [Int]
perfectSquares n = map (^ 2) $ takeWhile (<= n) [1 ..]

solve n = length as
  where
    as = takeWhile lessThan [3, 5 ..]
    lessThan x = ((x ^ 2) + 1) `div` 2 <= n

import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Builder as B
import System.IO

import Control.Monad
import Control.Monad.Trans.State
import Control.Monad.Trans.Class
import Data.List
import Data.Semigroup

type SIO = StateT P.ByteString IO

intSqrt :: Int -> Int
intSqrt x = let
  w = round (sqrt $ fromIntegral x :: Double)
  in  if  w*w > x  then  w-1  else  w

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [t] <- readInts
  replicateM_ t $ do
    [n] <- readInts
    let
      w = intSqrt (2*n - 1)
    putInts [div (w-1) 2]

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

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

putInts :: [Int] -> SIO ()
putInts li = let
  outBuilder = foldr (<>) (B.char7 '\n')
    $ intersperse (B.char7 ' ') (map B.intDec li)
  in  lift $ B.hPutBuilder stdout outBuilder

{-# LANGUAGE OverloadedStrings, FunctionalDependencies, FlexibleContexts,
    ConstraintKinds, BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Array
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Char
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid
import Data.Ratio
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable
import System.IO

------

type Hopper r = State [r]

type HopperT r = StateT [r]

runHopper :: Hopper r a -> [r] -> (a, [r])
runHopper = runState

evalHopper :: Hopper r a -> [r] -> a
evalHopper = evalState

runHopperT :: HopperT r m a -> [r] -> m (a, [r])
runHopperT = runStateT

evalHopperT :: Monad m => HopperT r m a -> [r] -> m a
evalHopperT = evalStateT

type MonadHopper r m = MonadState [r] m

pop :: MonadHopper r m => m (Maybe r)
pop = state f
  where
    f :: [r] -> (Maybe r, [r])
    f [] = (Nothing, [])
    f (q:qs) = (Just q, qs)

popJust :: MonadHopper r m => m r
popJust = fromJust <$> pop

------

popInt :: MonadHopper B.ByteString m => m Int
popInt = fst . fromJust . B.readInt <$> popJust

popInts :: MonadHopper B.ByteString m => m [Int]
popInts = map (fst . fromJust . B.readInt) . B.words <$> popJust

popInteger :: MonadHopper B.ByteString m => m Integer
popInteger = fst . fromJust . B.readInteger <$> popJust

popIntegers :: MonadHopper B.ByteString m => m [Integer]
popIntegers = map (fst . fromJust . B.readInteger) . B.words <$> popJust

------

dropCR :: B.ByteString -> B.ByteString
dropCR s | not (B.null s) && B.last s == '\r' = B.init s
         | otherwise = s

bshow :: Show a => a -> B.ByteString
bshow = B.pack . show

main = B.interact go
  where
    go :: B.ByteString -> B.ByteString
    go = evalHopper run . map dropCR . B.lines

------

run :: Hopper B.ByteString B.ByteString
run = do
  t <- popInt
  B.unlines <$> replicateM t run1

run1 = bshow . solve <$> popInt

solve n = length $ takeWhile (\(a, b, c) -> c <= n) triples

triples = map f [3, 5..]
  where
    f a = let b = a ^ 2 `quot` 2 in
            (a, b, b + 1)

import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as C

getInts :: IO [Int]
getInts = map (fst . fromJust . C.readInt) . C.words <$> C.getLine

main :: IO ()
main = do
  [t] <- getInts
  replicateM_ t $ do
    [n] <- getInts
    C.putStrLn $ C.pack $ show $ floor $ 0.5 * (sqrt (2.0 * fromIntegral n + 1) - 1) + 1e-10


import Control.Monad
import Data.List

main = do
	getLine
	a <- getLine
	b <- getLine
	let c = zip a b
	let cnt00 = fromIntegral . length $ elemIndices ('0', '0') c
	let cnt01 = fromIntegral . length $ elemIndices ('0', '1') c
	let cnt10 = fromIntegral . length $ elemIndices ('1', '0') c
	let cnt11 = fromIntegral . length $ elemIndices ('1', '1') c
	print $ cnt00 * cnt10 + cnt00 * cnt11 + cnt01 * cnt10

{-# LANGUAGE ExistentialQuantification #-}
module Main where
import Control.Applicative
import Data.Int

default (Int64)

main = interact exec
exec = show . (\[xs, ys] -> let (n0, n1) = runFold fc xs; in (\(n0', n1') -> n0' * n1 + n1' * n0 - n0' * n1') $ runFold fc $ map fst $ filter ((== '0') . snd) $ zip xs ys ) . tail . words
  where fc = liftA2 (,) (fcount '0') (fcount '1')

data Fold a b = forall x. Fold (x -> a -> x) x (x -> b)
data Pair a b = Pair !a !b
instance Functor (Fold a) where
  fmap f (Fold step begin done) = Fold step begin (f . done)
instance Applicative (Fold a) where
    pure b    = Fold (\() _ -> ()) () (\() -> b)
    (Fold stepL beginL doneL) <*> (Fold stepR beginR doneR) =
        let step (Pair xL xR) a = Pair (stepL xL a) (stepR xR a)
            begin = Pair beginL beginR
            done (Pair xL xR) = doneL xL (doneR xR)
        in  Fold step begin done
runFold (Fold step begin done) as = foldr (\a k x -> k $! step x a) done as begin

fcount a = Fold (\c x -> c + fromIntegral (fromEnum $ x == a)) 0 id
fsum = Fold (+) 0 id
flength = Fold (flip (const succ)) 0 id

import Control.Monad
import Data.List

main = do
	getLine
	a <- getLine
	b <- getLine
	let c = zip a b
	let cnt00 = length $ elemIndices ('0', '0') c
	let cnt01 = length $ elemIndices ('0', '1') c
	let cnt10 = length $ elemIndices ('1', '0') c
	let cnt11 = length $ elemIndices ('1', '1') c
	print $ cnt00 * cnt10 + cnt00 * cnt11 + cnt01 * cnt10

{-# LANGUAGE ExistentialQuantification #-}
module Main where
import Control.Applicative

default (Int)

main = interact exec
exec = show . (\[xs, ys] -> let (n0, n1) = runFold fc xs; in (\(n0', n1') -> n0' * n1 + n1' * n0 - n0' * n1') $ runFold fc $ map fst $ filter ((== '0') . snd) $ zip xs ys ) . tail . words
  where fc = liftA2 (,) (fcount '0') (fcount '1')

data Fold a b = forall x. Fold (x -> a -> x) x (x -> b)
data Pair a b = Pair !a !b
instance Functor (Fold a) where
  fmap f (Fold step begin done) = Fold step begin (f . done)
instance Applicative (Fold a) where
    pure b    = Fold (\() _ -> ()) () (\() -> b)
    (Fold stepL beginL doneL) <*> (Fold stepR beginR doneR) =
        let step (Pair xL xR) a = Pair (stepL xL a) (stepR xR a)
            begin = Pair beginL beginR
            done (Pair xL xR) = doneL xL (doneR xR)
        in  Fold step begin done
runFold (Fold step begin done) as = foldr (\a k x -> k $! step x a) done as begin

fcount a = Fold (\c x -> c + fromEnum (x == a)) 0 id
fsum = Fold (+) 0 id
flength = Fold (flip (const succ)) 0 id

{-# LANGUAGE ExistentialQuantification #-}
module Main where
import Control.Applicative

default (Int)

main = interact exec
exec = show . (\[xs, ys] -> let (nz, no) = runFold (liftA2 (,) (fcount '0') (fcount '1')) xs; ff = Fold (\c x -> c + if x == '0' then no else nz) 0 id; in runFold (liftA2 (-) ff (fcount '0')) $ map fst $ filter ((== '0') . snd) $ zip xs ys ) . tail . words

data Fold a b = forall x. Fold (x -> a -> x) x (x -> b)
data Pair a b = Pair !a !b
instance Functor (Fold a) where
  fmap f (Fold step begin done) = Fold step begin (f . done)
instance Applicative (Fold a) where
    pure b    = Fold (\() _ -> ()) () (\() -> b)
    (Fold stepL beginL doneL) <*> (Fold stepR beginR doneR) =
        let step (Pair xL xR) a = Pair (stepL xL a) (stepR xR a)
            begin = Pair beginL beginR
            done (Pair xL xR) = doneL xL (doneR xR)
        in  Fold step begin done
runFold (Fold step begin done) as = foldr (\a k x -> k $! step x a) done as begin

fcount a = Fold (\c x -> c + fromEnum (x == a)) 0 id
fsum = Fold (+) 0 id
flength = Fold (flip (const succ)) 0 id


import Control.Monad
main = do
  q <- readLn
  replicateM_ q $ do
    [n,k] <- map read . words <$> getLine
    s <- getLine
    let f i = minimum $ zipWith (-) =<< drop k $ scanl (+) 0 $ map fromEnum $ zipWith (/=) s $ drop i $ cycle "RGB"
    print $ minimum $ map f [0..2]


module Main where
import Prelude hiding (reads)
parse x = (takeWhile (/=' ') x) : if (null rest) then [] else parse (tail $ (dropWhile (/=' ')) x) where
    rest = dropWhile (/=' ') x
read' :: String -> Int
read' = read

reads :: Int -> IO [String]
reads 0 = return []
reads k = do
    x <- getLine
    xs <- reads $ k - 1
    return (x:xs)

arithm [] = 0
arithm [_] = 1
arithm (x:y:z) = if y-x==1 then 1 + arithm (y:z) else 1

main = do
    s <- getLine
    let [n,k] = map read' $ parse s
    ranges <- reads k
    let lists = map (tail . map read' . parse) ranges
    let [st] = filter ((==1) . head) lists
    let rest = filter ((/=1) . head) lists
    print $ (length st - arithm st) + sum (map (\x -> length x - 1) rest) + (n - arithm st)

import Control.Applicative

count [] _ = 0
count (l:ls) x
    | l == x = 1 + count ls (x+1)
    | otherwise = 0

main = do
    m <- read . head . tail . words <$> getLine :: IO Int
    l <- map (map read . words) . lines <$> getContents :: IO [[Int]]
    let s = sum $ do
        x <- tail <$> l
        let c = count x 1 `max` 1
        return $ length x - c + 1
    print $ (s-m) + (s-1)

start x (y:ys) = if y - x == 1 then 1 + start y ys else 0
start _ _ = 0
solve ([n, k]:xs) = 2 * n - k + 1 - 2 * (sum . map (start 0 . tail) $ xs)
main = interact $ show . solve . map (map (read :: String -> Int) . words) . lines

{-# 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.List
import Data.Int
import Data.Bits
import Data.Maybe
import Data.Monoid
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

{-
1 3 7 -> 2 + 2
2 5 -> 1 + 2
4 6 -> 1 + 2
-}

main = do
    (n,k) <- readIntPair
    as <- map (map readInt . B.words) . B.lines <$> B.getContents
    let ms = map tail as
    let m0:ms' = sortBy (\x y -> compare (head x) (head y)) ms
    let go x [] = 0
        go x (y:ys) | x == y - 1 = go y ys
                    | otherwise = 2 * length (y:ys)
    print $ go (head m0) (tail m0) + sum (map ((\x -> 2*x-1). length) ms')

import Data.List
import Control.Applicative
import Debug.Trace

solve :: Int -> [[Int]] -> Int
solve n chains =
  2 * (n - k) - (c - 1)
  where
    c = length chains
    k = length $ head $ group $ zipWith (\a -> \b -> a == b) [1..] $ head $ filter (\x -> head x == 1) $ chains

main = do
  [n, _] <- map read . words <$> getLine
  chains <- map (map read . tail . words) . lines <$> getContents
  putStrLn $ show $ solve n chains

module Main where

import Data.List
import Data.Bool
import Data.Maybe
import Control.Applicative
import Control.Monad

rl :: Read a => IO [a]
rl = fmap read . words <$> getLine

red i [] = []
red i (x:xs) | i == x = red (i+1) xs
             | otherwise = (x:xs)

main :: IO ()
main = do
  [n, k] <- rl :: IO [Int]
  fmap ((+1).sum) (forM [1..k] (const (fmap ((subtract 1).(* 2).length.red 1.tail) rl)))
    >>= print
  pure ()

{-# 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.List
import Data.Int
import Data.Bits
import Data.Maybe
import Data.Monoid
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 Matryoshka = Mat { top  :: Int , chain :: [Int] } deriving Show

instance Eq Matryoshka where
    a == b = top a == top b

instance Ord Matryoshka where
    compare a b = compare (top a) (top b)

main = do
    (n,k) <- readIntPair
    as <- map (map readInt . B.words) . B.lines <$> B.getContents
    let ms = [ Mat (head l) (tail l)  | _:l <- as ]
    let go s | S.null s = 0
             | otherwise =
                let (m,s') = S.deleteFindMin s in
                let sub _ [] = if S.null s' then 0 else 1 +go s'
                    sub x (y:ys) | y == x + 1 = sub y ys
                    sub x ys =
                        let ns = map (\x -> Mat x []) ys in
                        1 + length ys + go (foldr S.insert s' ns)
                in sub (top m) (chain m)
    print $ go $ S.fromList ms

import Data.List
import Control.Applicative

solve :: Int -> [[Int]] -> Int
solve n chains =
  2 * (n - lsum) + (length chains) - 1
  where
    lsum = sum $ map l chains
    l (a:rst) = length $ head $ group $ zipWith (\a -> \b -> a == b) (a:rst) [a..]
    l [] = 0

main = do
  [n, _] <- map read . words <$> getLine
  chains <- map (map read . tail . words) . lines <$> getContents
  putStrLn $ show $ solve n chains

start x (y:ys) = if x == y then 1 + start (x + 1) ys else 0
start _ [] = 0
solve ([n, k]:xs) = 2 * n - k - 1 - 2 * (sum . map (start 1) . tail $ xs)
main = interact $ show . solve . map (map (read :: String -> Int) . words) . lines

start x (y:ys) = if y - x == 1 then 1 + start y ys else 0
start _ _ = 0
solve ([n, k]:xs) = 2 * n - k - 1 - 2 * (sum . map (start 0 . tail) $ xs)
main = interact $ show . solve . map (map (read :: String -> Int) . words) . lines

start x (y:ys) = if x == y then 1 + start y ys else 0
start _ [] = 0
solve ([n, k]:xs) = 2 * n - k - 1 - 2 * (sum . map (start 1) . tail $ xs)
main = interact $ show . solve . map (map (read :: String -> Int) . words) . lines