import Data.List
import Data.Maybe

moveLetters :: String -> String -> [Int] -> Int -> [Int]
moveLetters "" "" list _ = list
moveLetters (s:xs) (t:xt) list offset
    | s == t = moveLetters xs xt list $ offset + 1
    | otherwise = moveLetters newxs xt newList $ offset + 1
    where newList = list ++ (map (+ offset) $ reverse [0..i - 1])
          i = fromMaybe 0 $ findIndex (== t) (s:xs)
          newxs = s : (take (i - 1) xs ++ drop i xs)

main = do
    getLine
    s <- getLine
    t <- getLine

    if sort s /= sort t then do
        putStrLn "-1"
    else do
        let perms = moveLetters s t [] 0
        putStrLn . show $ length perms
        putStrLn . unwords . map (show . (+ 1)) $ perms

{-# LANGUAGE BangPatterns #-}
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 System.IO
import Data.ByteString.Lazy.Builder
import Data.ByteString.Lazy.Builder.ASCII
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

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

getInts :: IO [Int]
getInts = construct `fmap` BS.getLine

fastPrint :: Char -> [Int] -> IO ()
fastPrint mid arr = hPutBuilder stdout  $ build arr
    where build = foldr (\n b -> intDec n <> charUtf8 mid <> b) mempty

getMove from to
    | from == to = []
    | otherwise = findp : getMove (swap findp from) to
    where check idx = if from !! idx == to !! idx then check (idx+1) else head ([x|x <- [idx+1..length from - 1], from !! x == to !! idx])
          findp = check 0

swap x from = take (x-1) from ++ [from !! x] ++ [from !! (x-1)] ++ drop (x+1) from

main = do
    getLine
    s <- getLine
    t <- getLine
    if (sort s) /= (sort t) then putStrLn "-1"
    else do
        let move = getMove s t
        print (length move)
        putStrLn $ intercalate " " $ map show move

import Data.List
import Data.Maybe

moveLetter :: Int -> String -> String -> [Int]
moveLetter offset s t = reverse $ map (+ trueOffset) steps
        where stepCount = fromMaybe 0 $ findIndex (== head t) s
              steps = if stepCount == 0 then [0] else [0..stepCount - 1]
              trueOffset = if stepCount == 0 then offset - 1 else offset

getPermutations :: Int -> String -> String -> [Int]
getPermutations _ "" "" = []
getPermutations offset (s:sx) (t:tx)
    | s == t = getPermutations (offset + 1) sx tx
    | otherwise = curLetterPerms ++ (getPermutations (offset + 1) sx tx)
    where curLetterPerms = (moveLetter offset (s:sx) (t:tx))

main = do
    getLine
    s <- getLine
    t <- getLine

    if sort s /= sort t then do
        putStrLn "-1"
    else do
        let perms = getPermutations 0 s t
        putStrLn . show $ length perms
        putStrLn . unwords . map (show . (+ 1)) $ perms

--cdef
--dfec

import Data.List
import Data.Maybe

moveLetter :: Int -> String -> String -> [Int]
moveLetter offset s t = reverse $ map (+ offset) steps
        where stepCount = fromMaybe 0 $ findIndex (== head t) s
              steps = if stepCount == 0 then [-1] else [0..stepCount - 1]

getPermutations :: Int -> String -> String -> [Int]
getPermutations _ "" "" = []
getPermutations offset (s:sx) (t:tx)
    | s == t = getPermutations (offset + 1) sx tx
    | otherwise = curLetterPerms ++ (getPermutations (offset + 1) sx tx)
    where curLetterPerms = (moveLetter offset (s:sx) (t:tx))

main = do
    getLine
    s <- getLine
    t <- getLine

    if sort s /= sort t then do
        putStrLn "-1"
    else do
        let perms = getPermutations 0 s t
        putStrLn . show $ length perms
        putStrLn . unwords . map (show . (+ 1)) $ perms

--abdfce
--dbdfec


import Data.List (isPrefixOf, isSuffixOf)

solve :: String -> String
solve s
  | isFreda && isRainbow  =  "OMG>.< I don't know!"
  | isFreda  =  "Freda's"
  | isRainbow  =  "Rainbow's"
  | otherwise  =  "OMG>.< I don't know!"
  where
    isFreda  =  isSuffixOf "lala." s
    isRainbow  =  isPrefixOf "miao." s

main :: IO ()
main  =  do
    n  <-  readLn
    sequence_ [getLine >>= putStrLn . solve | i <- [1..n]]

import Control.Monad (replicateM)
import Data.List

detect s
    | f1 && not f2 = "Rainbow's"
    | not f1 && f2 = "Freda's"
    | otherwise = "OMG>.< I don't know!"
  where
    f1 = isPrefixOf "miao." s
    f2 = isSuffixOf "lala." s

main = do
  n <- readLn
  ss <- replicateM n getLine
  putStrLn $ unlines $ map detect ss

import Data.List

first s = take 5 s == "miao."
second s = (reverse $ take 5 $ reverse s) == "lala."

solve s = case (first s, second s) of
  (True, False) -> "Rainbow's"
  (False, True) -> "Freda's"
  _ -> "OMG>.< I don't know!"

main = interact $ unlines . map solve . tail . lines

import Control.Monad

freda s = takeLast 5 s == "lala."
rainbow s = take 5 s == "miao."

takeLast n xs | length xs <= n = xs
              | otherwise = takeLast n $ tail xs

parse s | freda s && rainbow s = "OMG>.< I don't know!"
        | freda s = "Freda's"
        | rainbow s = "Rainbow's"
        | otherwise = "OMG>.< I don't know!"

main = do
    n <- fmap read getLine
    ls <- forM [1..n] (\_ -> getLine)
    mapM putStrLn . map parse $ ls

import Data.List

main = interact $ unlines . map getAnswer . tail . lines

getAnswer :: String -> String
getAnswer s | isPrefixOf "miao." s && isSuffixOf "lala." s = "OMG>.< I don't know!"
            | isSuffixOf "lala." s                         = "Freda's"
            | isPrefixOf "miao." s                         = "Rainbow's"
            | otherwise                                    = "OMG>.< I don't know!"

import Data.List
main=interact$unlines.map f.tail.lines
f cs
  | isPrefixOf "miao." cs && isSuffixOf "lala." cs = "OMG>.< I don't know!"
  | isPrefixOf "miao." cs = "Rainbow's"
  | isSuffixOf "lala." cs = "Freda's"
  | otherwise = "OMG>.< I don't know!"

readNlines :: Int -> [IO String]
readNlines 0 = []
readNlines n = getLine:(readNlines $ n-1)

readNlinesM = sequence . readNlines
isPrefix, isSuffix            :: Eq a => [a] -> [a] -> Bool
isPrefix x y                  =  x == take (length x) y
isSuffix x y                  =  lx <= ly && x == drop (ly - lx) y
     where lx                  =  length x
           ly                  =  length y

solve :: [String] -> String
solve [] = ""
solve (x:xs) = forone ++ "\n" ++ (solve xs)
    where forone =
             if a && not b then "Freda's" else
             if b && not a then "Rainbow's" else "OMG>.< I don't know!"
          a = isSuffix "lala." x
          b = isPrefix "miao." x
main = do
    n <- getLine
    lines <- readNlinesM(read(n))
    (putStrLn . solve) lines

import Data.List (isPrefixOf, isSuffixOf)

solve :: String -> String
solve s
  | isFreda && isRainbow  =  " OMG>.< I don't know!"
  | isFreda  =  "Freda's"
  | isRainbow  =  "Rainbow's"
  | otherwise  =  " OMG>.< I don't know!"
  where
    isFreda  =  isSuffixOf "lala." s
    isRainbow  =  isPrefixOf "miao." s

main :: IO ()
main  =  do
    n  <-  readLn
    sequence_ [getLine >>= putStrLn . solve | i <- [1..n]]

import Control.Monad

freda s = takeLast 5 s == "lala."
rainbow s = take 5 s == "miao."

takeLast n xs | length xs <= n = xs
              | otherwise = takeLast n $ tail xs

parse s | freda s && rainbow s = "OMG>.< I don't know!"
        | freda s = "Freda's"
        | rainbow s = "Rainbow's"
        | otherwise = "OMG>.< I don't know!"

main = do
    n <- fmap read getLine
    ls <- forM [1..n] (\_ -> getLine)
    print . unlines . map parse $ ls


{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad
import Control.Applicative

tup3 [x, y, z] = (x, y, z)

ilo (x0, y0) (x1, y1) = x0 * y1 - x1 * y0

different p0@(x0, y0) p1@(x1, y1) l@(a, b, c)
  | a == 0 = y0 `compare` (-c/b) /= y1 `compare` (-c/b)
  | b == 0 = x0 `compare` (-c/a) /= x1 `compare` (-c/a)
  | c == 0 =
      let
        qy = (c + a) / (-b)
        v0 = ilo (x0, y0) (1, qy)
        v1 = ilo (x1, y1) (1, qy)
      in v0 * v1 < 0
  | otherwise =
      let
        px = (-c/a) -- (px, 0)
        qy = (-c/b) -- (0, qy)
        v0 = ilo (x0 - px, y0) (-px, qy)
        v1 = ilo (x1 - px, y1) (-px, qy)
      in v0 * v1 < 0

val True = 1
val False = 0

main = do
  [x0, y0]::[Double] <- map read . words <$> getLine
  [x1, y1]::[Double] <- map read . words <$> getLine
  n::Int <- read <$> getLine
  ps::[(Double, Double, Double)] <- forM [1..n] $ \_ -> (tup3 . map read . words <$> getLine)
  print . sum $ map (val . different (x0, y0) (x1, y1)) ps

main = print . solve . map read . words =<< getContents
sign a x b y c = a * x + b * y + c > 0
solve (x1:y1:x2:y2:n:lines) = solve' lines
    where
        solve' []        = 0
        solve' (a:b:c:s) | (sign a x1 b y1 c) /= (sign a x2 b y2 c) = 1 + (solve' s)
                         | otherwise                                = solve' s

main = do
  [x1, y1] <- getLine >>= return. map read. words :: IO [Integer]
  [x2, y2] <- getLine >>= return. map read. words :: IO [Integer]
  getLine
  allroad <- getContents >>= return. map (map read. words). lines :: IO [[Integer]]
  print. length. filter (\[a, b, c] -> (a * x1 +  b * y1 + c) * (a * x2 + b * y2 + c) < 0) $ allroad

import           Control.Monad
-- import           Debug.Trace
import           Data.List

main :: IO ()
main = do
  [hx, hy, ux, uy] <- map (read :: String -> Integer) . words . unwords <$> replicateM 2 getLine
  n <- (read :: String -> Int) <$> getLine
  let cond (a, b, c) =  signum (a * hx + b * hy + c) * signum (a * ux + b * uy + c) < 0
  print =<< sum . map (fromEnum . cond . (\[a,b,c]->(a,b,c)) . map (read :: String -> Integer) . words) <$> replicateM n getLine

import Control.Monad
readWords::Read a=>IO[a]
readWords = fmap (map read.words) getLine

side [a,b,c] [x,y] = signum $ a*x + b*y + c
intersects::(Num a,Eq a)=>[[a]]->[a]->Bool
intersects segment line = foldl1 (*) (map (side line) segment) == -1

count = (length.).filter

main = do
    way <- replicateM 2 readWords
    n <- readLn
    lines <- replicateM n readWords
    print$ count (intersects way) lines

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

module Main where

import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Function
import Data.Maybe
import Data.Int

main :: IO ()
main = do
  x1y1 <- readIntsIO
  x2y2 <- readIntsIO
  [n] <- readIntsIO
  abcs <- replicateM n readIntsIO
  print $ solve x1y1 x2y2 abcs

solve :: [Int] -> [Int] -> [[Int]] -> Int
solve x1y1 x2y2 abcs = ncross
  where
    f :: [Int] -> [Int] -> Int64
    f xy1 abc = sum $ zipWith ((*) `on` fromIntegral) xy1 abc
    nx1y1 = map (f (x1y1++[1])) abcs
    nx2y2 = map (f (x2y2++[1])) abcs
    ncross = length $ filter (0 >) $ zipWith ((*) `on` signum) nx1y1 nx2y2

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

main = do
    contents <- getContents
    let allLines = lines contents
    let pointHome = lineToPoint $ allLines !! 0
    let pointUniversity = lineToPoint $ allLines !! 1

    putStrLn $ show
        $ length
            $ filter (\x -> x == True)
            $ map ((isDividedByLine pointHome pointUniversity) . lineToCoefficient)
            $ drop 3 allLines

lineToPoint :: String -> (Float, Float)
lineToPoint line = (intsInLine !! 0, intsInLine !! 1)
    where intsInLine = map read $ words line

lineToCoefficient :: String -> (Float, Float, Float)
lineToCoefficient line = (intsInLine !! 0, intsInLine !! 1, intsInLine !! 2)
    where intsInLine = map read $ words line

standardToPointSlope :: (Float, Float, Float) -> (Float, Float)
standardToPointSlope (cx, cy, c) = (
        (-1.0 * (cx / cy)),
        (-1.0 * (c / cy))
    )

getInterceptBySlope :: (Float, Float) -> Float -> Float
getInterceptBySlope (x, y) slope = y - slope * x

isDividedByLine :: (Float, Float) -> (Float, Float) -> (Float, Float, Float) -> Bool
isDividedByLine (x0, y0) (x1, y1) (cx, cy, c)
    | cx == 0 = (0 >= ((y0 - m0) * (y1 - m0)))
    | cy == 0 = (0 >= ((x0 - m1) * (x1 - m1)))
    | otherwise =
        0 >= (getInterceptBySlope (x0, y0) slope - intercept) * (getInterceptBySlope (x1, y1) slope - intercept)
        where
            m0 = -1.0 * c / cy
            m1 = -1.0 * c / cx
            (slope, intercept) = standardToPointSlope (cx, cy, c)

main = do
    contents <- getContents
    let allLines = lines contents
    let pointHome = lineToPoint $ allLines !! 0
    let pointUniversity = lineToPoint $ allLines !! 1

    putStrLn $ show
        $ length
            $ filter (\x -> x == True)
            $ map ((calculateIsLineCrossing pointHome pointUniversity) . lineToCoefficient)
            $ drop 3 allLines

lineToPoint :: String -> (Float, Float)
lineToPoint line = (intsInLine !! 0, intsInLine !! 1)
    where intsInLine = map read $ words line

lineToCoefficient :: String -> (Float, Float, Float)
lineToCoefficient line = (intsInLine !! 0, intsInLine !! 1, intsInLine !! 2)
    where intsInLine = map read $ words line

calculateParallelLineMByK :: (Float, Float) -> Float -> Float
calculateParallelLineMByK (x, y) k = y - k * x

calculateIsLineCrossing :: (Float, Float) -> (Float, Float) -> (Float, Float, Float) -> Bool
calculateIsLineCrossing (x0, y0) (x1, y1) (cx, cy, c)
    | cx == 0 = (0 >= ((y0 - m0) * (y1 - m0)))
    | cy == 0 = (0 >= ((x0 - m1) * (x1 - m1)))
    | otherwise =
        0 >= (calculateParallelLineMByK (x0, y0) k - m) * (calculateParallelLineMByK (x1, y1) k - m)
            where
                m0 = -1.0 * c / cy
                m1 = -1.0 * c / cx
                (k, m) = coefficientToKM (cx, cy, c)

coefficientToKM :: (Float, Float, Float) -> (Float, Float)
coefficientToKM (cx, cy, c) = (-1.0 * (cx / cy), -1.0 * (c / cy))

import           Control.Monad

main :: IO ()
main = do
  [hx, hy] <- map (read :: String -> Int) . words <$> getLine
  [ux, uy] <- map (read :: String -> Int) . words <$> getLine
  n <- (read :: String -> Int) <$> getLine
  let cond (a, b, c) = compare (a * hx + b * hy + c) 0 /= compare (a * ux + b * uy + c) 0
  print =<< sum . map (fromEnum . cond . (\[a,b,c]->(a,b,c)) . map (read :: String -> Int) . words) <$> replicateM n getLine

import           Control.Monad
-- import           Debug.Trace
import           Data.List

main :: IO ()
main = do
  [hx, hy, ux, uy, n] <- map (read :: String -> Int) . words . unwords <$> replicateM 3 getLine
  let cond (a, b, c) =  signum (a * hx + b * hy + c) * signum (a * ux + b * uy + c) < 0
  print =<< sum . map (fromEnum . cond . (\[a,b,c]->(a,b,c)) . map (read :: String -> Int) . words) <$> replicateM n getLine

import           Control.Monad

main :: IO ()
main = do
  [hx, hy] <- map (read :: String -> Int) . words <$> getLine
  [ux, uy] <- map (read :: String -> Int) . words <$> getLine
  n <- (read :: String -> Int) <$> getLine
  let cond (a, b, c) = (a * hx + b * hy + c) * (a * ux + b * uy + c) < 0
  print =<< sum . map (fromEnum . cond . (\[a,b,c]->(a,b,c)) . map (read :: String -> Int) . words) <$> replicateM n getLine

{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad
import Control.Applicative

tup3 [x, y, z] = (x, y, z)

ilo (x0, y0) (x1, y1) = x0 * y1 - x1 * y0

different p0@(x0, y0) p1@(x1, y1) l@(a, b, c)
  | a == 0 = x0 `compare` (-c/b) /= x1 `compare` (-c/b)
  | b == 0 = y0 `compare` (-c/a) /= y1 `compare` (-c/a)
  | c == 0 =
      let
        qy = (c + a) / (-b)
        v0 = ilo (x0, y0) (1, qy)
        v1 = ilo (x1, y1) (1, qy)
      in v0 * v1 < 0
  | otherwise =
      let
        px = (-c/a) -- (px, 0)
        qy = (-c/b) -- (0, qy)
        v0 = ilo (x0 - px, y0) (-px, qy)
        v1 = ilo (x1 - px, y1) (-px, qy)
      in v0 * v1 < 0

val True = 1
val False = 0

main = do
  [x0, y0]::[Double] <- map read . words <$> getLine
  [x1, y1]::[Double] <- map read . words <$> getLine
  n::Int <- read <$> getLine
  ps::[(Double, Double, Double)] <- forM [1..n] $ \_ -> (tup3 . map read . words <$> getLine)
  print . sum $ map (val . different (x0, y0) (x1, y1)) ps

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

module Main where

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

main :: IO ()
main = do
  [x1, y1] <- readIntsIO
  [x2, y2] <- readIntsIO
  [n] <- readIntsIO
  abcs <- replicateM n readIntsIO
  print $ solve (x1, y1) (x2, y2) abcs

solve :: (Int, Int) -> (Int, Int)-> [[Int]] -> Int
solve x1y1 x2y2 abcs = ncross
  where
    f (x, y) [a, b, c] = a * x + b * y + c
    nx1y1 = map (f x1y1) abcs
    nx2y2 = map (f x2y2) abcs
    ncross = length $ filter (0 >) $ zipWith (*) nx1y1 nx2y2

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

main = do
    contents <- getContents
    let allLines = lines contents
    let pointHome = lineToPoint $ allLines !! 0
    let pointUniversity = lineToPoint $ allLines !! 1

    putStrLn $ show
        $ length
            $ filter (\x -> x == True)
            $ map ((calculateIsLineCrossing pointHome pointUniversity) . lineToCoefficient)
            $ drop 3 allLines

lineToPoint :: String -> (Int, Int)
lineToPoint line = (intsInLine !! 0, intsInLine !! 1)
    where intsInLine = map read $ words line

lineToCoefficient :: String -> (Int, Int, Int)
lineToCoefficient line = (intsInLine !! 0, intsInLine !! 1, intsInLine !! 2)
    where intsInLine = map read $ words line

calculateIsLineCrossing :: (Int, Int) -> (Int, Int) -> (Int, Int, Int) -> Bool
calculateIsLineCrossing point0 point1 coefficients =
    0 >= (applyLineEquation point0 coefficients * applyLineEquation point1 coefficients)

applyLineEquation :: (Int, Int) -> (Int, Int, Int) -> Int
applyLineEquation (x, y) (cx, cy, c) = x * cx + y * cy + c


import Control.Monad (liftM)
import Prelude hiding (catch)

eps :: Double
eps  =  1e-11

type Coord     =  Double
type Velocity  =  Double
type Time      =  Double
type Coords    =  (Coord, Coord, Coord)
type Harry     =  (Velocity, Coords)
type Segment   =  (Coords, Coords)

distance :: Coords -> Coords -> Double
distance (x1, y1, z1) (x2, y2, z2)
  =  sqrt $ sum $ map (^2) $ zipWith (-) [x1, y1, z1] [x2, y2, z2]

catch :: Velocity -> Time -> Segment -> Velocity -> Coords -> Bool
catch vS time (a, b) vP pP  =  timeS + eps > timeP
  where
    timeS  =  time + (distance a b) / vS
    timeP  =  (distance pP b) / vP

solve :: Velocity -> [Coords] -> Velocity -> Coords -> Maybe (Time, Coords)
solve vS (p0 : psS) vP pP
  | distance pP p0 < eps  =  Just (0, pP)
  | otherwise  =  solve' 0 (p0 : psS)
  where
    solve' time (a : b : psS)
      | catch vS time (a, b) vP pP  =  solve'' time (a, b)
      | otherwise  =  solve' (time + (distance a b) / vS) (b : psS)
    solve' _ _  =  Nothing
    solve'' time (a, b)
      | distance a b < eps  =  Just (time, a)
      | catch vS time (a, m) vP pP  =  solve'' time (a, m)
      | otherwise  =  solve'' (time + (distance a m) / vS) (m, b)
      where
        m  =  (\(x1, y1, z1) (x2, y2, z2) -> (0.5 * (x1 + x2), 0.5 * (y1 + y2), 0.5 * (z1 + z2))) a b

parseCoords :: String -> Coords
parseCoords line  =  case map read (words line) of
    [x, y, z]  ->  (x, y, z)

main :: IO()
main  =  do
    ls  <-  liftM lines getContents
    let n    =  read $ head ls
    let psS  =  map parseCoords . take (n + 1) . tail $ ls
    let [vP, vS]  =  map read . words . (!! (n + 2)) $ ls
    let pP  =  parseCoords . (!! (n + 3)) $ ls
    case solve vS psS vP pP of
        Just (time, (x, y, z))  ->  putStrLn "YES" >> print time >> putStrLn (concat [show x, " ", show y, " ", show z])
        Nothing  ->  putStrLn "NO"

import IO
import Text.Printf

toList (a, b, c) = a:b:c:[]
fList (a:b:c:_) = (a, b, c)

delta :: (Double, Double, Double) -> (Double, Double, Double) -> Double
delta (x1, y1, z1) (x2, y2, z2) = sqrt ((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)

deltaPlus :: (Double, Double, Double) -> (Double, Double, Double) -> Double -> (Double, Double, Double)
deltaPlus (x1, y1, z1) (x2, y2, z2) r =
    let (dx, dy, dz) = (x2 - x1, y2 - y1, z2 - z1)
    in  (x1 + dx*r, y1 + dy*r, z1 + dz*r)

coord = do
           line <- getLine
           --putStrLn (">" ++ (show (words line)))
           return (fList (map (read :: String -> Double) (words line)))

makeList 0 _ _ = return []
makeList n a p = do
                 c <- coord
                 other <- makeList (n-1) (a + delta c p) c
                 return ((c, a + delta c p, delta c p):other)

binSearch :: (Double -> (Int, a)) -> Double -> Double -> IO (Double, a)
binSearch f min max | max == min = error ("bin search: min == max")
binSearch f min max | otherwise = do
        --putStrLn (show (min, max, (max+min)/2))
        case (f ((max+min)/2)) of
             (-1, _) -> binSearch f min ((max + min)/2)
             (0, cx) -> return ((max + min)/2, cx)
             (1, _) -> binSearch f ((max + min)/2) max

bsf c0 c1 vp vc p t0 t =
    let d  = delta c0 c1
        cx = deltaPlus c0 c1 ((t - t0) / (d/vc))
        tn = (delta cx p) / vp
    in if (abs (tn - t)) < ((0.1)^(11))
       then (0, cx)
       else if (tn - t) < 0
            then (-1, cx)
            else (1, cx)

ff (c:[]) _ _ _ = return Nothing
ff ((c0, preLen, _):cc@(c1, preLen1, len):cs) vp vc p =
        if ((preLen1 / vc) - ((delta p c1) / vp)) >= -((0.1)^(11))
        then do r <- (binSearch (bsf c0 c1 vp vc p (preLen/vc)) (preLen/vc) (preLen1/vc))
                return (Just r)
        else ff (cc:cs) vp vc p

main = do
    n <- (getLine >>= return . read) :: IO Int
    c0 <- coord
    c <- makeList n 0 c0
    l <- getLine
    let (l1:l2:ll) = words l
        ivp = read l1 :: Int
        ivc = read l2 :: Int
        vp = realToFrac ivp
        vc = realToFrac ivc
    p <- coord
    a <- ff ((c0, 0, 0):c) vp vc p
    p <- if (a == Nothing)
         then putStrLn "NO"
         else let (Just (t, (x, y, z))) = a
              in printf "YES\n%.10f\n%.10f %.10f %.10f" t x y z
    return (0)

import Control.Monad (liftM)
import Prelude hiding (catch)

eps :: Double
eps  =  1e-10

type Coord     =  Double
type Velocity  =  Double
type Time      =  Double
type Coords    =  (Coord, Coord, Coord)
type Harry     =  (Velocity, Coords)
type Segment   =  (Coords, Coords)

distance :: Coords -> Coords -> Double
distance (x1, y1, z1) (x2, y2, z2)
  =  sqrt $ sum $ map (^2) $ zipWith (-) [x1, y1, z1] [x2, y2, z2]

catch :: Velocity -> Time -> Segment -> Velocity -> Coords -> Bool
catch vS time (a, b) vP pP  =  timeS + eps > timeP
  where
    timeS  =  time + (distance a b) / vS
    timeP  =  (distance pP b) / vP

solve :: Velocity -> [Coords] -> Velocity -> Coords -> Maybe (Time, Coords)
solve vS (p0 : psS) vP pP
  | distance pP p0 < eps  =  Just (0, pP)
  | otherwise  =  solve' 0 (p0 : psS)
  where
    solve' time (a : b : psS)
      | catch vS time (a, b) vP pP  =  solve'' time (a, b)
      | otherwise  =  solve' (time + (distance a b) / vS) (b : psS)
    solve' _ _  =  Nothing
    solve'' time (a, b)
      | distance a b < 0.5 * eps  =  Just (time, a)
      | catch vS time (a, m) vP pP  =  solve'' time (a, m)
      | otherwise  =  solve'' (time + (distance a m) / vS) (m, b)
      where
        m  =  (\(x1, y1, z1) (x2, y2, z2) -> (0.5 * (x1 + x2), 0.5 * (y1 + y2), 0.5 * (z1 + z2))) a b

parseCoords :: String -> Coords
parseCoords line  =  case map read (words line) of
    [x, y, z]  ->  (x, y, z)

main :: IO()
main  =  do
    ls  <-  liftM lines getContents
    let n    =  read $ head ls
    let psS  =  map parseCoords . take (n + 1) . tail $ ls
    let [vP, vS]  =  map read . words . (!! (n + 2)) $ ls
    let pP  =  parseCoords . (!! (n + 3)) $ ls
    case solve vS psS vP pP of
        Just (time, (x, y, z))  ->  putStrLn "YES" >> print time >> putStrLn (concat [show x, " ", show y, " ", show z])
        Nothing  ->  putStrLn "NO"

import IO
import Text.Printf

toList (a, b, c) = a:b:c:[]
fList (a:b:c:_) = (a, b, c)

delta :: (Double, Double, Double) -> (Double, Double, Double) -> Double
delta (x1, y1, z1) (x2, y2, z2) = sqrt ((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)

deltaPlus :: (Double, Double, Double) -> (Double, Double, Double) -> Double -> (Double, Double, Double)
deltaPlus (x1, y1, z1) (x2, y2, z2) r =
    let (dx, dy, dz) = (x2 - x1, y2 - y1, z2 - z1)
    in  (x1 + dx*r, y1 + dy*r, z1 + dz*r)

coord = do
           line <- getLine
           --putStrLn (">" ++ (show (words line)))
           return (fList (map (read :: String -> Double) (words line)))

makeList 0 _ _ = return []
makeList n a p = do
                 c <- coord
                 other <- makeList (n-1) (a + delta c p) c
                 return ((c, a + delta c p, delta c p):other)

binSearch :: (Double -> (Int, a)) -> Double -> Double -> IO (Double, a)
binSearch f min max | max == min = error ("bin search: min == max")
binSearch f min max | otherwise = do
        --putStrLn (show (min, max, (max+min)/2))
        case (f ((max+min)/2)) of
             (-1, _) -> binSearch f min ((max + min)/2)
             (0, cx) -> return ((max + min)/2, cx)
             (1, _) -> binSearch f ((max + min)/2) max

bsf c0 c1 vp vc p t0 t =
    let d  = delta c0 c1
        cx = deltaPlus c0 c1 ((t - t0) / (d/vc))
        tn = (delta cx p) / vp
    in if (abs (tn - t)) < ((0.1)^(7))
       then (0, cx)
       else if (tn - t) < 0
            then (-1, cx)
            else (1, cx)

ff (c:[]) _ _ _ = return Nothing
ff ((c0, preLen, _):cc@(c1, preLen1, len):cs) vp vc p = --do
        --putStrLn (show cc)
        if ((preLen1 / vc) >= ((delta p c1) / vp))
        then do r <- (binSearch (bsf c0 c1 vp vc p (preLen/vc)) (preLen/vc) (preLen1/vc))
                return (Just r)
        else ff (cc:cs) vp vc p

p1 (Just (t, (x, y, z))) = "YES2\n" ++
                  (
                   showsPrec 6 t ("\n" ++
                   showsPrec 6 x (" " ++
                   showsPrec 6 y (" " ++
                   showsPrec 6 z "")))
                  )

p2 (Just (t, (x, y, z))) = "YES\n" ++ (show t) ++
                           "\n" ++ (show x) ++ " " ++
                           (show y) ++ " " ++ (show z)

main = do
--    putStrLn ">0"
    n <- (getLine >>= return . read) :: IO Int
--    putStrLn ">1"
    c0 <- coord
--    putStrLn ">2"
    c <- makeList n 0 c0
--    putStrLn ">3"
    l <- getLine
    let (l1:l2:ll) = words l
        ivp = read l1 :: Int
        ivc = read l2 :: Int
        vp = realToFrac ivp
        vc = realToFrac ivc
--    putStrLn ">4"
    p <- coord
--    putStrLn ">end"
    a <- ff ((c0, 0, 0):c) vp vc p
--    putStrLn ">c"
    p <- if (a == Nothing)
         then putStrLn "NO"
         else let (Just (t, (x, y, z))) = a
              in printf "YES\n%.6f\n%.6f %.6f %.6f" t x y z
    return (0)

import IO
import Text.Printf

toList (a, b, c) = a:b:c:[]
fList (a:b:c:_) = (a, b, c)

delta :: (Double, Double, Double) -> (Double, Double, Double) -> Double
delta (x1, y1, z1) (x2, y2, z2) = sqrt ((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)

deltaPlus :: (Double, Double, Double) -> (Double, Double, Double) -> Double -> (Double, Double, Double)
deltaPlus (x1, y1, z1) (x2, y2, z2) r =
    let (dx, dy, dz) = (x2 - x1, y2 - y1, z2 - z1)
    in  (x1 + dx*r, y1 + dy*r, z1 + dz*r)

coord = do
           line <- getLine
           --putStrLn (">" ++ (show (words line)))
           return (fList (map (read :: String -> Double) (words line)))

makeList 0 _ _ = return []
makeList n a p = do
                 c <- coord
                 other <- makeList (n-1) (a + delta c p) c
                 return ((c, a + delta c p, delta c p):other)

binSearch :: (Double -> (Int, a)) -> Double -> Double -> IO (Double, a)
binSearch f min max | max == min = error ("bin search: min == max")
binSearch f min max | otherwise = do
        --putStrLn (show (min, max, (max+min)/2))
        case (f ((max+min)/2)) of
             (-1, _) -> binSearch f min ((max + min)/2)
             (0, cx) -> return ((max + min)/2, cx)
             (1, _) -> binSearch f ((max + min)/2) max

bsf c0 c1 vp vc p t0 t =
    let d  = delta c0 c1
        cx = deltaPlus c0 c1 ((t - t0) / (d/vc))
        tn = (delta cx p) / vp
    in if (abs (tn - t)) < ((0.1)^(9))
       then (0, cx)
       else if (tn - t) < 0
            then (-1, cx)
            else (1, cx)

ff (c:[]) _ _ _ = return Nothing
ff ((c0, preLen, _):cc@(c1, preLen1, len):cs) vp vc p =
        if ((preLen1 / vc) - ((delta p c1) / vp)) >= -((0.1)^(7))
        then do r <- (binSearch (bsf c0 c1 vp vc p (preLen/vc)) (preLen/vc) (preLen1/vc))
                return (Just r)
        else ff (cc:cs) vp vc p

main = do
    n <- (getLine >>= return . read) :: IO Int
    c0 <- coord
    c <- makeList n 0 c0
    l <- getLine
    let (l1:l2:ll) = words l
        ivp = read l1 :: Int
        ivc = read l2 :: Int
        vp = realToFrac ivp
        vc = realToFrac ivc
    p <- coord
    a <- ff ((c0, 0, 0):c) vp vc p
    p <- if (a == Nothing)
         then putStrLn "NO"
         else let (Just (t, (x, y, z))) = a
              in printf "YES\n%.7f\n%.7f %.7f %.7f" t x y z
    return (0)


import Data.List

divNum :: Integer -> Integer -> Integer
divNum 0 _ = 0
divNum n k = mod n k + 1 + divNum (div n k) k

solve :: [Integer] -> String
solve [n, k] = show ((divNum n k) - 1)

main = interact $ intercalate "\n" .  map solve .  map (map (\x -> read x :: Integer) .  words) . tail . lines

import Data.Int
main = interact $ unlines . map (show . solve) . pairs . map read . tail . words
pairs (a:b:xs) = (a,b) : pairs xs
pairs [] = []
solve (0,_) = 0
solve (n,k) | n `mod` k == 0 = 1 + solve (n `div` k,k)
            | otherwise = n `mod` k + solve (n - n`mod` k,k)

{-# LANGUAGE BangPatterns #-}

import Control.Monad

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
      [n, k] <- map read.words <$> getLine
      print $ solve n k

solve :: Integer -> Integer -> Integer
solve n k = go 0 n
  where
    go !res x
        | x == 0 = res
        | r == 0 = go (res + 1) (quot x k)
        | otherwise = go (res + r) (x - r)
      where
        !r = rem x k

module Main where

import Control.Monad
import System.IO
import Data.Char
import Data.Array
import qualified Data.Map as Map
import Data.List

isDivisible :: Integer -> Integer -> Bool
isDivisible a b
    | b == 0 = False
    | a < b = False
    | (a `mod` b) == 0 = True
    | otherwise = False

stepsToZero :: Integer -> Integer -> Integer -> Integer
stepsToZero k n count
    | n == 0 = count
    | otherwise =
        if isDivisible n k
            then stepsToZero k (n `div` k) (count + 1)
            else
               let delta = n `mod` k
               in stepsToZero k (n - delta) (count + delta)

main :: IO()
main = do

    q <- readLn :: IO Int
    forM_ [1..q] $ \q_itr -> do
        s <- getLine
        let ws = words s
        let nums = map read ws :: [Integer]
        let (n:k:_) = nums

        let result = stepsToZero k n 0
        putStrLn $ show result

import Data.List

divNum :: Integer -> Integer -> Integer
divNum 0 _ = 0
divNum n k = mod n k + 1 + divNum (div n k) k

solve :: [Integer] -> String
solve [n, k] = show  (divNum n $ k - 1)

main = interact $ intercalate "\n" .  map solve .  map (map (\x -> read x :: Integer) .  words) . tail . lines


solve :: Integer -> Bool
solve n = elem 90 $ map (\k -> mod (k*180 - ((k*360) `div` n)) 180) $ map (\q -> q*n `div` 360) $ filter (\q -> (mod (q * n) (360)) == 0) [1..359]
--solve n = elem 0 $ map (`mod` 4) [n, 2*n, 3*n]
{--solve n = let ks = [(ceiling (4 / fromIntegral(n)))..(floor (4- (4 / fromIntegral(n))))] in
    elem 0 $ map (\x -> (x*n) `mod` 4) ks--}
--solve n = 0 `elem` map (\x -> (4*x) `mod` n) [1..(n-1)]
--solve n = elem 0 (map (`mod` n) (map (*4) ([1..(n-1)])))

getInt::IO Integer
getInt=do
    line <- getLine
    let i = read line :: Integer
    return (i)

main::IO()
main=do
    t <- getInt
    for t where
        for 0 = return ()
        for t = do
            n <- getInt
            if solve n then putStrLn "YES" else putStrLn "NO"
            for (t-1)

module Main where

main = interact $ unlines . map (\x -> if  (read x :: Int) `mod` 4 == 0 then "YES" else "NO" ) .   tail . lines

repeatNTimes 0 _ = return ()
repeatNTimes n action =
 do
  action
  repeatNTimes (n-1) action

main = do
    input1 <- getLine
    let t = read input1 :: Int

    --main cycle
    repeatNTimes t (do
        input2 <- getLine
        let n = read input2 :: Int

        if mod n 4 == 0 then putStrLn "YES" else putStrLn "NO"
        )

import Control.Monad (replicateM_)
main = readLn >>= flip replicateM_ (readLn >>= putStrLn . f) where
    f x | x `mod` 4 == 0 = "YES"
    f _ = "NO"

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}

import Data.List
import Data.Tuple

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

split = _split [] ""

(|>) = flip ($)

main = do
  input <- getContents
  let _:tokens = read <$> split input :: [Integer]
      res = tokens |> unfoldr (\case
        []  -> Nothing
        n:rest  -> Just (ans, rest) where
          ans = if subres then "YES" else "NO"
          subres = (90*n `mod` 360 == 0)
        )
  sequence_ $ putStrLn <$> res

solve :: Integer -> String
solve n= if mod n 4 == 0 then "YES" else "NO"

parse :: String -> Integer
parse = read

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

solveCase :: Int -> IO()
solveCase 0 = return ()
solveCase t = do
  test <- getLine
  putStrLn . solve . parse  $ test
  solveCase (t-1)

getInt :: IO Int
getInt = fmap read getLine

main :: IO()
main = do
  n <- getInt
  solveCase n

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

process a | mod a 4 ==0 = "YES"
	  | otherwise = "NO"

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

main = interact $ unlines . map process . map read . tail . lines
process x
    | mod x 4 == 0 = "YES"
    | otherwise = "NO"

solve :: String -> String
solve s
  | mod (read s) 4 == 0 = "YES"
  | otherwise = "No"

io :: String -> String
io s = unlines $ map solve $ drop 1 $ lines s

main :: IO ()
main = do
  interact io

solve :: Int -> Bool
solve n = let ks = [(ceiling (4 / fromIntegral(n)))..(floor (4- (4 / fromIntegral(n))))] in
    elem 0 $ map (\x -> (x*n) `mod` 4) ks
--solve n = 0 `elem` map (\x -> (4*x) `mod` n) [1..(n-1)]
--solve n = elem 0 (map (`mod` n) (map (*4) ([1..(n-1)])))

getInt::IO Int
getInt=do
    line <- getLine
    let i = read line :: Int
    return (i)

main::IO()
main=do
    t <- getInt
    for t where
        for 0 = return ()
        for t = do
            n <- getInt
            if solve n then putStrLn "YES" else putStrLn "NO"
            for (t-1)

getInt::IO Int
getInt=do
    line <- getLine
    let i = read line :: Int
    return (i)

main::IO()
main=do
    t <- getInt
    for t where
        for 0 = return ()
        for t = do
            n <- getInt
            if n `mod` 2 == 0 then putStrLn "YES" else putStrLn "NO"
            for (t-1)

solve :: Int -> Bool
solve n = elem 90 $ map (\k -> mod (k*180 - ((k*360) `div` n)) 180) $ map (\q -> q*n `div` 360) $ filter (\q -> (mod (q * n) (360)) == 0) [1..359]
--solve n = elem 0 $ map (`mod` 4) [n, 2*n, 3*n]
{--solve n = let ks = [(ceiling (4 / fromIntegral(n)))..(floor (4- (4 / fromIntegral(n))))] in
    elem 0 $ map (\x -> (x*n) `mod` 4) ks--}
--solve n = 0 `elem` map (\x -> (4*x) `mod` n) [1..(n-1)]
--solve n = elem 0 (map (`mod` n) (map (*4) ([1..(n-1)])))

getInt::IO Int
getInt=do
    line <- getLine
    let i = read line :: Int
    return (i)

main::IO()
main=do
    t <- getInt
    for t where
        for 0 = return ()
        for t = do
            n <- getInt
            if solve n then putStrLn "YES" else putStrLn "NO"
            for (t-1)

module Main where

main = interact $ unlines . map (\x -> let v = 90 / (360 / (read x :: Double) ) in if (v - (fromIntegral $ floor v )) == 0.0 then "YES" else "NO" ) .   tail . lines

module Main where

main = interact $ unlines . map (\x -> let v = map (/ (360 / (read x :: Double))) [90,270] in if (any (==0.0) $ zipWith (-) v $ map (fromIntegral .floor) v ) then "YES" else "NO" ) .   tail . lines

solve :: Integer -> String
solve n= if mod n 2 == 0 then "YES" else "NO"

parse :: String -> Integer
parse = read

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

solveCase :: Int -> IO()
solveCase 0 = return ()
solveCase t = do
  test <- getLine
  putStrLn . show . solve . parse  $ test
  solveCase (t-1)

getInt :: IO Int
getInt = fmap read getLine

main :: IO()
main = do
  n <- getInt
  solveCase n

solve :: Integer -> String
solve n= if mod n 2 == 0 then "YES" else "NO"

parse :: String -> Integer
parse = read

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

solveCase :: Int -> IO()
solveCase 0 = return ()
solveCase t = do
  test <- getLine
  putStrLn . solve . parse  $ test
  solveCase (t-1)

getInt :: IO Int
getInt = fmap read getLine

main :: IO()
main = do
  n <- getInt
  solveCase n

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

process a | div a 4 ==0 = "YES"
	  | otherwise = "NO"

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


main = do
    n <- getLine
    xs <- (map read . words) `fmap` getLine :: IO [Int]
    print $ (length . filter localExtrema) (zip3 xs (tail xs) (tail (tail xs)))

localExtrema (x,y,z) = (x < y && y > z) || (x > y && y < z)

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

process n [a] = n
process n [a,b] = n
process n (a:b:c:ds) | a<b && c<b = process (n+1) (b:c:ds)
		     | a>b && c>b = process (n+1) (b:c:ds)
		     | otherwise = process n (b:c:ds)

main = do
		n<-read <$> getLine ::IO Int
		s<- map read <$> words <$> getLine ::IO [Int]
		print $ process 0 s

module Main where
main = getContents >>= print . exec
exec = solve . tail . map read . words
solve [_] = 0
solve [_, _] = 0
solve xs = sum $ zipWith3 (\a b c -> fromEnum $ (a < b && b > c) || (a > b && b < c)) xs (tail xs :: [Int]) (tail $ tail xs)

solve :: [Int] -> [Int]
solve (x:y:z:xs)
    | y < x && y < z || y > x && y > z = y : (solve (y:z:xs))
    | otherwise = solve (y:z:xs)
solve _ = []

main = do
    getLine
    getContents >>= print.length.solve.(map read).words

module Main where
  import Data.List
  import Control.Monad
  import Data.Array
  import qualified Data.Set as S

  toArray :: [a] -> Array Int a
  toArray xs =
    let ls = length xs - 1
        in array (0, ls) $ zip [0..ls] xs

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

  readMultilineInput :: Int -> (String -> IO a) -> IO [a]
  readMultilineInput n transformer = replicateM n (getLine >>= transformer)

  elemSet :: (Ord a) => a -> S.Set a -> Bool
  elemSet elem sets =
    let found = elem `S.lookupIndex` sets
        in toBool found
        where
          toBool :: Maybe Int -> Bool
          toBool (Just _) = True
          toBool Nothing = False

  localExtreme :: (Int, Int, Int) -> Bool
  localExtreme (a, b, c)
    | a < b && b > c = True
    | a > b && b < c = True
    | otherwise = False

  reduceExtreme :: Int -> [Int] -> Int
  reduceExtreme i (a:b:c:xs)
    | localExtreme (a, b, c) = reduceExtreme (i + 1) (b:c:xs)
    | otherwise = reduceExtreme i (b:c:xs)
  reduceExtreme i (b:c:[]) = i
  reduceExtreme i (c:[]) = i
  reduceExtreme i [] = i

  test :: IO()
  test = do
    let xs = []
    print $ reduceExtreme 0 xs

  main :: IO()
  main = do
    n <- getLine
    xs <- getLine >>= return . (map readInt) . words
    print $ reduceExtreme 0 xs

solve :: [Int] -> [Int]
solve (x:y:z:xs)
    | y < x && y < z || y > x && y > z = y : (solve (y:z:xs))
    | otherwise = solve (z:xs)
solve _ = []

main = do
    getLine
    getContents >>= print.length.solve.(map read).words


--216C

import Control.Monad (liftM)

solve :: [Int] -> [Int]
solve [n, m, 1]  =  [1, n .. n + m]
solve [n, m, k]  =
    replicate k 1
    ++
    (takeWhile (<= (n + m)) $ concatMap (\i -> i : replicate (k-1) (i+1)) [n, 2*n ..])

prints :: Show a => [a] -> IO ()
prints xs  =  print (length xs) >> prints' xs
  where
    prints' []  =  return ()
    prints' [x]  =  print x
    prints' (x:xs)  =  putStr (show x ++ " ") >> prints' xs

main :: IO ()
main  =  liftM (map read . words) getLine >>= prints . solve

gao n m k = takeWhile (<=m) $ concat $ iterate next $ all 1
  where
    all = replicate k
    next a = zipWith (+) (n-1: repeat n) $ all $ maximum a

main = do
  [n, m, k] <- fmap (map read . words) getLine
  let a = gao n (n+m) k
  print $ length a
  putStrLn $ unwords $ map show a

main=getLine>>=s.map read.words
p=putStrLn.unwords.map show
s[n,m,k]
 | m+2<=n = do
     print $ 2*k
     p $ replicate k 1 ++ replicate k n
 | m+1==n = do
     print $ k+1+max 1 (k-1)
     p $ replicate k 1 ++ [n] ++ replicate (max 1 $ k-1) (n+1)
 | n==2 = do
     print $ k+2+max 1 (k-1)
     p $ replicate k 1 ++ [n] ++ replicate (max 1 $ k-1) (n+1) ++ [n+2]
 | m==n = do
     print $ k+2+max 0 (k-1)
     p $ replicate k 1 ++ [n] ++ replicate (max 0 $ k-1) (n+1) ++ [n+2]

gao n m k = concat $ takeWhile ((<=m) . maximum) $ iterate next $ all 1
  where
    all = replicate k
    next a = zipWith (+) (n-1: repeat n) $ all $ maximum a

main = do
  [n, m, k] <- fmap (map read . words) getLine
  let a = gao n (n+m) k
  print $ length a
  putStrLn $ unwords $ map show a

main=getLine>>=s.map read.words
p=putStrLn.unwords.map show
s[n,m,k]
 | m+2<=n = do
     print $ 2*k
     p $ replicate k 1 ++ replicate k n
 | m+1==n = do
     print $ k+2+max 0 (k-2)
     p $ replicate k 1 ++ [n] ++ replicate (max 1 $ k-1) (n+1)
 | m==n = do
     print $ k+2+max 0 (k-2)
     p $ replicate k 1 ++ [n] ++ replicate (max 0 $ k-1) (n+1) ++ [n+2]

main=getLine>>=s.map read.words
p=putStrLn.unwords.map show
s[n,m,k]
 | m+2<=n = do
     print $ 2*k
     p $ replicate k 1 ++ replicate k n
 | m+1==n = do
     print $ k+2+max 0 (k-2)
     p $ replicate k 1 ++ [n] ++ replicate (max 1 $ k-1) (n+1)
 | m==n = do
     print $ k+2+max 0 (k-2)
     p $ replicate k 1 ++ [n] ++ replicate (max 0 $ k-2) (n+1) ++ [n+2]

main=getLine>>=s.map read.words
p=putStrLn.unwords.map show
s[n,m,k]
 | m+2<=n = do
     print $ 2*k
     p $ replicate k 1 ++ replicate k n
 | m+1==n = do
     print $ k+2+max 0 (k-2)
     p $ replicate k 1 ++ [n] ++ replicate (max 1 $ k-1) (n+1)
 | m==n = do
     print $ k+2+max 0 (k-1)
     p $ replicate k 1 ++ [n] ++ replicate (max 0 $ k-1) (n+1) ++ [n+2]


import Data.List

main = interact $ show . sol . words

sol (_:k:str:_) = ans (read k) $ cal str

cal = reverse . sort . map (toInteger.length) . group . sort

ans 0 _  = 0
ans _ [] = 0
ans k (x:xs)
    | k >= x = x*x + ans (k-x) xs
    | otherwise = k*k

import Data.List (genericLength, group, sort, sortBy)

main :: IO ()
main = getLine >>= \s -> getLine >>= print . solve (map read (words s) !! 1)

solve :: Integer -> String -> Integer
solve k = solve' k . sortBy (flip compare) . map genericLength . group . sort

solve' :: Integer -> [Integer] -> Integer
solve' _ [] = 0
solve' m (x:xs) | m <= x = m * m
                | otherwise = x * x + solve' (m - x) xs

import Control.Applicative
import Data.List

main :: IO ()
main = do
  [_, k] <- map read <$> words <$> getLine
  c <- getLine
  putStrLn $ show $ solve k c

solve :: Integer -> [Char] -> Integer
solve k c = ans
  where
    cs  = reverse . sort . map (fromIntegral . length) .  group . sort $ c
    ans = snd $ foldl' (\(l,a) x -> if l < x then (0, a+l*l) else (l-x,a+x*x) ) (k,fromIntegral 0) cs

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

main = interact $ show . getAns . tail . words

getAns :: [String] -> Integer
getAns (k:xs:_) = calMax (read k) $ (sort . map (\xs -> ((toInteger . negate . length) xs, head xs)) . group . sort) xs

calMax :: Integer -> [(Integer, Char)] -> Integer
calMax _ [] = 0
calMax k ((n, c):xs)
	| k > negate n = calMax (k + n) xs + n * n
	| otherwise = k * k

import Data.List (group, sort, sortBy)

main :: IO ()
main = getLine >>= \s -> getLine >>= print . solve (map read (words s) !! 1)

solve :: Int -> String -> Int
solve k = solve' k . sortBy (flip compare) . map length . group . sort

solve' :: Int -> [Int] -> Int
solve' _ [] = 0
solve' m (x:xs) | m <= x = m * m
                | otherwise = x * x + solve' (m - x) xs

import Control.Applicative
import Data.List

main :: IO ()
main = do
  [_, k] <- map read <$> words <$> getLine
  c <- getLine
  putStrLn $ show $ solve k c

solve :: Int -> [Char] -> Int
solve k c = ans
  where
    cs  = reverse . sort . map length .  group . sort $ c
    ans = snd $ foldl' (\(l,a) x -> if l < x then (0, a+l*l) else (l-x,a+x*x) ) (k,0) cs


import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.Int
import Data.List
import Data.Maybe
import Data.Ratio
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Int64
md = 998244353

newtype MInt = MInt Int64 deriving Eq

instance Show MInt where
    show (MInt a) = show a

instance Num MInt where
    (MInt a) + (MInt b) =
        let r = a + b in
            MInt $ if r >= md then r - md else r
    (MInt a) * (MInt b) = MInt $ (a * b) `mod` md
    abs = id
    signum (MInt a) = MInt $ signum a
    fromInteger a = MInt $ fromInteger (a `mod` fromIntegral md)
    negate (MInt 0) = 0
    negate (MInt a) = MInt $ md - a

instance Fractional MInt where
  recip a = a ^ (md - 2)
  fromRational x =
        let a = numerator x `mod` fromIntegral md
            b = denominator x `mod` fromIntegral md
        in
            MInt (fromInteger a) / MInt (fromInteger b)

solve :: [Int] -> MInt
solve xs = (product . map fromIntegral) (zipWith (\x i -> (x - i) `max` 0) xs [1..])

main = do
    [n, m] <- map parseInt . BS.words <$> BS.getLine
    xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    let f = (product . map fromIntegral) [1..n] :: MInt
        fi = recip f
        r = sum (map ((f-) . solve) xss) * fi
    print r

import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.Int
import Data.List
import Data.Maybe
import Data.Ratio
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Integer
md = 998244353

newtype MInt = MInt Integer deriving Eq

instance Show MInt where
    show (MInt a) = show a

instance Num MInt where
    (MInt a) + (MInt b) =
        let r = a + b in
            MInt $ if r >= md then r - md else r
    (MInt a) * (MInt b) = MInt $ (a * b) `mod` md
    abs = id
    signum (MInt a) = MInt $ signum a
    fromInteger a = MInt $ fromInteger (a `mod` fromIntegral md)
    negate (MInt 0) = 0
    negate (MInt a) = MInt $ md - a

instance Fractional MInt where
  recip a = a ^ (md - 2)
  fromRational x =
        let a = numerator x `mod` fromIntegral md
            b = denominator x `mod` fromIntegral md
        in
            MInt (fromInteger a) / MInt (fromInteger b)

solve :: [Int] -> MInt
solve xs = (product . map fromIntegral) (zipWith (\x i -> (x - i) `max` 0) xs [1..])

main = do
    [n, m] <- map parseInt . BS.words <$> BS.getLine
    xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    let f = (product . map fromIntegral) [1..n] :: MInt
        r = sum (map ((f-) . solve) xss) / f
    print r

import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.Int
import Data.List
import Data.Maybe
import Data.Ratio
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Int64
md = 998244353

newtype MInt = MInt Int64 deriving Eq

instance Show MInt where
    show (MInt a) = show a

instance Num MInt where
    (MInt a) + (MInt b) =
        let r = a + b in
            MInt $ if r >= md then r - md else r
    (MInt a) * (MInt b) = MInt $ (a * b) `mod` md
    abs = id
    signum (MInt a) = MInt $ signum a
    fromInteger a = MInt $ fromInteger (a `mod` fromIntegral md)
    negate (MInt 0) = 0
    negate (MInt a) = MInt $ md - a

instance Fractional MInt where
  recip a = a ^ (md - 2)
  fromRational x =
        let a = numerator x `mod` fromIntegral md
            b = denominator x `mod` fromIntegral md
        in
            MInt (fromInteger a) / MInt (fromInteger b)

solve :: [Int] -> MInt
solve xs = (product . map fromIntegral) (zipWith (\x i -> (x - i) `max` 0) xs [1..])

main = do
    [n, m] <- map parseInt . BS.words <$> BS.getLine
    xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    let f = (product . map fromIntegral) [1..n] :: MInt
        r = sum (map ((f-) . solve) xss) / f
    print r

import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.Int
import Data.List
import Data.Maybe
import Data.Ratio
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Int64
md = 998244353

newtype MInt = MInt Int64 deriving (Eq)

instance Show MInt where
    show (MInt a) = show a

instance Num MInt where
    (MInt a) + (MInt b) =
        let r = a + b
            rr = if r >= md then r - md else r
        in
            MInt rr
    (MInt a) * (MInt b) = MInt $ (a * b) `mod` md
    abs = id
    signum (MInt a) = MInt $ signum a
    fromInteger a = MInt $ fromInteger (a `mod` fromIntegral md)
    negate (MInt 0) = 0
    negate (MInt a) = MInt $ md - a

instance Fractional MInt where
  recip a = a ^ (md - 2)
  fromRational x =
        let a = numerator x `mod` fromIntegral md
            b = denominator x `mod` fromIntegral md
        in
            MInt (fromInteger a) / MInt (fromInteger b)

solve :: [Int] -> MInt
solve xs = (product . map fromIntegral) (zipWith (\x i -> (x - i) `max` 0) xs [1..])

main = do
    [n, m] <- map parseInt . BS.words <$> BS.getLine
    xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    let f = (product . map fromIntegral) [1..n] :: MInt
        fi = recip f
        r = sum (map ((f-) . solve) xss) * fi
    print r

import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.Int
import Data.List
import Data.Maybe
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Int64
md = 998244353

add :: Int64 -> Int64 -> Int64
add a b =
    let
        r = a + b
    in
        if r >= md then
            r - md
        else
            r

sub :: Int64 -> Int64 -> Int64
sub a b =
    let
        r = a - b
    in
        if r < 0 then
            r + md
        else
            r

mul :: Int64 -> Int64 -> Int64
mul a b = (a * b) `mod` md

pm :: Int64 -> Int64 -> Int64
pm a 0 = 1
pm a e =
    let
        r = pm a (e `div` 2)
        rr = r `mul` r
    in
        if even e then
            rr
        else
            rr `mul` a

inv :: Int64 -> Int64
inv a = pm a (md - 2)

solve :: [Int] -> Int64
solve xs = foldr1 mul (zipWith (\x i -> (x - i) `max` 0) (map fromIntegral xs) [1..])

main = do
    [n, m] <- map parseInt . BS.words <$> BS.getLine
    xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    let f = foldr1 mul [1..(fromIntegral n)]
        fi = inv f
        r = foldr1 add (map ((f `sub`) . solve) xss) `mul` fi
    print r

import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.List
import Data.Maybe
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Int
md = 998244353

add :: Int -> Int -> Int
add a b =
    let
        r = a + b
    in
        if r >= md then
            r - md
        else
            r

sub :: Int -> Int -> Int
sub a b =
    let
        r = a - b
    in
        if r < 0 then
            r + md
        else
            r

mul :: Int -> Int -> Int
mul a b = (a * b) `mod` md

pm :: Int -> Int -> Int
pm a 0 = 1
pm a e =
    let
        r = pm a (e `div` 2)
        rr = r `mul` r
    in
        if even e then
            rr
        else
            rr `mul` a

inv :: Int -> Int
inv a = pm a (md - 2)

solve :: [Int] -> Int
solve xs = foldr1 mul (zipWith (\x i -> (x - i) `max` 0) xs [1..])

main = do
    print (maxBound::Int)
    -- [n, m] <- map parseInt . BS.words <$> BS.getLine
    -- xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    -- let f = foldr1 mul [2..n]
    --     fi = inv f
    --     r = foldr1 add (map ((f `sub`) . solve) xss) `mul` fi
    -- print r

import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bits
import Data.List
import Data.Maybe
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

md :: Int
md = 998244353

add :: Int -> Int -> Int
add a b =
    let
        r = a + b
    in
        if r >= md then
            r - md
        else
            r

sub :: Int -> Int -> Int
sub a b =
    let
        r = a - b
    in
        if r < 0 then
            r + md
        else
            r

mul :: Int -> Int -> Int
mul a b = (a * b) `mod` md

pm :: Int -> Int -> Int
pm a 0 = 1
pm a e =
    let
        r = pm a (e `div` 2)
        rr = r `mul` r
    in
        if even e then
            rr
        else
            rr `mul` a

inv :: Int -> Int
inv a = pm a (md - 2)

solve :: [Int] -> Int
solve xs = foldr1 mul (zipWith (\x i -> (x - i) `max` 0) xs [1..])

main = do
    [n, m] <- map parseInt . BS.words <$> BS.getLine
    xss <- fmap (map sort . transpose) . replicateM n $ map parseInt . BS.words <$> BS.getLine
    let f = foldr1 mul [2..n]
        fi = inv f
        r = foldr1 add (map ((f `sub`) . solve) xss) `mul` fi
    print r


import Control.Monad (replicateM, replicateM_)

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    [n, _m] <- map read . words <$> getLine
    a <- replicateM n getLine
    let lastColIssues = length [() | conv <- map last a, conv == 'R']
        lastRowIssues = length [() | conv <- last a,     conv == 'D']
    print (lastColIssues + lastRowIssues)

readInts :: IO [Int]
readInts = do
    line <- getLine
    return $ map (read) $ words line

readInt :: IO Int
readInt = do
    line <- getLine
    return $ read line

getNLines :: Int -> IO [String]
getNLines 0 = return []
getNLines n = do
    line <- getLine
    lines' <- getNLines (n-1)
    return $ line : lines'

countR :: [String] -> Int
countR [] = 0
countR (line:other) = (if (head $ reverse line) == 'R' then 1 else 0) + (countR other)

countD :: [String] -> Int
countD matrix = let work = last matrix in foldl (\acc x -> acc + (if x == 'D' then 1 else 0)) 0 work

main :: IO ()
main = do
    t <- readInt
    for t where
        for 0 = return ()
        for i = do
            [n,_] <- readInts
            matrix <- getNLines n
            putStrLn $ show $ countR matrix + countD matrix
            for (i-1)

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

main = getLine >>= flip replicateM_ test . read

test = do
  [n, _] <- map read . words <$> getLine
  as <- replicateM n getLine
  print $ length (filter (== 'R') (last <$> as)) + length (filter (== 'D') (last as))

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

onecase = do
		[x,y]<- map read <$> words <$> getLine ::IO [Int]
		z<-replicateM x getLine
		let a1= length $ filter (=='R') $ map last z
		let a2 = length $ filter (=='D') $ last z
		print $ a1+a2

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


--le me trying to write a reading function
import Control.Monad (mfilter)

--am facut citirea de mana si nu da rasp corect decat pt cifre
citesc_nr :: String -> Int
citesc_nr s = read s
citesc_array :: [String] -> [Int]
citesc_array = map read
main = do
    x <- getLine
    let n = (citesc_nr x)
    --putStrLn (x)
    --vector <- read `fmap` getLine :: IO Int
    vector <- getLine
    --print vector
    --let i = (citesc_nr (takeWhile (/= '\n') vector) )
    --let y = filter (/=' ') vector
    let y = words vector

    --let replace a b = map $ maybe b id . mfilter (/= a) . Just
    --let y = replace ' ' '3' vector
    --let despart c = c:[]
    --let f x = map despart x
    --let array = f y
    let int_array = citesc_array y
    print $ solve int_array
    --print y

solve(x:xs) = dp xs x 1 1 where
    dp [] pred current ans = max current ans
    dp (curr:xs) pred current ans
        | curr > pred = dp xs curr (current+1) ans
        | otherwise = dp xs curr 1 (max current ans)

{--main :: IO ()
main = do
    line <- getLine
    if null line
        then return ()
        else do
            putStrLn $ reverseWords line
            main

reverseWords :: String -> String
reverseWords = unwords . map reverse . words
--}

main :: IO ()
main = do
    _ <-getLine
    inp <- getLine
    let nums = map read (words inp) :: [Int]
    print $ maximum $ map length $ doTheThing nums

doTheThing :: [Int] -> [[Int]]
doTheThing = foldl makeNum [[]]

makeNum :: [[Int]] -> Int -> [[Int]]
makeNum [[]] num = [[num]]
makeNum nums num =
    if head (head nums) < num
        then let newFirstNums = num : head nums in newFirstNums : tail nums
        else [num] : nums

import Data.List

toint :: String -> Int
toint = read

main = getLine >> getLine >>= print . solve . map toint . words

solve a = succ . maximum . (0 : ) . map length . filter head . group . zipWith (<) a $ tail a

import Control.Applicative
import Data.List

third (_, _, c) = c

solve a =
    third $ foldl' f (-1, 0, 0) a
    where
        f (prev, cont, maxv) curr =
            if prev < curr then
                (curr, cont + 1, max maxv (cont + 1))
            else
                (curr, 1, max maxv 1)

main = do
    _ <- getLine
    a <- map read . words <$> getLine :: IO [Int]
    print $ solve a

import Data.Function
import Data.List
main = interact $ show . solve . map read . tail . words
solve xs = succ $ maximum $ (0 :) $ map length $ filter ((> 0) . head) $ group $ map signum $ zipWith (-) (tail xs) xs

main = getContents >>= print . solve 1 1 . map read . tail . words

solve :: Int -> Int -> [Int] -> Int
solve x y [a0] = max x y
solve x y (a0:a1:as) = if a0 < a1
                       then solve x (y + 1) (a1:as)
                       else solve (max x y) 1 (a1:as)

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

process m n _ [] = max m n
process m n l (a:as) | a>l = process m (n+1) a as
                     | otherwise = process (max m n) 1 a as

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

import Data.Maybe
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInt = read `fmap` getLine :: IO Int
getInts = (map (fst . fromJust . BS8.readInt) . BS8.words) `fmap` BS.getLine :: IO [Int]

main = do
    n <- getInt
    xs <- getInts
    print $ solve xs

solve (x:xs) = f xs x 1 1 where
    f [] prev cnt maxCnt = max cnt maxCnt
    f (curr:xs) prev cnt maxCnt
        | curr > prev = f xs curr (cnt+1) maxCnt
        | otherwise = f xs curr 1 (max cnt maxCnt)

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

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import qualified Data.ByteString.Char8 as B
import Text.Printf

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

getList = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

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

main = getLine >> getList >>= print . succ . maximum . ( 0 : ) . map length . filter head . group . f

f [] = []
f [a] = []
f ( a : b : l ) = ( a < b ) : f ( b : l )

-- ttt lst len mx
--   | (length lst) == 1 = mx
--   | (head lst) < (head (tail lst)) = ttt (tail lst) (succ len) (maximum [(succ len), mx])
--   | otherwise = ttt (tail lst) 1 mx

ttt (a : b : cc) len mx
  | a < b = ttt (b : cc) (succ len) (maximum [(succ len), mx])
  | otherwise = ttt (b : cc) 1 mx
ttt (a : bb) len mx = mx

conv (a : bb) = ((read a) :: Integer) : (conv bb)
conv mpt = []

main = do
  ff <- getLine
  gg <- getLine
  let aa = (conv (words gg))

      ans = ttt aa 1 1

  putStrLn (show ans)

import Data.List

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

f :: [Int] -> Int
f (n:as) = 1 +
    case filter head . group . zipWith (<) as $ tail as of
        [] -> 0
        xs -> maximum . map length $ xs

{-# LANGUAGE BangPatterns #-}

import Control.Applicative

main = do
    getLine
    arr <- foldr(\n acc -> (read n :: Int) : acc) [] . words <$> getLine
    print $ func arr (1, 1)

func :: [Int] -> (Int, Int) -> Int
func (x:y:s) !(cr, mx)
    | y > x = func (y:s) (cr + 1, mx)
    | otherwise = func (y:s) (1, max cr mx)
func _ !(cr, mx) = max cr mx

import Control.Applicative

main = do
    getLine
    arr <- foldr (\n acc -> (read n :: Int) : acc) [] . words <$> getLine
    print $ func arr (1, 1)

func :: [Int] -> (Int, Int) -> Int
func (x:y:s) (cr, mx)
    | y > x = func (y:s) (cr + 1, mx)
    | otherwise = func (y:s) (1, max cr mx)
func _ (cr, mx) = max cr mx

import Control.Applicative
import Data.Foldable

main = do
    getLine
    arr <- foldr'(\n acc -> (read n :: Int) : acc) [] . words <$> getLine
    print $ func arr (1, 1)

func :: [Int] -> (Int, Int) -> Int
func (x:y:s) (cr, mx)
    | y > x = func (y:s) (cr + 1, mx)
    | otherwise = func (y:s) (1, max cr mx)
func _ (cr, mx) = max cr mx

main = do
  n <- getLine
  l <- getLine
  let a = readInt l
  putStrLn . show . get1st $ foldl f (0, 0, 0) a
    where f (ans, cur, num) x = if num < x
            then (max ans (cur + 1), cur + 1, x)
            else (ans, 1, x)
          get1st (x, _, _) = x

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

import Control.Applicative
import Data.List

main = interact $ show . (+ 1) . maximum . (0 :) . map length . filter (id . head) . group . (zipWith (<) <$> id <*> tail) . map (read :: String -> Int) . tail . words

import Data.List
import Data.Int
import Data.Maybe
import Data.Char
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
main=   readsolveprint
readsolveprint::IO()
readsolveprint = interact $ solve. concat. map (map read.words).take 2.lines
solve :: [Int]-> String
solve (x:xs) = show $ (\ (a,b,c)->a) $foldl f (1,1, head xs) xs
  where
  f (b1,b2,b3) a | a<=b3              = (b1, 1, a)
                 | a>b3 && b2+1 > b1 =  (b2+1, b2+1, a)
                 | otherwise         =  (b1, b2+1, a)

import Control.Applicative
import Data.List

main = interact $ show . maximum . (0 :) . map ((+ 1) . length) . filter (id . head) . group . (zipWith (<) <$> id <*> tail) . map (read :: String -> Int) . tail . words

import Data.Function
import Data.List
main = interact $ show . solve . map read . tail . words
solve xs = succ $ maximum $ (0 :) $ map length $ filter ((/= 0) . head) $ group $ map signum $ zipWith (-) (tail xs) xs


import Control.Monad (replicateM_)
main = read <$> getLine >>= flip replicateM_ scase
scase = read <$> getLine >>=
        \n -> map read . words <$> getLine >>=
        \as -> putStrLn $ if (possible as n) then "YES" else "NO"

possible :: [Int] -> Int -> Bool
possible as n = any (f (sum as) n) as
  where f :: Int -> Int -> Int -> Bool
        f s n e = (s-e) == (n-1) * e

-- https://codeforces.com/contest/1679/problem/D
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DatatypeContexts #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict #-}
{-# LANGUAGE TupleSections #-}

import Control.Arrow ((>>>))
import Control.Monad (
    MonadPlus (mplus),
    foldM_,
    forM_,
    guard,
    join,
    replicateM,
    replicateM_,
 )
import qualified Control.Monad.ST.Strict as ST
import qualified Control.Monad.State.Strict as S

import qualified Data.Array as A
import qualified Data.Array.MArray.Safe as MA
import qualified Data.Array.ST.Safe as STA
import qualified Data.Array.Unboxed as UA

import Data.Bits
import qualified Data.ByteString.Char8 as B8
import Data.Char
import Data.Int

import Data.Function (on)
import Data.Functor ((<&>))
import Data.Maybe (fromJust, fromMaybe, isJust)

import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import qualified Data.List   as L
import Data.List (
    elemIndices,
    find,
    findIndices,
    intersect,
    isPrefixOf,
    nub,
    sort,
    sortOn,
 )
import qualified Data.Map as M
import qualified Data.Set as S

import Control.Monad.Cont (MonadIO (liftIO))
import Data.IORef (modifyIORef, newIORef, readIORef)
import GHC.IO.Handle (hDuplicateTo)
import System.IO (
    IOMode (ReadMode, WriteMode),
    openFile,
    stdin,
    stdout,
 )
import System.Posix.Internals (puts)
import Text.Printf (printf)

-- import Debug.Trace (trace, traceM, traceShowM)
-- debug = flip trace

readIntB8 :: B8.ByteString -> Int
readIntB8 = B8.readInt >>> (fst . fromJust)

readIntB8s :: B8.ByteString -> [Int]
readIntB8s = B8.words >>> map (B8.readInt >>> (fst . fromJust))

readIntegerB8 :: B8.ByteString -> Integer
readIntegerB8 = B8.readInteger >>> (fst . fromJust)

readIntegerB8s :: B8.ByteString -> [Integer]
readIntegerB8s = B8.words >>> map (B8.readInteger >>> (fst . fromJust))

modifyArray :: (MA.MArray a e m, MA.Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray arr ix f = do
    value <- MA.readArray arr ix
    MA.writeArray arr ix $ f value

putsYesNo :: Bool -> IO ()
putsYesNo value = do
    let result :: String =
            if value
                then "Yes"
                else "No"
    printf "%s\n" result

{- END OF GENERAL -}

solve :: Int -> [Int] -> Bool
solve n as = any checkElem $ as
    where
        sumAs = sum as
        checkElem e = isDivisible && midElement == e
            where
                isDivisible = (sumAs - e) `mod` (n - 1) == 0
                midElement = (sumAs - e) `div` (n - 1)

main :: IO ()
main = do
    t <- B8.getLine <&> readIntB8
    replicateM_ t $ do
        n <- B8.getLine <&> readIntB8
        as <- B8.getLine <&> readIntB8s
        let answer = solve n as
        putsYesNo answer

chunksOf :: Int -> [a] -> [[a]]
chunksOf count (item1:item2:items) = [item1, item2]:chunksOf count items
chunksOf count [] = []
chunksOf count [x] = []

processLine :: [[String]] -> [String]
processLine args = do
    [countS, itemsS] <- args
    let count = read countS :: Int
    let items = read <$> words itemsS :: [Int]
    let sumItems = sum items

    let {
        _go item = (_sum `div` _count) == item && (_sum `mod` _count) == 0
        where _sum = sumItems - item
              _count = count - 1
        }

    let result = or (_go <$> items)
    return (if result then "YES" else "NO")

main = do
    input <- tail . lines <$> getContents
    let chunks = chunksOf 2 input

    let results = processLine chunks

    mapM_ putStrLn results

poss :: [Int] -> Bool
poss ls = avg `elem` ls'
  where
    ls' = map (fromIntegral :: Int -> Float) ls
    avg = (sum ls') / (fromIntegral $ length ls')

solve = solves . lines

val s = poss (map read (words s))

solves [] = ""
solves [a, b] = if val b then "yes" else "no"
solves (a : b : s) = (if val b then "yes\n" else "no\n") ++ solves s

main = do
    getLine
    interact solve

import Control.Monad (replicateM)
-- import Data.Array.IArray

main :: IO ()
main = do
  t <- readLn :: IO Int
  res <- replicateM t run
  putStrLn $ unlines res

run :: IO String
run = do
  n <- readLn :: IO Int
  as <- map read . words <$> getLine :: IO [Int]
  return . toRes $ solve n as

toRes :: Bool -> String
toRes b = if b then "YES" else "NO"

solve :: Int -> [Int] -> Bool
solve n as = md == 0 && mean `elem` as
  where
    (mean, md) = sum as `divMod` n

chunksOf :: Int -> [a] -> [[a]]
chunksOf count (item1:item2:items) = [item1, item2]:chunksOf count items
chunksOf count [] = []
chunksOf count [x] = []

processLine :: [[String]] -> [String]
processLine args = do
    [countS, itemsS] <- args
    let count = read countS :: Int
    let items = read <$> words itemsS :: [Int]
    let sumItems = sum items

    let {
        _go item = (_sum `div` _count) == item && (_sum `mod` _count) == 0
        where _sum = sumItems - item
              _count = count - 1
        }

    let result = or (_go <$> items)
    return (if result then "YES" else "NO")

main = do
    input <- tail . lines <$> getContents
    let chunks = chunksOf 2 input

    let results = processLine chunks

    mapM_ print results

poss :: [Int] -> Bool
poss ls = avg `elem` ls
  where
    avg = (sum ls) `div` (length ls)

solve = solves . lines

val s = poss (map read (words s))

solves [] = ""
solves [a, b] = if val b then "yes" else "no"
solves (a : b : s) = (if val b then "yes\n" else "no\n") ++ solves s

main = do
    getLine
    interact solve


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

isStrong xs = case dropWhile (isSpace) xs of
                ('#':_) -> True
                _ -> False

solve :: [String] -> [String]
solve [] = [""]
solve lst@(x:xs)
      | isStrong x = x : ending xs
      | otherwise  = (:) (filter (not.isSpace) $ concat $ stuff) (ending other)
      where (stuff, other) = break (isStrong) lst
            ending [] = []
            ending xs = solve xs

main = do
  ls <- getContents
  putStr $ unlines $ solve (lines ls)

import Data.List

main :: IO ()
main = interact solver

solver :: String -> String
solver str = let p = lines str
                 fu st = [x|x<-st,x/=' ']
                 rev st = if fu st /= [] && head (fu st) == '#' then
                           (st,False)
                          else
                           (fu st,True)
                 comp st = groupBy (\x y->(snd x)&&(snd y)) st
             in unlines $ map (foldr (++) [] . map fst) $ comp $ map rev $ lines str

import Data.Char

main=interact$unlines.solve.lines

solve :: [String] -> [String]
solve [] = []
solve (s:ss) = case break isAmp ss of
                 ([],ys) | isAmp s   -> s:solve ys
                         | otherwise -> f[s]:solve ys
                 (xs,ys) | isAmp s   -> s:f xs:solve ys
                         | otherwise -> f(s:xs):solve ys

f :: [String] -> String
f = filter(not.isSpace).concat

isAmp :: String -> Bool
isAmp cs = case dropWhile(' '==)cs of
                    ('#':_) -> True
                    _       -> False

import Data.List
import Data.Char

solve :: [String] -> Maybe String -> [String]
solve [] (Just x) = [x]
solve [] _ = []
solve (x:xs) acc
      | (headM $ dropWhile (isSpace) x) == Just '#' =
          case acc of
            Nothing -> x : solve xs Nothing
            Just y  -> y : (x : solve xs Nothing)
      | True = solve xs $ appendM (filter (not.isSpace) x) acc
      where headM [] = Nothing
            headM (x:_) = Just x
            appendM x (Just y) = Just (y ++ x)
            appendM x _ = Just x

main = do
  ls <- getContents
  putStrLn.unlines $ solve (lines ls) Nothing

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

isStrong xs = case dropWhile (isSpace) xs of
                ('#':_) -> True
                _ -> False

solve :: [String] -> [String]
solve [] = [""]
solve lst@(x:xs)
      | isStrong x = x : solve xs
      | otherwise  = (filter (not.isSpace) $ concat $ stuff) : solve other
      where (stuff, other) = break (isStrong) lst

main = do
  ls <- getContents
  putStr $ unlines $ solve (lines ls)

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

appendM x (Just y) = Just (y ++ x)
appendM x _ = Just x

isStrong xs = case dropWhile (isSpace) xs of
                ('#':_) -> True
                _ -> False

solve :: [String] -> Maybe String -> [String]
solve [] (Just x) = [x]
solve [] _ = []
solve (x:xs) acc
      | isStrong x = solve xs $ appendM (filter (not.isSpace) x) acc
solve (x:xs) (Just y) = y : (x : solve xs Nothing)
solve (x:xs) _ = x : solve xs Nothing

main = do
  ls <- getContents
  mapM_ (putStrLn) $ solve (lines ls) Nothing

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

isStrong xs = case dropWhile (isSpace) xs of
                ('#':_) -> True
                _ -> False

solve :: [String] -> [String]
solve [] = [""]
solve lst@(x:xs)
      | isStrong x = x : solve xs
      | otherwise  = (:) (filter (not.isSpace) $ concat $ stuff) (ending other)
      where (stuff, other) = break (isStrong) lst
            ending [] = []
            ending xs = solve xs

main = do
  ls <- getContents
  putStr $ unlines $ solve (lines ls)

import Data.Char

main=interact$unlines.solve.lines

solve :: [String] -> [String]
solve [] = []
solve (s:ss)
  | isAmp s   = s:g(f xs:solve ys)
  | otherwise = f(s:xs):solve ys
   where (xs,ys) = break isAmp ss

f :: [String] -> String
f = filter(not.isSpace).concat
g :: [String] -> [String]
g [""] = []
g ss = ss
h :: [String] -> [String]
h ("":ss) = ss
h ss =ss

isAmp :: String -> Bool
isAmp cs = case dropWhile(' '==)cs of
                    ('#':_) -> True
                    _       -> False

import Data.Char

main=interact$unlines.solve.lines

solve :: [String] -> [String]
solve [] = []
solve (s:ss)
  | isAmp s   = s:g(f xs:solve ys)
  | otherwise = f(s:xs):solve ys
   where (xs,ys) = break isAmp ss

f :: [String] -> String
f = filter(not.isSpace).concat
g :: [String] -> [String]
g [""] = []
g ss = ss

isAmp :: String -> Bool
isAmp cs = case dropWhile(' '==)cs of
                    ('#':_) -> True
                    _       -> False