{-# LANGUAGE TupleSections, MultiParamTypeClasses, ExistentialQuantification,
             RankNTypes, FlexibleContexts, DeriveFunctor #-}

import Control.Monad
import Control.Applicative
--import Control.Lens
--import Control.Zipper
import Control.Arrow
import qualified Data.Map as M
--import qualified Data.Vector as V
import qualified Data.Set as S
import Data.List
--import Debug.Trace
--import Control.Comonad
import Data.Monoid
--import Data.Functor.Foldable
--import Test.QuickCheck
import Data.Tree
--import Data.Machine
--import Data.Traversable
--import Data.Distributive
--import Linear
import Control.Monad.Cont

main = do
  [d, h, v, e] <- fmap (map read . words) getLine
  maybe (putStrLn "NO") (\x -> putStrLn "YES" >> print x) (solve d h v e)

solve :: Double -> Double -> Double -> Double -> Maybe Double
solve d h v e | v - e' > epsilon = Just $ (h*pi *(d/2)**2)/(v - e')
              | otherwise = Nothing
              where
                e' = e*pi*(d/2)**2
                epsilon = 10**(-6)

solve::(Double, Double, Double, Double) -> String
solve (d,h,v,e) = do
		let a = e * d * d * pi / 4
		if v <= a then "NO" else "YES\n" ++ show ((h * d * d * pi/4)/(v - a))

main = getLine >>= (putStrLn . solve . getFour . map (\x -> read x :: Double) . words)
	where
		getFour (a : b : c : d : xz) = (a, b, c, d)

main = getLine >>= putStr . unlines . solve . map (read :: String -> Double) . words

solve [d, h, v, e] = if dh < 0
                     then ["YES", show (-h / dh)]
                     else ["NO"]
                         where dh = e - 4 * v / (pi * d * d)

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE OverloadedStrings #-}
-- {-# LANGUAGE BangPatterns #-}
-- {-# LANGUAGE ViewPatterns #-}
-- {-# LANGUAGE TupleSections #-}

import           System.IO hiding (char8)
import           Control.Applicative
import           Control.Monad
import           Data.List
import           Data.Tuple
import           Data.Int
import           Data.Char
import           Data.Function (on)
import           Data.Array
-- import           Data.UArray
-- import           Data.Array.IArray
-- import           Data.Array.ST
-- import           Data.Array.MArray
-- import           Data.Array.Unsafe
import           Data.Ix
import           Data.Maybe
import           Data.Monoid hiding ((<>))
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BL
import           Data.ByteString.Builder
-- import           Data.Vector.Unboxed ((//), (++), (!), (!?))
-- import qualified Data.Vector.Unboxed as U
-- import           Data.IntSet (IntSet)
-- import qualified Data.IntSet as S
-- import           Data.IntMap.Strict (IntMap)
-- import qualified Data.IntMap.Strict as M
-- import           Data.Sequence ((|>), (<|), (><),ViewR((:>)), ViewL((:<)))
-- import qualified Data.Sequence as S
-- import           Debug.Trace

main = do
  xs <- map fromIntegral . readIntN <$> BS.getLine
  printMaybe $ solve xs

solve :: [Double] -> Maybe Double
solve [d,h,v,e] = if 0 < ans && ans < 10000 then Just ans else Nothing where
  ans = h/(4*v/pi/d/d - e)

printMaybe (Just x) = do
  putStrLn "YES"
  print x
printMaybe Nothing = do
  putStrLn "NO"

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

readInt1 :: BS.ByteString -> Int
readInt1 = fst . fromJust . BS.readInt

readInt2 :: BS.ByteString -> (Int,Int)
readInt2 = toTuple . readIntN

readInt3 :: BS.ByteString -> (Int,Int,Int)
readInt3 = toTriple . readIntN

readIntN :: BS.ByteString -> [Int]
readIntN =  map readInt1 . BS.words

readInt641 :: BS.ByteString -> Int64
readInt641 = fromIntegral . fst . fromJust . BS.readInteger

readInt642 :: BS.ByteString -> (Int64,Int64)
readInt642 = toTuple . readInt64N

readInt643 :: BS.ByteString -> (Int64,Int64,Int64)
readInt643 = toTriple . readInt64N

readInt64N :: BS.ByteString -> [Int64]
readInt64N =  map readInt641 . BS.words

readInteger1 :: BS.ByteString -> Integer
readInteger1 = fst . fromJust . BS.readInteger

readInteger2 :: BS.ByteString -> (Integer,Integer)
readInteger2 = toTuple . readIntegerN

readInteger3 :: BS.ByteString -> (Integer,Integer,Integer)
readInteger3 = toTriple . readIntegerN

readIntegerN :: BS.ByteString -> [Integer]
readIntegerN =  map readInteger1 . BS.words

toTuple :: [a] -> (a, a)
toTuple [x, y] = (x, y)

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

fromTuple :: (a, a) -> [a]
fromTuple (x, y) = [x, y]

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

-- if not applying, use "const"

applyTuple :: (a -> a') -> (b -> b') -> (a, b) -> (a', b')
applyTuple f g (x, y) = (f x, g y)

applyTriple :: (a -> a') -> (b -> b') -> (c -> c') -> (a, b, c) -> (a', b', c')
applyTriple f g h (x, y, z) = (f x, g y, h z)

main = do
	input <- getLine
	let [d, h, v, e] = map (read::String->Double) $ words input
	let	ans = h/(4*v/pi/d^2-e)
	if (ans > 0) && (ans /= 1/0)
		then putStrLn $ ("YES\n" ++) $ show ans
		else putStrLn "NO"

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Array
import qualified Data.IntMap as I
import qualified Data.Map as M

main=do
	 [d,h,v,e]<- map read.words <$> getLine::IO [Double]
	 if pi/4.0 *d*d *e >v then putStrLn "NO"
			      else do
					putStrLn "YES"
					print $  (pi/4.0 *d*d *h) / (v-pi/4.0 *d*d *e)

main = do
  [d, h, v, e] <- getLine >>= return. map read. words
  let ans = a * h / (v - e * a) where a = (d / 2)^2 * pi
  putStrLn $ if ans < 0 then "NO" else "YES"
  putStr $ if ans < 0 then [] else show ans ++ "\n"

main = do
    s <- getLine
    let [d, h, v, e] = map read . words $ s
        v' = v / (3.1415926 * d * d / 4)
        res = h / (v' - e)
    if v' <= e
    then putStrLn "NO"
    else putStrLn "YES" >> print res

import Data.Bits

-- import Data.List (List)
import qualified Data.List as List
import Data.Map (Map)
import qualified Data.Map as Map
import System.IO
import Text.Read

parseLine :: IO [Double]
parseLine = do
  input <- getLine
  return ((map read . words) input :: [Double])

solveN 0 ans = return ans
solveN n ans = do
  ls <- parseLine
  let [d, h, v, e] = ls
  solveN 0 (pi * d ^ 2 * h / (4 * v - pi * d ^ 2 * e))

main :: IO ()
main
  -- input <- getLine
  -- let (n:k:_) = (map read . words) input :: [Int]
  -- p <- parseLine
 = do
  input <- solveN 1 0.0
  if input >= 0
    then do
      putStrLn "YES"
      print input
    else putStrLn "NO"

{-
instructions go here...
-}

module Main where
import Data.List (group, sort)

solve :: Float -> Float -> Float -> Float -> Maybe Float
solve d h v e = if vPlus >= vMinus
                  then Nothing
                  else Just $ vInit / (vMinus - vPlus)
  where vPlus  = e * pi * (d/2)^2
        vMinus = v
        vInit = pi * (d/2)^2 * h

main :: IO ()
main = do
  line <- getLine
  let [d,h,v,e] = map read (words line) :: [Float]
  let d' = d / 100 -- cm to m
      h' = h / 100 -- cm to m
      v' = v / (1000*1000) -- ml/s to m3/s
      e' = e / 100 -- cm/s to m/s
  case solve d' h' v' e' of
               Nothing -> putStrLn "NO"
               Just t  -> do
                 putStrLn "YES"
                 putStrLn (show t)

module Main where

main :: IO ()
main = do
	[d, h, v, e] <- getRs
	case res d h v e of
		Nothing -> putStrLn "NO"
		Just t -> do
                    putStrLn "YES"
                    print t

getRs :: IO [Float]
getRs = do
	s <- getLine
	return $ map read $ words s

area :: (Ord a, Fractional a, Floating a) => a -> a
area d = let r = d / 2
         in r^2 * pi

res :: (Ord a, Fractional a, Floating a) => a -> a -> a -> a -> Maybe a
res d h v e = let a = area d
              in resA h v e a

resA :: (Ord a, Fractional a, Floating a) => a -> a -> a -> a -> Maybe a
resA h v e a = let discr = v - e * a
               in if discr > 0 then Just ( h * a / discr) else Nothing

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

solve :: [Double] -> Maybe Double
solve [d, h, v, e] | pi / 4.0 * d * d * e > v = Nothing
                   | otherwise = Just ((pi / 4.0 * d * d * h) / (v - pi / 4.0 * d * d * e))

output :: Maybe Double -> IO()
output Nothing = putStrLn "NO"
output (Just x) = putStrLn $ "YES\n" ++ show x

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns #-}
{-# 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.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 (Map)
import qualified Data.Map as Map
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
  [d, h, v, e] <- fmap (map read . words) getLine

  let x = h*pi*d^2/4 / (v - e*pi*d^2/4) :: Double

  if x < 0
     then putStrLn "NO"
     else do
       putStrLn "YES"
       print x

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

solve :: [Double] -> Maybe Double
solve [d, h, v, e] | pi / 4.0 * d * d * e > v = Nothing
                   | otherwise = Just ((pi / 4.0 * d * d * h) / (v - pi / 4.0 * d * d * e))

output :: Maybe Double -> IO()
output Nothing = putStrLn "No"
output (Just x) = putStrLn $ "Yes\n" ++ show x

{-# LANGUAGE TupleSections, MultiParamTypeClasses, ExistentialQuantification,
             RankNTypes, FlexibleContexts, DeriveFunctor #-}

import Control.Monad
import Control.Applicative
--import Control.Lens
--import Control.Zipper
import Control.Arrow
import qualified Data.Map as M
--import qualified Data.Vector as V
import qualified Data.Set as S
import Data.List
--import Debug.Trace
--import Control.Comonad
import Data.Monoid
--import Data.Functor.Foldable
--import Test.QuickCheck
import Data.Tree
--import Data.Machine
--import Data.Traversable
--import Data.Distributive
--import Linear
import Control.Monad.Cont

main = do
  [d, h, v, e] <- fmap (map read . words) getLine
  maybe (putStrLn "NO") (\x -> putStrLn "YES" >> print x) (solve d h v e)

solve :: Double -> Double -> Double -> Double -> Maybe Double
solve d h v e | abs (v - e') > epsilon = Just $ (h*pi *(d/2)**2)/(v - e')
              | otherwise = Nothing
              where
                e' = e*pi*(d/2)**2
                epsilon = 10**(-7)

{-# LANGUAGE TupleSections, MultiParamTypeClasses, ExistentialQuantification,
             RankNTypes, FlexibleContexts, DeriveFunctor #-}

import Control.Monad
import Control.Applicative
--import Control.Lens
--import Control.Zipper
import Control.Arrow
import qualified Data.Map as M
--import qualified Data.Vector as V
import qualified Data.Set as S
import Data.List
--import Debug.Trace
--import Control.Comonad
import Data.Monoid
--import Data.Functor.Foldable
--import Test.QuickCheck
import Data.Tree
--import Data.Machine
--import Data.Traversable
--import Data.Distributive
--import Linear
import Control.Monad.Cont

main = do
  [d, h, v, e] <- fmap (map read . words) getLine
  maybe (putStrLn "NO") (\x -> putStrLn "YES" >> print x) (solve d h v e)

solve :: Double -> Double -> Double -> Double -> Maybe Double
solve d h v e | v > e' = Just $ (pi *(d/2)**2)/(v - e')
              | otherwise = Nothing
              where
                e' = e*pi*(d/2)**2

solve::(Double, Double, Double, Double) -> String
solve (d,h,v,e) = do
		let a = e * d * pi / 4
		if v <= a then "NO" else "YES\n" ++ show ((h * d * pi/4)/(v - a))

main = getLine >>= (putStrLn . solve . getFour . map (\x -> read x :: Double) . words)
	where
		getFour (a : b : c : d : xz) = (a, b, c, d)

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Array
import qualified Data.IntMap as I
import qualified Data.Map as M

main=do
	 [d,h,v,e]<- map read.words <$> getLine::IO [Double]
	 if 3.14/4.0 *d*d *e >v then putStrLn "NO"
			      else do
					putStrLn "YES"
					print $  (3.14/4.0 *d*d *h) / (v-3.14/4.0 *d*d *e)


main = interact $ unlines . map (show . solve) . triplets . map read . tail . words
triplets (a:b:c:xs) = (a,b,c) : triplets xs
triplets [] = []
solve (l,r,d) | l > d = d
              | otherwise = (r + d) `div` d * d

main = getContents >>= putStr . unlines . map answer . tail . lines

answer = show . solve . map read . words

solve [l,r,d]
    | l > d = d
    | otherwise = ((r + d) `div` d) * d

-- Educational Codeforces Round #58 (A), Contest 1101, Problem set ?.

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function

main :: IO ()
main = do
  q <- read <$> getLine
  replicateM_ q $ do
    [l,r,d] <- map read . words <$> getLine
    print $ query l r d

query :: Int -> Int -> Int -> Int
query l r d
  | d < l     = d
  | otherwise = r + d - r `mod` d

import Text.Printf
import Data.List
import Data.Functor
import Control.Applicative
import Prelude
import Control.Monad
import Data.Maybe
import Data.Function
import Data.Array

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

solve :: Int -> Int -> Int -> Int
solve l r d
    | d < l     = d
    | otherwise = ((r `div` d) + 1) * d

main :: IO ()
main = do
    q <- readInt <$> getLine
    replicateM_ q $ do
        [l, r, d] <- (map readInt . words) <$> getLine
        printf "%d\n" $ solve l r d

main = do
    s <- getContents
    printSolution $ solve $ parse s

parse :: String -> [(Integer, Integer, Integer)]
parse s = f $ tail $ words s

f (l:r:d:v) = (read l, read r, read d):(f v)
f _ = []

solve :: [(Integer, Integer, Integer)] -> [Integer]
solve xs = map solveQuery xs

solveQuery (l, r, d) = if d < l then d else ((div r d) + 1) * d

printSolution (x:xs) = do
    print x
    printSolution xs
printSolution [] = return ()

{-# 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.ByteString.Lazy.Builder
import Data.ByteString.Lazy.Builder.ASCII
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 l r d
    | d < l = d
    | otherwise = r `div` d * d + d

main = do
    [n] <- getInts
    ans <- forM [1..n] $ \i -> do
        [l, r, d] <- getInts
        return $ solve l r d

    mapM_ print ans


import Control.Monad

solve :: [Int] -> Int
solve [x,y] = (d `div` 5) + e `div` 2 + e `mod` 2
  where
    d= abs (x-y)
    e=d `mod` 5

routine :: IO ()
routine = do
  l <- (map read.words) <$> getLine
  print $ solve l

main :: IO ()
main = do
  n <- readLn
  replicateM_ n routine

import Control.Monad
import Data.List

f :: Int -> Int
f x
    |r == 0 = t
    |or [r == 4, r == 3] = t + 2
    |otherwise = t + 1
    where r = x `mod` 5
          t = x `div` 5

main = do
    t <- readLn
    replicateM_ t $ do
        a <- fmap (sort . fmap read . words) getLine
        print $ f ((a !! 1) - (a !! 0))

import Control.Monad ( replicateM_ )

main = do
  n <- readLn
  replicateM_ n $ do
    [x, y] <- fmap (fmap read . words) getLine
    let diff = abs $ x - y
        div5 = diff `div` 5
        rem1 = (diff - div5 * 5)
        div2 = rem1 `div` 2
        remain = rem1 - (div2 * 2)
    print $ div5 + div2 + remain

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

process (a:b:[]) | y==0 = x
                 | y<=2 = x+1
		 | otherwise = x+2
		where 	x= div (abs (a-b)) 5
                        y= mod (abs (a-b)) 5

main = do
		n<- read <$> getLine ::IO Int
		a<-map (map read) <$> map words <$> replicateM n getLine ::IO [[Int]]
		mapM_ print $ map process a

process :: Int -> Int -> [Int] -> Int
process a b xs = sum $ zipWith div (scanr (flip mod) d (tail xs)) xs
    where d = abs (a-b)

readInt :: String -> Int
readInt = read

run :: Int -> IO ()
run 0 = return ()
run t = do
    [a,b] <- fmap (map readInt.words) getLine
    print $ process a b [1,2,5]
    run (t-1)

main :: IO ()
main = do
    t <- fmap readInt getLine
    run t

{-# LANGUAGE FlexibleContexts #-}

import Control.Monad(forM_, liftM, replicateM, replicateM_)

--import Debug.Trace
traceShowId :: a -> a
traceShowId = id

-- Input parsing
parseInts :: String -> [Int]
parseInts str = map read (words str)

list2pair :: [a] -> (a, a)
list2pair [x, y] = (x, y)

parseIntsM :: IO [Int]
parseIntsM = parseInts <$> getLine

-- Solution

data State = State {
  dist :: Int,
  steps :: Int
}

step :: Int -> State -> State
step k s =
  let d = dist s in
    State (d `mod` k) (steps s + (d `div` k))

stepCount :: Int -> Int -> Int
stepCount a b = steps . (step 1) . (step 2) . (step 5) $ (State (abs (b - a)) 0)

taskM :: IO ()
taskM = do
  [a, b] <- parseIntsM
  print $ stepCount a b

main :: IO ()
main = do
  [t] <- parseIntsM
  replicateM_ t taskM


import Control.Monad

f :: (Int,Int) -> Char -> (Int,Int)
f (ans,open) '(' = (ans,open+1)
f (ans,open) ')'
  | open > 0 = (ans,open-1)
  | otherwise = (ans+1,open)

main = do
  t <- read <$> getLine
  replicateM_ t $ do
    getLine
    s <- getLine
    print . fst $ foldl f (0,0) s

import Control.Monad

type Test = String

getLines :: Int -> IO [Test]
getLines count = replicateM count getTest

getTest :: IO Test
getTest = do
    _ <- getLine
    line <- getLine
    return line

getAnswer :: [Test] -> [Integer]
getAnswer [] = []
getAnswer (first:rest) = (process first 0 0 0) : getAnswer rest

process :: Test -> Integer -> Integer -> Integer -> Integer
process [] opened closed amount = 0
process (first:rest) opened closed amount = if first == '('
                                            then toAdd + process rest (opened + 1) closed (amount + add)
                                            else toAdd + process rest opened (closed + 1) (amount + add)
    where toAdd = if closed > opened && (closed - opened > amount)
                  then add
                  else 0
          add = if closed > opened && (closed - opened > amount)
                then closed - opened - amount
                else 0

main :: IO ()
main = do
    count <- getLine
    tests <- getLines (read count)
    mapM_ print (getAnswer tests)

bracketToNumber :: Char -> Int
bracketToNumber '(' = 1
bracketToNumber ')' = -1

solve :: String -> Int
solve = negate . minimum . scanl (+) 0 . map bracketToNumber

main :: IO ()
main = interact $ unlines . map show . map solve . map snd . filter (even . fst) . zip [1 .. ] . tail . lines

{-# OPTIONS -O2 #-}

import Control.Monad (replicateM_)
import Control.Monad.Writer

main = readLn >>= flip replicateM_ (getLine >> getLine >>= print . f)

f :: String -> Int
f = counter 0 where
    counter :: Int -> String -> Int
    counter _ [] = 0
    counter n (x:xs) = case x of
        '('         -> counter (n+1) xs
        ')' | n > 0 -> counter (n-1) xs
            | otherwise -> 1 + counter n (xs ++ ")")
        x           -> error $ "Strange character " ++ show x

import           Control.Monad (forM_, replicateM)

main = do
  t <- readLn
  bracketStrings <- replicateM t $ getLine >> getLine
  forM_ bracketStrings (\s -> print (snd (foldl (\(l, c) p -> if p == '('
                                                              then (l+1, c)
                                                              else if l == 0
                                                                   then (0, c+1)
                                                                   else (l-1, c)) (0, 0) s)))

import           Control.Monad (forM_, replicateM)

main = do
  t <- readLn
  replicateM t $ do
    getLine
    s <- getLine
    print $ snd $ foldl (\(l, c) p -> if p == '('
                                      then (l+1, c)
                                      else if l == 0
                                           then (0, c+1)
                                           else (l-1, c)) (0, 0) s

{-# LANGUAGE Safe #-}

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

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

solve :: String -> Int
solve cs = maximum $ scanl (flip (\c-> if c==')' then (+1) else (subtract 1))) 0 cs

import Control.Monad

type Test = String

getLines :: Int -> IO [Test]
getLines count = replicateM count getTest

getTest :: IO Test
getTest = do
    _ <- getLine
    line <- getLine
    return line

getAnswer :: [Test] -> [Integer]
getAnswer [] = []
getAnswer (first:rest) = (process first 0 0 0) : getAnswer rest

process :: Test -> Integer -> Integer -> Integer -> Integer
process [] opened closed amount = 0
process (first:rest) opened closed amount = if first == '('
                                            then toAdd + process rest (opened + 1) closed (amount + add)
                                            else toAdd + process rest opened (closed + 1) (amount + add)
    where toAdd = if closed > opened && (closed - opened > amount)
                  then add
                  else 0
          add = if closed > opened && (closed - opened > amount)
                then closed - opened - amount
                else 0

main :: IO ()
main = do
    count <- getLine
    tests <- getLines (read count)
    print (getAnswer tests)


{-# 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)
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)
import Control.DeepSeq
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 #-}
slices :: Int -> [a] -> [[a]]
slices n xs = map(take n).takeWhile(not.null) $ iterate(drop n)xs
{-# INLINE slices #-}

main :: IO ()
main = do
  [m,n] <- map read.words <$> getLine
  xss <- slices n.map readInt.B.words <$> B.getContents
  putStr.unwords.map show $ solve $ transpose xss

solve :: [[Int]] -> [Int]
solve (xs:xss) = foldl' step (scanl1 (+) xs) xss
   where
     step :: [Int] -> [Int] -> [Int]
     step times ys = times`deepseq`go 0 times ys
      where
        go !time (t:times) (y:ys)
          | time <= t = (t+y)    : go (t+y)    times ys
          | otherwise = (time+y) : go (time+y) times ys
        go _ _ _ = []

import Data.List
import Data.Maybe (fromJust)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

main :: IO()
main = putStrLn . unwords . map show . solve . map (fst . fromJust . C.readInt) . C.words =<< B.getContents

solve :: [Int] -> [Int]
solve (n:m:a) = let b = transpose $ rect 0 [] a
                in solve' b (replicate n 0)
                where rect :: Int -> [Int] -> [Int] -> [[Int]]
                      rect _ l [] = [reverse l]
                      rect i l (a:s) | i == m = reverse l : rect 0 [] (a:s)
                                     | otherwise = rect (i + 1) (a:l) s

                      solve' :: [[Int]] -> [Int] -> [Int]
                      solve' s t = foldl (\ t l -> addTo (head t) l t) t s

                      addTo :: Int-> [Int] -> [Int] -> [Int]
                      addTo _ [] [] = []
                      addTo f (l:ls) (t:ts) = let nt = l + max f t
                                              in nt : addTo nt ls ts

import Data.List
import Data.Maybe (fromJust)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

main :: IO()
main = putStrLn . unwords . map show . solve . map (fst . fromJust . C.readInt) . C.words =<< B.getContents

solve :: [Int] -> [Int]
solve (n:m:a) = let b = transpose $ rect 0 [] a
                in solve' b (replicate n 0)
                where rect :: Int -> [Int] -> [Int] -> [[Int]]
                      rect _ l [] = [reverse l]
                      rect i l (a:s) | i == m = reverse l : rect 0 [] (a:s)
                                     | otherwise = rect (i + 1) (a:l) s

                      solve' :: [[Int]] -> [Int] -> [Int]
                      solve' [] t = t
                      solve' (l:s) t = solve' s (addTo (head t) l t)

                      addTo :: Int-> [Int] -> [Int] -> [Int]
                      addTo _ [] [] = []
                      addTo f (l:ls) (t:ts) = let nt = l + max f t
                                              in nt : addTo nt ls ts

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

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

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

gao :: [Int] -> [Int] -> [Int]
gao a b = let c = zipWith (+) b $ zipWith max a (0:c) in c

main :: IO ()
main = do
  [m,n] <- getInts
  arr <- replicateM m getInts
  let ans = tail . map last $ scanl gao (replicate n 0) arr
  putStrLn $ unwords $ map show ans

{-# OPTIONS_GHC -O2 -optc-Ofast -fignore-asserts #-}
import Control.Applicative
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as C

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

gao :: [Int] -> [Int] -> [Int]
gao a b = let c = zipWith (+) b $ zipWith max a $ 0: c in c

main :: IO ()
main = do
  (m:n:_) <- getInts
  a <- replicateM m $ getInts
  let ans = tail . map last $ scanl gao (replicate n 0) a
  putStrLn $ unwords $ map show ans
  where
  getInts = map readInt . C.words <$> C.getLine


import Data.Foldable
import Data.Function
import Data.List
import Data.Maybe
import Data.Traversable

getLineOfInts :: IO [Int]
getLineOfInts = do
  line <- getLine
  let ints = read <$> words line :: [Int]
  return ints

putLineOfInts :: [Int] -> IO ()
putLineOfInts xs =
  putStrLn $ unwords $ (show <$> xs)

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

  for_ [1..t] $ \_ -> do
    [n, m] <- getLineOfInts

    rows <- for [1..n] $ \_ -> getLineOfInts
    cols <- for [1..m] $ \_ -> getLineOfInts

    let memberOfFirstCol = head $ head rows
    let firstCol = fromJust $ find (memberOfFirstCol `elem`) cols

    let rowIndex r = fromJust $ elemIndex (head r) firstCol
    let indexedRows = (\r -> (rowIndex r, r)) <$> rows

    let sortedIndexedRows = sortBy (compare `on` fst) indexedRows

    for sortedIndexedRows $ \(_, r) -> putLineOfInts r

{-# LANGUAGE BlockArguments, BangPatterns, LambdaCase, TypeApplications #-}

import Prelude
import Data.List as L
import Data.Maybe
import Data.Functor
import Data.Foldable
import Data.Bifunctor
import Data.Function
import qualified Data.Set as Set
import qualified Data.Map as Map
import Control.Monad
import Control.Applicative
import Data.ByteString.Char8 as B
import Control.Arrow

main :: IO ()
main = do
  n <- readLn
  replicateM_ n do
    [n, m] <- fmap int . B.words <$> B.getLine
    rows <- replicateM n $ (id &&& Set.fromList) . fmap int . B.words <$> B.getLine
    c : cols <- replicateM m $ fmap int . B.words <$> B.getLine
    B.putStr $ B.unlines $ fmap (B.unwords . fmap (B.pack . show)) $ fmap (`selectContaining` rows) c
  where
    selectContaining n = fst . fromJust . L.find (Set.member n . snd)

int :: ByteString -> Int
int s = let Just (a, _) = readInt s in a


{-# LANGUAGE ViewPatterns #-}
module Main where
import Data.List
import Data.Int

default (Int)
main = interact exec
exec = show . uncurry solve . (\(n:xs) -> splitAt n xs) . map read . words
solve (rsort -> xs) (rsort -> ys) = play (0 :: Int64) (0 :: Int64) xs ys
play a b [] [] = a - b
play a b (x:xs) [] = play b (a +. x) [] xs
play a b [] (_:ys) = play b a ys []
play a b xxs@(x:xs) yys@(y:ys)
  | x >= y = play b (a +. x) yys xs
  | x < y  = play b a       ys  xxs
rsort = sortBy (flip compare)

a +. b = a + fromIntegral b

import Control.Applicative
import Data.List

main = do
  n <- readLn :: IO Int
  as <- reverse . sort . map read . words <$> getLine :: IO [Integer]
  bs <- reverse . sort . map read . words <$> getLine :: IO [Integer]

  let (x,y) = foldl (\(a,b) (c,d) -> (a+c,b+d)) (0,0) (solve 0 as bs)
  print $ x - y

solve _ [] [] = []
solve t [] (b:bs) = if t == 0 then solve 1 [] bs else (0,b) : solve 0 [] bs
solve t (a:as) [] = if t == 0 then (a,0) : solve 1 as [] else solve 0 as []
solve t (a:as) (b:bs)
  | t == 0 = if a > b then (a,0) : solve 1 as (b:bs) else solve 1 (a:as) bs
  | t == 1 = if a < b then (0,b) : solve 0 (a:as) bs else solve 0 as (b:bs)

import Control.Applicative
import Data.List

main = do
  n <- readLn :: IO Int
  as <- reverse . sort . map read . words <$> getLine :: IO [Int]
  bs <- reverse . sort . map read . words <$> getLine :: IO [Int]

  let (x,y) = foldl (\(a,b) (c,d) -> (a+c,b+d)) (0,0) (solve 0 as bs)
  print $ x - y

solve _ [] [] = []
solve t [] (b:bs) = if t == 0 then solve 1 [] bs else (0,b) : solve 0 [] bs
solve t (a:as) [] = if t == 0 then (a,0) : solve 1 as [] else solve 0 as []
solve t (a:as) (b:bs)
  | t == 0 = if a > b then (a,0) : solve 1 as (b:bs) else solve 1 (a:as) bs
  | t == 1 = if a < b then (0,b) : solve 0 (a:as) bs else solve 0 as (b:bs)

{-# LANGUAGE ExistentialQuantification, NoMonomorphismRestriction #-}
module Main where
import Data.Int
import Control.Applicative
default (Int64)
main = interact exec
exec = show . solve . tail . map read . words
solve = runFold ((\s p n m -> if p && n then s else s - 2 * m) <$> premap abs sumf <*> anyf (>= 0) <*> anyf (<= 0) <*> premap abs minf)
sumf = Fold (+) 0 id
anyf f = Fold (\a x -> a || f x) False id
minf = Fold min maxBound id

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
premap f (Fold step begin done) = Fold step' begin done
  where
    step' x a = step x (f a)


import qualified Data.Map.Strict as Map
import qualified Data.Set as Set

type ColourMap = Map.Map Char [Int]

zipWithRemainder :: [a] -> [b] -> ([(a, b)], [a], [b])
zipWithRemainder a b = helper a b []
  where helper (ah:at) (bh:bt) acc = helper at bt ((ah,bh):acc)
        helper a b acc = (acc, a, b)

query q map = Map.findWithDefault [] q map

getColourMap :: String -> ColourMap
getColourMap inp = Map.fromListWith (++) $ zip inp (map return [1..])

stage1 :: ColourMap -> ColourMap -> ([(Int, Int)], ColourMap, ColourMap)
stage1 a b = helper (Map.keys a) (Map.keys b) a b []
  where helper [] _ a b acc = (acc, a, b)
        helper _ [] a b acc = (acc, a, b)
        helper ak@(ah:at) bk@(bh:bt) a b acc
          | ah < bh || ah == '?'  = helper at bk a b acc
          | ah > bh || bh == '?'  = helper ak bt a b acc
          | otherwise =
             let
               (pairs, newa, newb) = zipWithRemainder (query ah a) (query bh b)
             in
               helper at bt (Map.insert ah newa a) (Map.insert bh newb b) (pairs ++ acc)

stage2 :: ([(Int, Int)], ColourMap, ColourMap) -> ([(Int, Int)], ColourMap, ColourMap)
stage2 arg = (helperB. helperA) arg
  where
    helperA (acc, a, b) = let
        aWild = query '?' a
        pairWild [] _ b acc = ([], b, acc)
        pairWild wilds [] b acc = (wilds, b, acc)
        pairWild wilds ('?':bt) b acc = pairWild wilds bt b acc
        pairWild wilds (bh:bt) b acc =
          let
            (pairs, newWilds, newb) = zipWithRemainder wilds (query bh b)
          in
            pairWild newWilds bt (Map.insert bh newb b) (pairs ++ acc)
        (newWild, newB, newAcc) = pairWild aWild (Map.keys b) b []
      in
        (newAcc ++ acc, Map.insert '?' newWild a, newB)
    helperB (acc, a, b) =
      let
        inv (a, b) = (b, a)
        (pairs, nb, na) = helperA ([], b, a)
      in
        ((map inv pairs) ++ acc, na, nb)

stage3 (acc, a, b) = acc ++ zip (query '?' a) (query '?' b)

main = do
  n <- getLine
  a <- getLine
  b <- getLine
  let sol = (stage3 . stage2) $ stage1 (getColourMap a) (getColourMap b)
  putStrLn . show $ length sol
  let showPair (a, b) = (show a) ++ (' ' : show b)
  mapM_ (putStrLn . showPair) sol

{-# LANGUAGE BangPatterns, Safe #-}
{-# Options_GHC -O2 #-}
import Data.ByteString.Builder
import Data.Monoid
import System.IO
import qualified Data.ByteString.Char8 as C
import Data.Time (diffTimeToPicoseconds, utctDayTime, getCurrentTime)
import Data.Char
import Data.Array

getSeed = return . fromInteger . (`mod` 999983) . diffTimeToPicoseconds . utctDayTime =<< getCurrentTime :: IO Int

ru, ri :: C.ByteString -> Int

ru = C.foldl' (\a c -> a * 10 - 48 + ord c) 0
ri s
  | C.head s == '-' = negate $ ru $ C.tail s
  | otherwise = ru s

c :: Char -> Int
c '?' = 0
c x = ord x - 96

cvt :: C.ByteString -> Array Int [Int]
cvt !s = accumArray ins [] (0, 26) $ (`zip` [1 .. ]) $ map c $ C.unpack s
  where
    ins e a = a : e

merge a b = l `seq` (zip a b, drop l a, drop l b)
  where
    l = min (length a) (length b)

solve :: [C.ByteString] -> Builder
solve (sn:x:y:l) = intDec (length res) <> char7 '\n' <> (mconcat $ map enc res)
  where
    n = ri $ C.takeWhile isDigit sn
    a = cvt $ C.take n x
    b = cvt $ C.take n y
    res = loop 1 [] (a ! 0) (b ! 0)
    enc (u, v) = intDec u <> char7 ' ' <> intDec v <> char7 '\n'
    loop i r u v
      | i > 26 = let (r', u', v') = merge u v in concat (r' : r)
      | otherwise = loop (succ i) (d : d0 : d1 : r) u' v'
      where
        ai = a ! i
        bi = b ! i
        (d, ai', bi') = merge ai bi
        (d0, ai'', v') = merge ai' v
        (d1, u', bi'') = merge u bi'

main :: IO ()
main = hPutBuilder stdout . solve . C.lines =<< C.getContents


f :: Int -> [Int] -> Bool
f n (x:xs) = let k = (n-x) `mod` n
             in check n k 1 xs

check :: Int -> Int -> Int -> [Int] -> Bool
check _ _ _ [] = True
check n k i (x:xs) | odd i = (if x >= k then x - k else n - (k - x)) == i && check n k (i+1) xs
                   | even i = (x + k) `mod` n == i && check n k (i+1) xs

main = do
    n <- fmap read getLine
    xs <- fmap (map read . words) getLine
    putStrLn $ if f n xs then "Yes"
                         else "No"

{-# 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

solve xs@(x:_) n =
  (zipWith (\a b -> (a + b * x) `mod` n) xs (cycle [-1, 1])) == [0..(n-1)]

main = do
  n <- readInt <$> getLine
  xs <- map readInt . words <$> getLine
  putStrLn (if solve xs n then "Yes" else "No")

import Control.Applicative

solve :: Int -> [Int] -> Bool
solve n list@(x:xs) = and . zipWith (\y (i,z) -> ((y+z) `mod` n) == i) list . zip [0..n-1] $ cycle [n-x,x]

main :: IO ()
main = do
  n <- readLn
  xs <- map read . words <$> getLine
  putStrLn $ if solve n xs == True
    then "Yes"
    else "No"

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

solve :: [Int] -> String
solve (n:a) = solve' 0
    where
        solve' :: Int -> String
        solve' m | m == n           = "No"
                 | check a m 0 True = "Yes"
                 | otherwise        = solve' (m+1)

        check :: [Int] -> Int -> Int -> Bool -> Bool
        check []     _ _ _ = True
        check (a:ax) s i add | add && (rem (a+s) n) == i   = check ax s (i+1) False
                             | (not add) && (rem (a-s+n) n) == i = check ax s (i+1) True
                             | otherwise = False

-- http://codeforces.com/problemset/problem/556B.hs

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

createGears :: Int -> [Int] -> [[Int]]
createGears n active = createGears' True active
	where
		createGears' _ [] = []
		createGears' isOdd (a:as)
			| isOdd = (drop a $ cycle [0..(n-1)]) : createGears' (not isOdd) as
			| otherwise = (drop (n-a-1) $ cycle [(n-1),(n-2)..0]) : createGears' (not isOdd) as

checkGears :: Int -> [[Int]] -> Bool
checkGears 0 _ = False
checkGears n gears
	| sequenced = True
	| otherwise = checkGears (n-1) (pushButton gears)
	where
		sequenced = isSequence $ map head gears
		pushButton xs = map (drop 1) $ xs

isSequence :: (Enum a, Eq a) => [a] -> Bool
isSequence [] = True
isSequence (x:[]) = True
isSequence (x:y:zs)
	| y == succ x = isSequence $ y:zs
	| otherwise = False

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

main = do

	n <- fmap read $ getLine :: IO Int
	active <- fmap (map read) $ fmap words $ getLine :: IO [Int]

	let
		valid = checkGears n $ createGears n active

	case valid of
		True -> putStrLn "Yes"
		False -> putStrLn "No"

import Data.List

main = getContents >>= putStrLn . (\b -> if b then "Yes" else "No") . solve . map read . tail . words

solve :: [Int] -> Bool
solve xs = foldl1 (||) [solve' xs i | i <- [1..length xs]]

solve' xs i = let ys = go xs i (length xs) in ys == [0..length xs-1]

go [] _ _ = []
go (x:xs) i n = mod (x + i) n : go xs (n - i) n

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

f :: [Integer] -> Integer -> Bool -> [Integer]
f [] _ _ = []
f (x:xs) n b
    | b = (x+n) : f xs n (not b)
    | otherwise = (x-n) : f xs n (not b)

main = do
    n <- getLine
    tab <- readInts
    let d = head tab in
        putStrLn $ if (map (flip mod $ read n) $ f tab d False) == (take (read n) [0..])
            then "Yes"
            else "No"

import Data.List (group)

probB line = if (length . group) b == 1 then "Yes" else "No"
    where a = map read $ words line :: [Int]
          n = length a
          f x y = if even y then y - x else x - y
          b = map ((`mod` n ).(+ n)) $ zipWith f a [0..]

main = do
    _ <- getLine
    line <- getLine
    putStrLn $ probB line

probB line = if loop (n + 1) a then "Yes" else "No"
    where
        a = map read $ words line :: [Int]
        n = length a
        f x y = (x + y + n) `mod` n
        loop (-1) _ = False
        loop _ a | foldr (&&) True $ zipWith (==) a [0..n-1] = True
        loop k a = loop (k-1) $ zipWith f a $ cycle [1, -1]

main = do
    _ <- getLine
    line <- getLine
    putStrLn $ probB line

module Main where

parse x = (takeWhile (/=' ') x) : if (null rest) then [] else parse (tail $ (dropWhile (/=' ')) x) where
    rest = dropWhile (/=' ') x

read'::String->Int
read'=read
isOk :: Int -> [Int] -> Bool
isOk n [] = True
isOk n [x] = True
isOk n (x:y:z) = mod (x + y) n == 0 && isOk n (y:z)

word True = "Yes"
word False = "No"

main = do
    s1 <- getLine
    s2 <- getLine
    putStr $ word $ isOk (read s1) $ map (`mod` (read s1)) $ zipWith (-) [0..(read s1)-1] (map read' $ parse s2)

bool :: a -> a -> Bool -> a
bool f _ False = f
bool _ t True  = t

parse :: String -> [Int]
parse contents = map read (words a)
                 where [_, a] = lines contents

solve :: [Int] -> Bool
solve a = any isPermutation . take n . iterate rotate $ a
          where n             = length a
                isPermutation = (== [0..n - 1])
                rotate        = zipWith plus (cycle [1, n - 1])
                x `plus` y    = (x + y) `rem` n

main :: IO ()
main = putStrLn . bool "No" "Yes" . solve . parse =<< getContents

import Data.List (sort)

bool :: a -> a -> Bool -> a
bool f _ False = f
bool _ t True  = t

parse :: String -> [Int]
parse contents = map read (words a)
                 where [_, a] = lines contents

solve :: [Int] -> Bool
solve a = any (isPermutation . rotation) [0..n - 1]
          where n             = length a
                isPermutation = (== [0..n - 1]) . sort
                rotate   d    = (`rem` n) . (+ d)
                rotation d    = zipWith rotate (cycle [d, d * (n - 1)]) a

main :: IO ()
main = putStrLn . bool "No" "Yes" . solve . parse =<< getContents

{-# 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

solve x xs n =
  (zipWith (\a b -> (a + b * x) `mod` n) xs (cycle [-1, 1])) == [0..(n-1)]

main = do
  n <- readInt <$> getLine
  (x:xs) <- map readInt . words <$> getLine
  putStrLn (if solve x xs n then "Yes" else "No")


import Data.List
import Data.Ord (comparing)

type Summand = (Int, Int)

readExpression::[Char]->Int->[Summand]
readExpression [] _ = []
readExpression (x:xs) 0  = let
				z = if (x=='-')	then (-1) else 1
				in readExpression xs z
readExpression t z = let
						(s,t1) = readSummand t z
						all = readExpression t1 0
					in (s:all)

readSummand::[Char]->Int->(Summand, [Char])
readSummand (x:xs) z | '0'<=x && x<='9' = let
							(k,t1) = readCoefficient (read [x]) xs
							(i,t2) = readIncrement t1
							zk = z*k
							in ((zk,i),t2)

readSummand t z = let
							(i,t1) = readIncrement t
							in ((z,i),t1)

readIncrement::[Char]->(Int, [Char])
readIncrement ('+':'+':'a':xs) = (1,xs)
readIncrement ('a':'+':'+':xs) = (0,xs)

readCoefficient::Int->[Char]->(Int, [Char])
readCoefficient k ('*':xs) = (k, xs)
readCoefficient k (x:xs) = readCoefficient (10*k + (read [x])) xs

sortSummands::[Summand]->[Summand]
sortSummands xs = sortBy cmpCoeff xs
	where cmpCoeff = comparing fst

calc::Int->[Summand]->Int
calc _ [] = 0
calc a ((c,i):xs) = let
					a' = if (i==1) then (a+1) else a
					s = c * a'
					in s + (calc (a+1) xs)

calc2::[Char]->[Summand]->Int
calc2 s x = calc (read s) x

main = do
	a <- getLine
	x <- getLine
	putStrLn (show (calc2 a (sortSummands (readExpression x 1))))

import List
main = interact f
f input = output where
  [init,expr] = lines input
  output = show $ cal (read init::Int) $ sort $ coef ("+":(summ $ incr expr))
  incr [] = []
  incr ('a':'+':'+':xs) = 'b':(incr xs)
  incr ('+':'+':'a':xs) = 'c':(incr xs)
  incr (x:xs) = x:(incr xs)
  summ [] = []
  summ xs = if null y then [x] else x:[head y]:(summ $ tail y)
    where (x,y) = break (\x -> x=='+' || x=='-') xs
  coef [] = []
  coef (x:y:xs) = ((if x=="+" then 1 else -1) * c,d):(coef xs)
    where
      (v,w) = break (=='*') y
      c = read (if null w then "1" else v) :: Int
      d = if null w then v else (tail w)
  cal x [] = 0
  cal x ((y,z):xs) = (if z=="b" then y*x else y*(x+1)) + (cal (x+1) xs)


import qualified Data.Map.Strict as Map

toint s = (read s) :: Integer

agregar x m = Map.insertWith (+) x 1 m
quitar :: Integer -> (Map.Map Integer Integer) -> (Map.Map Integer Integer)
quitar x m = Map.insertWith (+) x (-1) m

doit :: [Integer] -> (Map.Map Integer Integer) -> [Integer] -> Integer
doit [] s p = 0
doit (x:xs) s p =
	let ss = quitar x s in
	if any (\t -> (Map.findWithDefault 0 (t-x) ss) > 0) p then
		doit xs s p
	else
		1 + (doit xs s p)

asdasd [] = Map.fromList []
asdasd (x:xs) = agregar x (asdasd xs)

solve::String -> String
solve ss =
	let x = Prelude.map toint (tail (words ss)) in
	let s = asdasd x in
	let p = Prelude.map (\x -> 2^x) [0..30] in
	let r = doit x s p in
	(show r) ++ "\n"

main = do
  interact $ solve

import qualified Data.IntMap.Strict as IntMap

main = interact $ pureMain

pureMain :: String -> String
pureMain s = show $ length $ filter (== False) $ map f a
  where
    [[n], a] = map (map read . words) $ lines s
    m = foldr (\ k -> IntMap.insertWith (+) k 1) IntMap.empty a
    f n = or $ map (\ x -> if n == x then m IntMap.! x > 1 else IntMap.member x m) [2^i - n | i <- [0..31]]

import qualified Data.IntSet as IntSet

main = interact $ pureMain

pureMain :: String -> String
pureMain s = show $ length $ filter (== False) $ map f a
  where
    [[n], a] = map (map read . words) $ lines s
    set = IntSet.fromList a
    f n = or $ map (\ x -> IntSet.member x set) [2^i - n | i <- [0..31], 2^i - n /= n]


import Control.Applicative ((<$>))
import Data.List (groupBy, sort, unfoldr)
import Data.Array (listArray, elems, (!))
import Data.Function (on)

data Zipper a = Zipper { zleft :: Zipper a, zhead :: a, zright :: Zipper a }

zipper :: [a] -> Zipper a
zipper xs =
    let (first,last) = go last xs first in first
  where
    go :: Zipper a -> [a] -> Zipper a -> (Zipper a, Zipper a)
    go prev []     next = (next,prev)
    go prev (x:xs) next =
      let
        this = Zipper prev x rest
        (rest,last) = go this xs next
      in (this,last)

data Result = Result { rcount :: Int, rz :: [Int], rp :: Maybe Result }

instance Eq Result where
  (==) = (==) `on` rcount
instance Ord Result where
  (<=) = (<=) `on` rcount

main = do
  [n, s] <- map read . words <$> getLine
  as <- map read . words <$> getLine :: IO [Int]

  let
    vector l = listArray (1, length l) l

    gs = map (map snd) . groupBy ((==) `on` fst) . sort $ zip as [1..]

    r@(Result c _ _) = minimum $ solve $ reverse gs
      where
        zs g = take (length g) . iterate zright $ zipper g

        d i j = min d' (n-d') where d' = abs $ i-j
        dc i j = if i <= j then j-i else j+n-i -- clockwise distance
        dcc i j = dc j i

        right l = map zhead . take l . iterate zright
        left l = map zhead . take l . iterate zleft

        solve [g] = map f $ zs g
          where
            l = length g
            f z@(Zipper (Zipper _ p _) i (Zipper _ n _)) =
              min
                (Result (d s p + dc i p) (right l z) Nothing)
                (Result (d s n + dcc i n) (left l z) Nothing)

        solve (g:gs) = map f $ zs g
          where
            l = length g
            rs = solve gs

            f z@(Zipper (Zipper _ p _) i (Zipper _ n _)) =
                minimum $ map f' rs
              where
                f' r@(Result c (i':_) _) =
                    min
                      (Result (c + d i' p + dc i p) (right l z) (Just r))
                      (Result (c + d i' n + dcc i n) (left l z) (Just r))
                  where

    restore = zipWith diff is $ tail is
      where
        diff a b
          | d1 < d2 = if a <= b then d1 else -d1
          | otherwise = if a <= b then -d2 else d2
          where
            d1 = abs (b-a)
            d2 = n - d1

    is = (s:) . concat $ reverse $ unfoldr next (Just r)
      where
        next Nothing = Nothing
        next (Just (Result _ l r)) = Just (reverse l, r)

  print c
  -- print gs
  -- print is
  putStr $ unlines $ map (\a -> if a >= 0 then "+" ++ show a else show a) $ restore

import Control.Applicative ((<$>))
import Data.List (groupBy, sort, unfoldr)
import Data.Function (on)

data Zipper a = Zipper { zleft :: Zipper a, zhead :: a, zright :: Zipper a }

zipper :: [a] -> Zipper a
zipper xs =
    let (first,last) = go last xs first in first
  where
    go :: Zipper a -> [a] -> Zipper a -> (Zipper a, Zipper a)
    go prev []     next = (next,prev)
    go prev (x:xs) next =
      let
        this = Zipper prev x rest
        (rest,last) = go this xs next
      in (this,last)

data Result = Result { rcount :: Int, rz :: [Int], rp :: Maybe Result }

instance Eq Result where
  (==) = (==) `on` rcount
instance Ord Result where
  (<=) = (<=) `on` rcount

main = do
  [n, s] <- map read . words <$> getLine
  as <- map read . words <$> getLine :: IO [Int]

  let
    gs = map (map snd) . groupBy ((==) `on` fst) . sort $ zip as [1..]

    r@(Result c _ _) = minimum $ foldl next [Result 0 [s] Nothing] gs
      where
        next rs g = map f . take l . iterate zright $ zipper g
          where
            l = length g
            right = map zhead . take l . iterate zright
            left = map zhead . take l . iterate zleft

            f z@(Zipper (Zipper _ prev _) i (Zipper _ next _)) =
                minimum $ map f' rs
              where
                f' r@(Result c (i':_) _) =
                    min
                      (Result (c + d i' prev + dc i prev) (right z) (Just r))
                      (Result (c + d i' next + dcc i next) (left z) (Just r))
                  where
                    d i j = min d' (n-d') where d' = abs $ i-j
                    dc i j = if i <= j then j-i else j+n-i -- clockwise distance
                    dcc i j = dc j i

    restore = zipWith diff is (tail is)
      where
        diff a b
          | d1 < d2 = if a <= b then d1 else -d1
          | otherwise = if a <= b then -d2 else d2
          where
            d1 = abs (b-a)
            d2 = n - d1

        is = concat . reverse $ unfoldr next (Just r)
          where
            next Nothing = Nothing
            next (Just (Result _ l r)) = Just (reverse l, r)

  print c
  putStr . unlines $ map (\a -> if a >= 0 then "+" ++ show a else show a) restore

import Control.Applicative ((<$>))
import Data.List (groupBy, sort, unfoldr)
import Data.Function (on)

data Zipper a = Zipper { zleft :: Zipper a, zhead :: a, zright :: Zipper a }

zipper :: [a] -> Zipper a
zipper xs =
    let (first,last) = go last xs first in first
  where
    go :: Zipper a -> [a] -> Zipper a -> (Zipper a, Zipper a)
    go prev []     next = (next,prev)
    go prev (x:xs) next =
      let
        this = Zipper prev x rest
        (rest,last) = go this xs next
      in (this,last)

data Result = Result { rcount :: Int, rz :: [Int], rp :: Maybe Result }

instance Eq Result where
  (==) = (==) `on` rcount
instance Ord Result where
  (<=) = (<=) `on` rcount

main = do
  [n, s] <- map read . words <$> getLine
  as <- map read . words <$> getLine :: IO [Int]

  let
    gs = map (map snd) . groupBy ((==) `on` fst) . sort $ zip as [1..]

    r@(Result c _ _) = minimum . solve $ reverse gs
      where
        solve [] = [Result 0 [s] Nothing]
        solve (g:gs) = map f . take l . iterate zright $ zipper g
          where
            l = length g
            right = map zhead . take l . iterate zright
            left = map zhead . take l . iterate zleft
            rs = solve gs

            f z@(Zipper (Zipper _ prev _) i (Zipper _ next _)) =
                minimum $ map f' rs
              where
                f' r@(Result c (i':_) _) =
                    min
                      (Result (c + d i' prev + dc i prev) (right z) (Just r))
                      (Result (c + d i' next + dcc i next) (left z) (Just r))
                  where
                    d i j = min d' (n-d') where d' = abs $ i-j
                    dc i j = if i <= j then j-i else j+n-i -- clockwise distance
                    dcc i j = dc j i

    restore = zipWith diff is $ tail is
      where
        diff a b
          | d1 < d2 = if a <= b then d1 else -d1
          | otherwise = if a <= b then -d2 else d2
          where
            d1 = abs (b-a)
            d2 = n - d1

    is = concat $ reverse $ unfoldr next (Just r)
      where
        next Nothing = Nothing
        next (Just (Result _ l r)) = Just (reverse l, r)

  print c
  putStr $ unlines $ map (\a -> if a >= 0 then "+" ++ show a else show a) $ restore

{-# OPTIONS_GHC -O2 #-}

import Control.Applicative ((<$>))
import Data.List (groupBy, sort, unfoldr)
import Data.Function (on)

data Zipper a = Zipper { zleft :: Zipper a, zhead :: a, zright :: Zipper a }

zipper :: [a] -> Zipper a
zipper xs =
    let (first,last) = go last xs first in first
  where
    go :: Zipper a -> [a] -> Zipper a -> (Zipper a, Zipper a)
    go prev []     next = (next,prev)
    go prev (x:xs) next =
      let
        this = Zipper prev x rest
        (rest,last) = go this xs next
      in (this,last)

data Result = Result { rcount :: Int, rz :: [Int], rp :: Maybe Result }

instance Eq Result where
  (==) = (==) `on` rcount
instance Ord Result where
  (<=) = (<=) `on` rcount

main = do
  [n, s] <- map read . words <$> getLine
  as <- map read . words <$> getLine :: IO [Int]

  let
    gs = map (map snd) . groupBy ((==) `on` fst) . sort $ zip as [1..]

    r@(Result c _ _) = minimum . solve $ reverse gs
      where
        solve [] = [Result 0 [s] Nothing]
        solve (g:gs) = map f . take l . iterate zright $ zipper g
          where
            l = length g
            right = map zhead . take l . iterate zright
            left = map zhead . take l . iterate zleft
            rs = solve gs

            f z@(Zipper (Zipper _ prev _) i (Zipper _ next _)) =
                minimum $ map f' rs
              where
                f' r@(Result c (i':_) _) =
                    min
                      (Result (c + d i' prev + dc i prev) (right z) (Just r))
                      (Result (c + d i' next + dcc i next) (left z) (Just r))
                  where
                    d i j = min d' (n-d') where d' = abs $ i-j
                    dc i j = if i <= j then j-i else j+n-i -- clockwise distance
                    dcc i j = dc j i

    restore = zipWith diff is (tail is)
      where
        diff a b
          | d1 < d2 = if a <= b then d1 else -d1
          | otherwise = if a <= b then -d2 else d2
          where
            d1 = abs (b-a)
            d2 = n - d1

        is = concat . reverse $ unfoldr next (Just r)
          where
            next Nothing = Nothing
            next (Just (Result _ l r)) = Just (reverse l, r)

  print c
  putStr . unlines $ map (\a -> if a >= 0 then "+" ++ show a else show a) restore

import Control.Applicative ((<$>))
import Data.List (groupBy, sort, unfoldr)
import Data.Array (listArray, elems, (!))

main = do
  [n, s] <- map read . words <$> getLine
  as <- map read . words <$> getLine :: IO [Int]

  let
    gs = map (map snd) . groupBy (\a b -> fst a == fst b) . sort $ zip as [1..]
    m = length gs
    a = listArray (1, m) $ map (\l -> listArray (1, length l) l) gs
    ls = listArray (1, m) $ map length gs

    d i j = min a (n-a) where a = abs $ i - j
    dc i j = if i <= j then j-i else j+n-i -- clockwise
    dcc i j = dc j i

    rs = listArray (1, m) $ map c' [1..m]
      where
        c' i = listArray (1, l) [f i j | j <- [1..l]]
          where
            l = ls!i

        f 1 j = min (d s p1 + dc p p1, False, j, 0) (d s p2 + dcc p p2, True, j, 0)
          where
            p = a!1!j

            l = ls!1

            j1 = (j-2+l) `mod` l + 1
            j2 = j `mod` l + 1

            p1 = a!1!j1
            p2 = a!1!j2

        f i j = minimum $ map f [1..ls!(i-1)]
          where
            f j' = min (c + c1, False, j, j') (c + c2, True, j, j')
              where
                (c, _, _, _) = rs!(i-1)!j'

                p' = a!(i-1)!j'

                c1 = d p' p1 + dc p p1
                c2 = d p' p2 + dcc p p2

            p = a!i!j

            l = ls!i

            j1 = (j-2+l) `mod` l + 1
            j2 = j `mod` l + 1

            p1 = a!i!j1
            p2 = a!i!j2

    r@(c, _, _, _) = minimum [rs!m!j | j <- [1..ls ! m]]

    restore = zipWith diff is $ tail is
      where
        diff a b
          | d1 < d2 = if a <= b then d1 else -d1
          | otherwise = if a <= b then -d2 else d2
          where
            d1 = abs (b-a)
            d2 = n - d1

        is = (s:) . concat $ map list un
        un = reverse $ unfoldr next (m, r)
        next (0, _) = Nothing
        next (i, (_, dir, j, j')) = Just ((i, dir, j), (i-1, rs!(i-1)!j'))

        list (i, dir, j) =
            take l (if dir then drop j es else drop (l-j+1) $ reverse es)
          where
            es = es' ++ es' where es' = elems $ a!i
            l = ls!i

  print c
  putStr $ unlines $ map (\a -> if a >= 0 then "+" ++ show a else show a) $ restore

import Control.Applicative ((<$>))
import Data.List (groupBy, sort, unfoldr)
import Data.Function (on)

data Zipper a = Zipper { zleft :: Zipper a, zhead :: a, zright :: Zipper a }

zipper :: [a] -> Zipper a
zipper xs =
    let (first,last) = go last xs first in first
  where
    go :: Zipper a -> [a] -> Zipper a -> (Zipper a, Zipper a)
    go prev []     next = (next,prev)
    go prev (x:xs) next =
      let
        this = Zipper prev x rest
        (rest,last) = go this xs next
      in (this,last)

data Result = Result { rcount :: Int, rz :: [Int], rp :: Maybe Result }

instance Eq Result where
  (==) = (==) `on` rcount
instance Ord Result where
  (<=) = (<=) `on` rcount

main = do
  [n, s] <- map read . words <$> getLine
  as <- map read . words <$> getLine :: IO [Int]

  let
    gs = map (map snd) . groupBy ((==) `on` fst) . sort $ zip as [1..]

    r@(Result c _ _) = minimum $ foldl next r0 gs
      where
        r0 = [Result 0 [s] Nothing]
        next rs g = map f . take l . iterate zright $ zipper g
          where
            l = length g
            right = map zhead . take l . iterate zright
            left = map zhead . take l . iterate zleft

            f z@(Zipper (Zipper _ prev _) i (Zipper _ next _)) =
                minimum $ map f' rs
              where
                f' r@(Result c (i':_) _) =
                    min
                      (Result (c + d i' prev + dc i prev) (right z) (Just r))
                      (Result (c + d i' next + dcc i next) (left z) (Just r))
                  where
                    d i j = min d' (n-d') where d' = abs $ i-j
                    dc i j = if i <= j then j-i else j+n-i -- clockwise distance
                    dcc i j = dc j i

    restore = zipWith diff is (tail is)
      where
        diff a b
          | d1 < d2 = if a <= b then d1 else -d1
          | otherwise = if a <= b then -d2 else d2
          where
            d1 = abs (b-a)
            d2 = n - d1

        is = concat . reverse $ unfoldr next (Just r)
          where
            next Nothing = Nothing
            next (Just (Result _ l r)) = Just (reverse l, r)

  print c
  putStr . unlines $ map (\a -> if a >= 0 then "+" ++ show a else show a) restore


{-# LANGUAGE ScopedTypeVariables #-}

import Text.Printf
import Data.List
import Data.Functor
import Data.Function
import Control.Monad
import Data.Maybe
import Data.Foldable (foldr', foldr1)
import Prelude
import Control.Monad.State.Strict
import Data.Int
import qualified Data.ByteString.Char8 as B8
import qualified Data.Map.Strict as MapS
import qualified Data.Set as Set

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

main :: IO ()
main = do
    [n, k] <- (map readB8Int) <$> B8.words <$> B8.getLine
    (prices :: [Int64]) <- (map $ fromIntegral . readB8Int) <$> B8.words <$> B8.getLine
    let minPrice = foldr1 min prices
    let possible = all (\price -> (price - minPrice) `mod` (fromIntegral k) == 0) prices
    if not possible
        then printf "-1\n"
        else printf "%lld\n" $ foldr1 (+) $ map (\price -> (price - minPrice) `div` (fromIntegral k)) $ prices

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import qualified Data.ByteString as B (ByteString, getLine)
import qualified Data.ByteString.Char8 as C (readInt, words, reverse, dropWhile, unpack)
import qualified Data.Maybe as M (fromJust)
import Data.Char (isSpace)
import Data.Int (Int64)

readInt :: B.ByteString -> Int64
readInt = fromIntegral . fst . M.fromJust . C.readInt

run k (x:xs) ans | x `mod` k /= 0 = []
                 | otherwise = run k xs (x `div` k:ans)
run k [] ans = ans

g [] = -1
g x = sum $ map (\i -> i - mx) x where mx = minimum x

main = do
    (map readInt . C.words -> [n, k]) <- B.getLine
    (map readInt . C.words -> xs) <- B.getLine
    let mxs = minimum xs
    putStrLn $ show $ g (run k (map (\i -> i - mxs) xs) [])

import Control.Applicative( (<$>))
main = do
    [n_, k_] <- map read . words <$> getLine
    as_ <- map (read::String->Integer) . words <$> getLine
    let
	min = minimum as_

	quot' a b =
		    let
			(q,r) = a `quotRem` b
		    in
			if r==0 then Just q else Nothing

	maybeSum sm [] = Just sm
	maybeSum sm (Nothing:_) = Nothing
	maybeSum sm ((Just v):rem) = maybeSum (sm+v) rem

    case maybeSum 0 $ map ((`quot'` k_) . subtract min) as_ of
	Just v	-> print v
	Nothing -> print (-1)

import Data.Functor

posMod x y = mod ((mod x y) + y) y

sol :: Integer -> [Integer] -> Integer
sol k a = sum $ map (`div` k) $ map (+ (-m)) $ a where
  m = foldl1 min a

main = do
  [n, k] <- map read <$> words <$> getLine
  a <- map read <$> words <$> getLine
  putStrLn $ show $
    if and $ map (\x -> x `posMod` k == head a `posMod` k) a then sol k a
    else -1

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import qualified Data.ByteString as B (getLine)
import qualified Data.ByteString.Char8 as C (readInt, words, reverse, dropWhile, unpack)
import qualified Data.Maybe as M (fromJust)
import Data.Char (isSpace)

readInt = fst . M.fromJust . C.readInt

run k (x:xs) ans | x `mod` k /= 0 = []
                 | otherwise = run k xs (x `div` k:ans)
run k [] ans = ans

g :: [Int] -> Int
g [] = -1
g x = sum $ map (\i -> i - (minimum x)) x

main = do
    (map readInt . C.words -> [n, k]) <- B.getLine
    (map readInt . C.words -> xs) <- B.getLine
    putStrLn $ show $ g (run k (map (\i -> i - (minimum xs)) xs) [])

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

import qualified Data.ByteString as B (getLine)
import qualified Data.ByteString.Char8 as C (readInt, words, reverse, dropWhile, unpack)
import qualified Data.Maybe as M (fromJust)
import Data.Char (isSpace)

readInt = fst . M.fromJust . C.readInt

run k (x:xs) ans | x `mod` k /= 0 = []
                 | otherwise = run k xs (x `div` k:ans)
run k [] ans = ans

g :: [Int] -> Int
g [] = -1
g x = sum $ map (\i -> i - (minimum x)) x

main = do
    (map readInt . C.words -> [n, k]) <- B.getLine
    (map readInt . C.words -> xs) <- B.getLine
    putStrLn $ show $ g (run k xs [])


import Data.List
import Data.Array
import Data.Ord
import Data.Char

res x=solve x [0,0,0] where
	solve [] v = maximum v
	solve (x:xs) [v1,v2,v3]
		|x=='a' 	= solve xs [v1+1,v2,1+(maximum [v1,v2,v3])]
		|otherwise 	= solve xs [v1,1+ (max v1 v2),v3]
main=interact$show.res.head.lines

{-# 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.Array.Unboxed

main = B.interact exec
exec = B.pack . evalState app . B.words
app = show `fmap` (solve . B.unpack) `fmap` pop
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  int = maybe undefined fst . B.readInt

solve s = maximum [as!(i - 1) + bs!(j - 1) - bs!(i - 1) + as!n - as!(j - 1) | i <- [1..n + 1], j <- [i..n + 1]]
  where
  n = length s
  as = listArray (0, n) $ scanl (+) 0 $ map (fromEnum . (== 'a')) s :: UArray Int Int
  bs = listArray (0, n) $ scanl (+) 0 $ map (fromEnum . (== 'b')) s :: UArray Int Int

import Prelude
import Data.List
import Data.Maybe
import Data.Function
import Data.Char
import Data.Graph
import Data.Tree
import Data.Int
--import qualified Data.HashTable.IO as B
import qualified Data.ByteString.Lazy.Char8 as B

readInts :: IO [Int]
readInts = fmap (map (foldl' (\x y -> 10*x + digitToInt y) 0) . words) getLine

textRepresentation :: Show a => [a] -> String
textRepresentation row = unwords . map show $ row

help listA listB maxA = g listB listA
    where f = \(b:xb) (a:xa) -> if null xb then b else max (b+maxA-a)     $ f xb xa
          g = \(b:xb) (a:xa) -> if null xa then a else max (a-b+ f xb xa) $ g xb xa

main :: IO ()
main = do
    str <- getLine
    let tableA = scanl (\x y -> if y=='a' then x+1 else x) 0 str
        tableB = scanl (\x y -> if y=='b' then x+1 else x) 0 str
    print $ help tableA tableB $ maximum tableA

--индексиране на памет
-- $! синтаксис
--fmap
{-main :: IO ()
main = do
    print 3
    where gg=H.fromList-}

import Prelude
import Data.List
import Data.Maybe
import Data.Function
import Data.Char
import Data.Graph
import Data.Tree
import Data.Int
import Data.Bits
import qualified Data.Set as Set
import qualified Data.ByteString.Lazy.Char8 as B

readInts :: IO [Int]
readInts = fmap (map (foldl' (\x y -> 10*x + digitToInt y) 0) . words) getLine

readInts64 :: IO [Int64]
readInts64 = fmap (map (foldl' (\x y -> 10*x + (fromInteger . toInteger . digitToInt $ y)) 0) . words) getLine

textRepresentation :: Show a => [a] -> String
textRepresentation row = unwords . map show $ row

help listA listB maxA = g listB listA
    where f = \(b:xb) (a:xa) -> if null xb then b else max (b+maxA-a)     $ f xb xa
          g = \(b:xb) (a:xa) -> if null xa then a else max (a-b+(f xb xa)) $ g xb xa

main :: IO ()
main = do
    str <- getLine
    let tableA = scanl (\x y -> if y=='a' then x+1 else x) 0 str
        tableB = scanl (\x y -> if y=='b' then x+1 else x) 0 str
        m = maximum tableA
    print $ help tableA tableB m

--kind
--по-бавно решение с глобален параметър
--индексиране на памет
--dolna granica na slojnost

import Prelude
import Data.List
import Data.Maybe
import Data.Function
import Data.Char
import Data.Graph
import Data.Tree
import Data.Int
--import qualified Data.HashTable.IO as B
import qualified Data.ByteString.Lazy.Char8 as B

readInts :: IO [Int]
readInts = fmap (map (foldl' (\x y -> 10*x + digitToInt y) 0) . words) getLine

textRepresentation :: Show a => [a] -> String
textRepresentation row = unwords . map show $ row

f :: [Int] -> [Int] -> Int -> Int
f [x] _ _= x
f (b:xb) (a:xa) maxA= max (b+maxA-a) $ f xb xa maxA

g :: [Int] -> [Int] -> Int -> Int
g [x] _ _ = x
g (a:xa) (b:xb) maxA = max (a+rec-b) $ g xa xb maxA
    where rec= f xb xa maxA

main :: IO ()
main = do
    str <- getLine
    let tableA = scanl (\x y -> if y=='a' then x+1 else x) 0 str
        maxA = maximum tableA
        tableB = scanl (\x y -> if y=='b' then x+1 else x) 0 str
    print $ g tableA tableB maxA

--индексиране на памет
-- $! синтаксис
--fmap
{-main :: IO ()
main = do
    print 3
    where gg=H.fromList-}

import Data.List
import Data.Char

readInts :: IO [Int]
readInts = fmap (map (foldl' (\x y -> 10*x + digitToInt y) 0) . words) getLine

f :: [Int] -> [Int] -> Int -> Int
f [x] _ _= x
f (b:xb) (a:xa) maxA= max (b+maxA-a) $ f xb xa maxA

help :: [Int] -> [Int] -> Int -> Int
help [x] _ _ = x
help (a:xa) (b:xb) maxA = max (a-b + f xb xa maxA) $ help xa xb maxA

{-help listA listB maxA = g listB listA
    where f = \(b:xb) (a:xa) -> if null xb then b else max (b+maxA-a)     $ f xb xa
          g = \(b:xb) (a:xa) -> if null xa then a else max (a-b+(f xb xa)) $ g xb xa
-}

main :: IO ()
main = do
    str <- getLine
    let tableA = scanl (\x y -> if y=='a' then x+1 else x) 0 str
        tableB = scanl (\x y -> if y=='b' then x+1 else x) 0 str
        m = maximum tableA
    print $ help tableA tableB m

{-# 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.Array.Unboxed

main = B.interact exec
exec = B.pack . evalState app . B.words
app = show `fmap` (solve . B.unpack) `fmap` pop
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  int = maybe undefined fst . B.readInt

solve s = maximum [as!(i - 1) + bs!j - bs!i + as!n - as!(j - 1) | i <- [1..n], j <- [i..n]]
  where
  n = length s
  as = listArray (0, n) $ scanl (+) 0 $ map (fromEnum . (== 'a')) s :: UArray Int Int
  bs = listArray (0, n) $ scanl (+) 0 $ map (fromEnum . (== 'b')) s :: UArray Int Int

{-# 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.Array.Unboxed

main = B.interact exec
exec = B.pack . evalState app . B.words
app = show `fmap` (solve . B.unpack) `fmap` pop
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  int = maybe undefined fst . B.readInt

solve s = maximum [as!(i - 1) + bs!(j - 1) - bs!(i - 1) + as!n - as!(j - 1) | i <- [1..n], j <- [i..n]]
  where
  n = length s
  as = listArray (0, n) $ scanl (+) 0 $ map (fromEnum . (== 'a')) s :: UArray Int Int
  bs = listArray (0, n) $ scanl (+) 0 $ map (fromEnum . (== 'b')) s :: UArray Int Int

import Prelude
import Data.List
import Data.Maybe
import Data.Function
import Data.Char
import Data.Graph
import Data.Tree
import Data.Int
--import qualified Data.HashTable.IO as B
import qualified Data.ByteString.Lazy.Char8 as B

readInts :: IO [Int]
readInts = fmap (map (foldl' (\x y -> 10*x + digitToInt y) 0) . words) getLine

textRepresentation :: Show a => [a] -> String
textRepresentation row = unwords . map show $ row

createTable :: Char -> [(Char,Int)] -> [(Char,Int)]
createTable element [] = [(element,1)]
createTable element list@((x,y):xs)
    | x /= element = (element,1):list
    | otherwise = (x,y+1):xs

forSplit :: Int -> Int -> [(Char,Int)] -> [([(Char,Int)],[(Char,Int)])]
forSplit i n list
    | i>=n = []
    | otherwise = splitAt i list : forSplit (i+1) n list

compareA (x,x1) (y,y1)
    | x == y = compare x1 y1
    | x == 'a' = GT
    | otherwise = LT

compareB (x,x1) (y,y1)
    | x == y = compare x1 y1
    | x == 'a' = LT
    | otherwise = GT

help :: [(Char,Int)]->((Char,Int),(Char,Int)) -> Int
help _ ((x,_),(y,_))
    | x=='b' || y=='b' =0
help list ((x,x1),(y,y1)) = x1+y1+(if l=='a' then 0 else r)
    where temp1 = dropWhile (/= (x,x1)) list
          temp  = dropWhileEnd (/= (y,y1)) temp1
          (l,r) = maximumBy compareB temp

main :: IO ()
main = do
    str <- getLine
    let table = foldr createTable [] str
        maxA = checkA $ maximumBy compareA table
        maxB = checkB $ maximumBy compareB table
    print $ foldl max (maxA+maxB) . map (help table) . map (\(x,y)->(func x,func y)) $ forSplit 1 (length table) table
    where checkA (a,b) = if a=='a' then b else 0
          checkB (a,b) = if a=='b' then b else 0
          func = maximumBy compareA

--индексиране на памет
-- $! синтаксис
--fmap
{-main :: IO ()
main = do
    print 3
    where gg=H.fromList-}


{-# LANGUAGE UnicodeSyntax #-}

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

split c s = word : find c rest
	where
		(word, rest) = break (== c) s

		find c [] = []
		find c s = split c $ tail s

between a b c = a <= c && c <= b

solve s =
		if length ss >= 2 &&
				(between 1 8 $ length $ head ss) &&
				(between 1 3 $ length $ last ss) &&
				all (between 2 11 . length) (tail $ init ss)
			then Just $ recombine ss'
			else Nothing

	where
		ss = split '.' s
		ss' = head ss :
			(concatMap (\s → let (a, b) = splitAt (min 3 $ length s - 1) s in [a, b]) $ tail $ init ss) ++
			[last ss]

		recombine [] = []
		recombine (x:y:xs) = (x ++ "." ++ y) : recombine xs

trim = f . f
	where
		f = reverse . dropWhile isSpace

main = do
	s ← trim <$> getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ putStrLn $ fromJust r
		else
			putStrLn "NO"

import Data.List

between n x m = n <= x && x <= m

candivide ws = let h = head ws
                   l = last ws
                   ws' = init (tail ws)
               in between 1 (length h) 8 &&
                  between 1 (length l) 3 &&
                  all (\x -> between 2 (length x) 11) ws'

divide cs | length cs <= 3 = [take (length cs - 1) cs, drop (length cs - 1) cs]
          | otherwise      = [take 3 cs, drop 3 cs]

concat_words [] = []
concat_words (x:y:xs) = (x ++ "." ++ y) : concat_words xs

find_dot [] = []
find_dot ('.':cs) = split_dot cs

split_dot cs = let (word, rest) = break (== '.') cs
               in word : find_dot rest

main = do s <- getLine
          let a = split_dot s
              h  = head a
              l  = last a
              ws = init (tail a)
          let divided = [h] ++ concatMap divide ws ++ [l]
          let concated = concat_words divided
          if length a < 2 then putStrLn "NO"
                          else if candivide a then do putStrLn "YES"
                                                      mapM_ putStrLn concated
                                              else putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Data.Char
import Data.Maybe

import qualified Data.ByteString.Char8 as B

solve s =
		if not $ length ss >= 2 && (let l = B.length (head ss) in 1 <= l && l <= 8) &&
				(let l = B.length (last ss) in 1 <= l && l <= 3) &&
				all (\l → 2 <= B.length l && B.length l <= 11) (tail (init ss))
			then Nothing
		else
			Just $ recombine ss'

	where
		ss = B.split '.' s
		ss' = [head ss] ++
			(concat $ map (\s → let (a, b) = B.splitAt (min 3 (B.length s - 1)) s in [a, b]) (tail (init ss))) ++
			[last ss]

		recombine [] = []
		recombine xs = ((xs !! 0) `B.append` (B.pack ".") `B.append` (xs !! 1)) : (recombine (tail (tail xs)))

main = do
	s ← B.getLine
	let r = solve $ fst $ B.breakEnd (not . isSpace) s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ B.putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

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

split c s = word : find c rest
	where
		(word, rest) = break (== c) s

		find c [] = []
		find c s = split c $ tail s

between a b c = a <= c && c <= b

solve s =
		if length ss >= 2 &&
				(between 1 8 $ length $ head ss) &&
				(between 1 3 $ length $ last ss) &&
				all (between 2 11 . length) (tail $ init ss)
			then Just $ recombine ss'
			else Nothing

	where
		ss = split '.' s
		ss' = head ss :
			(concatMap (\s → let (a, b) = splitAt (min 3 $ length s - 1) s in [a, b]) $ tail $ init ss) ++
			[last ss]

		recombine [] = []
		recombine (x:y:xs) = (x ++ "." ++ y) : recombine xs

main = do
	s ← getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

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

split x xs = split' xs
	where
		split' xs = xs' : if null xs'' then [] else split' $ tail xs''
			where
				(xs', xs'') = break (== x) xs

between a b c = a <= c && c <= b

solve s =
		if length ss >= 2 &&
				(between 1 8 $ length $ head ss) &&
				(between 1 3 $ length $ last ss) &&
				all (between 2 11 . length) (tail $ init ss)
			then Just $ recombine ss'
			else Nothing

	where
		ss = split '.' s
		ss' = head ss :
			(concatMap (\s → let (a, b) = splitAt (min 3 $ length s - 1) s in [a, b]) $ tail $ init ss) ++
			[last ss]

		recombine [] = []
		recombine (x:y:xs) = (x ++ "." ++ y) : recombine xs

main = do
	s ← getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

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

split x xs = split' xs
	where
		split' xs = xs' : split'' xs''
			where
				(xs', xs'') = break (== x) xs

		split'' [] = []
		split'' xs = split' $ tail xs

between a b c = a <= c && c <= b

solve s =
		if length ss >= 2 &&
				(between 1 8 $ length $ head ss) &&
				(between 1 3 $ length $ last ss) &&
				all (between 2 11 . length) (tail $ init ss)
			then Just $ recombine ss'
			else Nothing

	where
		ss = split '.' s
		ss' = head ss :
			(concatMap (\s → let (a, b) = splitAt (min 3 $ length s - 1) s in [a, b]) $ tail $ init ss) ++
			[last ss]

		recombine [] = []
		recombine (x:y:xs) = (x ++ "." ++ y) : recombine xs

main = do
	s ← getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Data.Char
import Data.Maybe

import qualified Data.ByteString.Char8 as B

solve s =
		if not $ length ss >= 2 &&
				(let l = B.length (head ss) in 1 <= l && l <= 8) &&
				(let l = B.length (last ss) in 1 <= l && l <= 3) &&
				all (\l → 2 <= B.length l && B.length l <= 11) (tail $ init ss)
			then Nothing
		else
			Just $ recombine ss'

	where
		ss = B.split '.' s
		ss' = head ss :
			(concat $ map (\s → let (a, b) = B.splitAt (min 3 $ B.length s - 1) s in [a, b]) (tail $ init ss)) ++
			[last ss]

		recombine [] = []
		recombine xs = (xs !! 0 `B.append` B.pack "." `B.append` (xs !! 1)) : (recombine $ tail $ tail xs)

trim = f . f
	where
		f = B.reverse . B.dropWhile isSpace

main = do
	s ← trim <$> B.getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ B.putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

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

split x xs = split' xs
	where
		split' xs = xs' : if null xs'' then [] else split' $ tail xs''
			where
				(xs', xs'') = break (== x) xs

between a b c = a <= c && c <= b

solve s =
		if length ss >= 2 &&
				(between 1 8 $ length $ head ss) &&
				(between 1 3 $ length $ last ss) &&
				all (between 2 11 . length) (tail $ init ss)
			then Just $ join $ [head ss] ++ (concatMap splitJoined $ tail $ init ss) ++ [last ss]
			else Nothing

	where
		ss = split '.' s

		splitJoined s = [a, b]
			where
				(a, b) = splitAt (min 3 $ length s - 1) s

		join [] = []
		join (x:y:xs) = (x ++ "." ++ y) : join xs

main = do
	s ← getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ putStrLn $ fromJust r
		else
			putStrLn "NO"

maxLengthName  =  8
maxLengthExt  =  3

solve :: String -> Maybe [String]
solve ""  =  Nothing
solve line  =  res >>= return . reverse . (map (\(name, ext) -> name ++ "." ++ ext))
  where
    res  =  f (Just []) "" line
    f :: Maybe [(String, String)] -> String -> String -> Maybe [(String, String)]
    f (Just []) "" ""  =  Nothing
    f (Just []) "" ('.' : _)  =  Nothing
    f (Just []) acc ""  =  Nothing
    f (Just []) acc ('.' : s)  =  f (Just [(acc, "")]) "" s
    f (Just []) acc (c : s)
      | length acc' > maxLengthName  =  Nothing
      | otherwise  =  f (Just []) acc' s
      where
        acc'  =  acc ++ [c]
    f (Just ((n, _):fs)) "" ""  =  Nothing
    f (Just ((n, _):fs)) "" ('.' : _)  =  Nothing
    f (Just ((n, _):fs)) "" (c : s)  =  f (Just ((n, ""):fs)) [c] s
    f (Just ((n, _):fs)) acc ""
      | length acc > maxLengthExt  =  Nothing
      | otherwise  =  Just ((n, acc):fs)
    f (Just ((n, _):fs)) acc ('.' : s)
      | length acc < 2  =  Nothing
      | otherwise  =  f (Just ((n', "") : (n, e') : fs)) "" s
      where
        (e', n')  =  if length acc <= maxLengthExt
                        then splitAt 1 acc
                        else splitAt 3 acc
    f (Just ((n, _):fs)) acc (c : s)
      | length acc' > maxLengthName + maxLengthExt  =  Nothing
      | otherwise  =  f (Just ((n, ""):fs)) acc' s
      where
        acc'  =  acc ++ [c]

printAns :: Maybe [String] -> IO ()
printAns Nothing  =  putStrLn "NO"
printAns (Just fs)  =  putStrLn "YES" >> mapM_ putStrLn fs

main :: IO ()
main  =  getLine >>= printAns . solve

import Data.List

main=interact$solve.parse

solve :: [String] -> String
solve (s:ss)
  | isValid (s:ss) = "YES\n" ++ intercalate "." ( (s: (map part$init ss)) ++ [last ss])
  | otherwise      = "NO\n"
solve _ = "NO\n"

part :: String -> String
part (a:b:c:d:e)=[a,b,c]++"\n"++d:e
part [a,b,c]=[a,b]++"\n"++[c]
part [a,b]=[a]++"\n"++[b]

parse :: String -> [String]
parse = lines.map f
   where f '.'='\n'
         f c = c

isValid :: [String] -> Bool
isValid [] = False
isValid (s:ss) = isValidName s && isValid' ss
isValid' [] = False
isValid' [s] = isValidExt s
isValid' (s:ss) = isValidNameExt s && isValid' ss

isValidName s = let l = length s
                in 1<=l && l<=8
isValidExt s = let l = length s
               in 1<=l && l<=3
isValidNameExt s = let l = length s
                   in 2<=l && l<=11

import Data.List

between n x m = n <= x && x <= m

candivide ws = let h = head ws
                   l = last ws
                   ws' = init (tail ws)
               in between 1 (length h) 8 &&
                  between 1 (length l) 3 &&
                  all (\x -> between 2 x 11) ws'

divide cs | length cs <= 3 = [take (length cs - 1) cs, drop (length cs - 1) cs]
          | otherwise      = [take 3 cs, drop 3 cs]

concat_words [] = []
concat_words (x:y:xs) = (x ++ "." ++ y) : concat_words xs

main = do s <- getLine
          let a = words [ if c == '.' then ' ' else c | c <- s]
              h  = head a
              l  = last a
              ws = init (tail a)
          let divided = [h] ++ concatMap divide ws ++ [l]
          let concated = concat_words divided
          mapM_ putStrLn concated

import Data.List

between n x m = n <= x && x <= m

candivide ws = let h = head ws
                   l = last ws
                   ws' = init (tail ws)
               in between 1 (length h) 8 &&
                  between 1 (length l) 3 &&
                  all (\x -> between 2 (length x) 11) ws'

divide cs | length cs <= 3 = [take (length cs - 1) cs, drop (length cs - 1) cs]
          | otherwise      = [take 3 cs, drop 3 cs]

concat_words [] = []
concat_words (x:y:xs) = (x ++ "." ++ y) : concat_words xs

main = do s <- getLine
          let a = words [ if c == '.' then ' ' else c | c <- s]
              h  = head a
              l  = last a
              ws = init (tail a)
          let divided = [h] ++ concatMap divide ws ++ [l]
          let concated = concat_words divided
          if candivide a then do putStrLn "YES"
                                 mapM_ putStrLn concated
                         else putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Data.Maybe

import qualified Data.ByteString.Char8 as B

solve s =
		if not $ B.length (head ss) <= 8 && B.length (last ss) <= 3 &&
				all (\l → 2 <= B.length l && B.length l <= 11) (tail (init ss))
			then Nothing
		else
			Just $ recombine ss'

	where
		ss = B.split '.' s
		ss' = [head ss] ++
			(concat $ map (\s → let (a, b) = B.splitAt ((min 3 (B.length s - 1)) - 1) s in [a, b]) (tail (init ss))) ++
			[last ss]

		recombine [] = []
		recombine xs = ((xs !! 0) `B.append` (B.pack ".") `B.append` (xs !! 1)) : (recombine (tail (tail xs)))

main = do
	s ← B.getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ B.putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Control.Arrow
import Data.Char
import Data.List
import Data.Maybe

split c = unfoldr $ \l ->
  if null l
  	then Nothing
	else Just $ second (drop 1) $ break (== c) l

solve s =
		if length ss >= 2 &&
				(let l = length $ head ss in 1 <= l && l <= 8) &&
				(let l = length $ last ss in 1 <= l && l <= 3) &&
				all (\s → let l = length s in 2 <= l && l <= 11) (tail $ init ss)
			then Just $ recombine ss'
			else Nothing

	where
		ss = split '.' s
		ss' = head ss :
			(concat $ map (\s → let (a, b) = splitAt (min 3 $ length s - 1) s in [a, b]) $ tail $ init ss) ++
			[last ss]

		recombine [] = []
		recombine xs = (xs !! 0 ++ "." ++ (xs !! 1)) : (recombine $ tail $ tail xs)

trim = f . f
	where
		f = reverse . dropWhile isSpace

main = do
	s ← trim <$> getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Data.Char
import Data.Maybe

import qualified Data.ByteString.Char8 as B

solve s =
		if not $ B.length (head ss) <= 8 && B.length (last ss) <= 3 &&
				all (\l → 2 <= B.length l && B.length l <= 11) (tail (init ss))
			then Nothing
		else
			Just $ recombine ss'

	where
		ss = B.split '.' s
		ss' = [head ss] ++
			(concat $ map (\s → let (a, b) = B.splitAt ((min 3 (B.length s - 1)) - 1) s in [a, b]) (tail (init ss))) ++
			[last ss]

		recombine [] = []
		recombine xs = ((xs !! 0) `B.append` (B.pack ".") `B.append` (xs !! 1)) : (recombine (tail (tail xs)))

main = do
	s ← B.getLine
	let r = solve $ fst $ B.breakEnd (not . isSpace) s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ B.putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Data.Maybe

import qualified Data.ByteString.Char8 as B

solve s =
		if not $ B.length (head ss) <= 8 && B.length (last ss) <= 3 &&
				all (\l → 2 <= B.length l && B.length l <= 11) (tail (init ss))
			then Nothing
		else
			Just $ recombine ss'

	where
		ss = B.split '.' s
		ss' = [head ss] ++
			(concat $ map (\s → let (a, b) = B.splitAt (min 8 (B.length s - 1)) s in [a, b]) (tail (init ss))) ++
			[last ss]

		recombine [] = []
		recombine xs = ((xs !! 0) `B.append` (B.pack ".") `B.append` (xs !! 1)) : (recombine (tail (tail xs)))

main = do
	s ← B.getLine
	let r = solve s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ B.putStrLn $ fromJust r
		else
			putStrLn "NO"

{-# LANGUAGE UnicodeSyntax #-}

import Control.Applicative
import Data.Char
import Data.Maybe

import qualified Data.ByteString.Char8 as B

solve s =
		if not $ length ss >= 2 && (let l = B.length (head ss) in 1 <= l && l <= 8) &&
				(let l = B.length (last ss) in 1 <= l && l <= 3) &&
				all (\l → 2 <= B.length l && B.length l <= 11) (tail (init ss))
			then Nothing
		else
			Just $ recombine ss'

	where
		ss = B.split '.' s
		ss' = [head ss] ++
			(concat $ map (\s → let (a, b) = B.splitAt ((min 3 (B.length s - 1)) - 1) s in [a, b]) (tail (init ss))) ++
			[last ss]

		recombine [] = []
		recombine xs = ((xs !! 0) `B.append` (B.pack ".") `B.append` (xs !! 1)) : (recombine (tail (tail xs)))

main = do
	s ← B.getLine
	let r = solve $ fst $ B.breakEnd (not . isSpace) s
	if isJust r
		then do
			putStrLn "YES"
			mapM_ B.putStrLn $ fromJust r
		else
			putStrLn "NO"

import Data.List

main=interact$solve.parse

solve :: [String] -> String
solve (s:ss)
  | isValid (s:ss) = "YES\n" ++ intercalate "." ( (s: (map part$init ss)) ++ [last ss])
  | otherwise      = "NO\n"
solve _ = "NO\n"

part :: String -> String
part (a:b:c:d:e)=[a,b,c]++"\n"++d:e
part (a:b:c:d)=[a,b]++"\n"++c:d
part (a:b:c)=[a]++"\n"++b:c

parse :: String -> [String]
parse = words.map f
   where f '.'=' '
         f c = c

isValid :: [String] -> Bool
isValid (s:ss) = isValidName s && isValid' ss
isValid _ = False
isValid' [] = False
isValid' [s] = isValidExt s
isValid' (s:ss) = isValidNameExt s && isValid' ss

isValidName s = let l = length s
                in 1<=l && l<=8
isValidExt s = let l = length s
               in 1<=l && l<=3
isValidNameExt s = let l = length s
                   in 2<=l && l<=11


import List

main = getLine >> getLine >>=
       \s -> putStrLn $ show $ head $ [1..] \\ (sort . map read . words) s

import List

next c []              = c
next c (x:xs) | c < x  = c
next c (x:xs) | c == x = next (c + 1) xs

main = do
  n <- readInt
  s <- getLine
  putStrLn $ show $ next 1 (sort $ list $ words $ s)

  where
  list []     = []
  list (c:cs) = (read c :: Int) : list cs

  readInt :: IO Int
  readInt = readLn

import List

main = do
    n <- (readLn :: IO Int)
    s <- getLine
    putStrLn $ show $ head $ [1..] \\
               (sort $ map (read :: String -> Int) $ words $ s)

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

main= do
	 n<- read <$> getLine ::IO Int
	 s<- map read. words <$> getLine ::IO [Int]
	 let a = accumArray  (||) (False) (1,n) $ zip (filter (<=n) s) (repeat True)
	 print $ 1 + ( length ( takeWhile id ( elems a)))

import Control.Monad(liftM)
import Data.List(sort)

main = do getLine
          nums <- liftM (zip [1..] . sort . map read . words) getLine
          let nums' = dropWhile (uncurry (==)) nums
              ans = if null nums'
                     then
                        length nums + 1
                     else
                        fst (head nums')
          print ans

import List
main = getLine >> fmap (map read . words) getLine >>= putStr . show . head . ([1 ..] \\)

import Data.List
main = do getLine >> fmap (map read . words) getLine >>= putStr . show . head . ([1 ..] \\)

import Data.List (sort)

main = do
  n <- getLine
  l <- getLine
  let a = sort $ readInt l
  putStrLn . show $ count a 1

readInt :: String -> [Int]
readInt l = map read (words l)

count :: [Int] -> Int -> Int
count [] y = y
count (x:xs) y = if x == y then count xs (y + 1) else y

import List

solve :: [Int] -> Int
solve xs  =  solve' 1 (sort xs)
  where
    solve' n []  =  n
    solve' n (m:xs)
      | n == m  =  solve' (n+1) xs
      | otherwise  =  n

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

solve :: [Int] -> Int
solve xs = head . filter (\x -> not $ x `elem` xs) $ [1..]
main = getLine >> fmap (map read . words) getLine >>= print . solve

main :: IO()
main = do
  _ <- getLine
  lines <- getLine
  print $ gao $ map read $ words lines

gao :: [Int] -> Int
gao a = if null lst then 1 else succ $ last lst
  where lst = takeWhile (`elem` a) [1..]

import Data.List

main :: IO()
main = do
  _ <- getLine
  lines <- getLine
  print $ gao $ map read $ words lines

gao :: [Int] -> Int
gao a = head $ [1..] \\ a

{-gao :: [Int] -> Int-}
{-gao a = if null lst then 1 else succ $ last lst-}
  {-where lst = takeWhile (`elem` a) [1..]-}

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

main :: IO ()
main = do
  n  <- readLn
  xs <- zip [1 ..] . sort . take n . map read . words <$> getLine
  print . maybe (n + 1) (+ 1) $ findIndex (uncurry (/=)) xs

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

main= do
	 n<- read <$> getLine ::IO Int
	 s<- map read. words <$> getLine ::IO [Int]
	 let a = accumArray  (||) (False) (1,n) $ zip (takeWhile (<=n) s) (repeat True)
	 print $ 1 + ( length ( takeWhile id ( elems a)))


analyze :: [[Int]] -> Bool
analyze [] = True
analyze (x:y:xs) = px >= cx && pVar >= 0 && cVar >= 0 && cVar <= pVar && analyze (y:xs)
    where
        (px, cx, py, cy) = (head x, x !! 1, head y, y !! 1)
        (pVar, cVar) = (py - px, cy - cx)
analyze (x:xs) = head x >= (x !! 1) && analyze xs

solve :: IO ()
solve = do
    n <- getLine >>= \v -> return (read v :: Int)
    let inps = getLine >>= \v -> return [read n :: Int | n <- (words v)]
    inps' <- sequence (take n $ repeat inps)
    let out = if analyze inps' then "YES" else "NO"
    putStrLn out

main :: IO ()
main = do
    t <- getLine >>= \v -> return (read v :: Int)
    sequence $ take t (repeat solve)
    return ()

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

process1 _ _ [] = True
process1 a b ([c,d]:e) | c<a = False
                       | d<b = False
                       | d>c = False
		       | c-a < d-b = False
	               | otherwise = process1 c d e

process v = process1 0 0 v

onecase = do
		n<- read <$> getLine ::IO Int
		v<- map (map read) <$> map words <$> replicateM n getLine :: IO [[Int]]
		putStrLn $ if process v then "YES" else "NO"

main::IO ()
main=do
  	t<- read <$> getLine ::IO Int
	replicateM_ t onecase

analyze :: [[Int]] -> Bool
analyze [] = True
analyze (x:y:xs) = head x >= (x !! 1) && head x <= head y && x !! 1 <= y !! 1 && analyze (y:xs)
analyze (x:xs) = head x >= (x !! 1) && analyze xs

solve :: IO ()
solve = do
    n <- getLine >>= \v -> return (read v :: Int)
    let inps = getLine >>= \v -> return [read n :: Int | n <- (words v)]
    inps' <- sequence (take n $ repeat inps)
    let out = if analyze inps' then "YES" else "NO"
    putStrLn out

main :: IO ()
main = do
    t <- getLine >>= \v -> return (read v :: Int)
    sequence $ take t (repeat solve)
    return ()

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

process1 _ _ [] = True
process1 a b ([c,d]:e) | c<a = False
                       | d<b = False
                       | d>c = False
	               | otherwise = process1 c d e

process v = process1 0 0 v

onecase = do
		n<- read <$> getLine ::IO Int
		v<- map (map read) <$> map words <$> replicateM n getLine :: IO [[Int]]
		putStrLn $ if process v then "YES" else "NO"

main::IO ()
main=do
  	t<- read <$> getLine ::IO Int
	replicateM_ t onecase