module Main (main) where

import System
import Data.Char
import Data.List
import Control.Monad
import Control.Applicative

makeList :: Int -> [[(Int,Int)]]
makeList n = [ [(x,y) | x <- [-n .. n]] | y <- [-n .. n]]

trim :: String -> String
trim = reverse . (dropWhile (`elem` " ")) . reverse

toString ::  [[(Int,Int)]] -> [String]
toString array = map convert array
         where
           n = (length (head array)) `div` 2
           convert list = trim$concatMap (\(x,y) -> [toChar (n - (abs x) - (abs y)) , ' ']) list
           toChar num = if num >= 0 then (head (show num)) else ' '

solve :: Int -> [String]
solve = toString.makeList

main = interact$unlines.solve.read.head.words

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Function
import Data.Ord
import Data.Bits
import Data.Monoid
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.Array.Unboxed as UA
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main= someFunc
someFunc::IO()
someFunc = (solve.concat =<< forM [1.. 1] ( \i -> map (fst.fromJust.C.readInteger).C.words<$>C.getLine))
solve [x]  = forM_ (map (\b-> unwords $ map (\a-> if a<0 then " " else show a) $ slv1 b x)  $ slv1 x x) $ \i -> putStrLn $ dropWhileEnd (==' ') i
slv1 x y =  [x-y..x]++[x-1,x-2..x-y]

import Control.Monad
import Data.List
solve n = let a = scanl' (flip (:)) [0] [1..n]
              b = zipWith (++) (map (reverse . tail) a) a
              c = b ++ (reverse $ init b)
              pad n x = (replicate (1+(4*n - length x)`div`2) ' ') ++ x
          in map (pad n. concat . intersperse " " . map show) c
main =  (readLn :: IO Int) >>= \x -> mapM  putStrLn $ solve x

str = unwords . map show . nums
nums n = [0..n] ++ [n-1,n-2..0]
main = do
  n <- read `fmap` getLine
  let sol = map f $ nums n
      f k = replicate ((n-k)*2) ' ' ++ str k
  putStr . unlines $ sol

import Data.List
f n = [0 .. n] ++ [n - 1, n - 2 .. 0]
g n x = (replicate (2 * (n - x)) ' ') ++ (intersperse ' ' $ concatMap show $ f x)
main = interact $ unlines.(\n -> map (g n) (f n)).read

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
{-# OPTIONS_GHC -O2 #-}

import Data.List
import Data.Maybe
import Data.Char
import Data.Array.IArray
import Data.Array.Unboxed (UArray)
import Data.Int
import Data.Ratio
import Data.Bits
import Data.Function
import Data.Ord
--import Control.Monad.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Sequence (Seq, (<|), (|>), (><), ViewL(..), ViewR(..))
import qualified Data.Sequence as Seq
import qualified Data.Foldable as F
import Data.Graph

parseInput = do
    readInt
  where
    readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace
    readInteger = state $ fromJust . BS.readInteger . BS.dropWhile isSpace
    readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace
    readLine = state $ BS.span (not . isEoln) . BS.dropWhile isEoln
    isEoln ch = ch == '\r' || ch == '\n'

main = putStr =<< solve . evalState parseInput <$> BS.getContents

solve n = unlines [ replicate (abs i * 2) ' ' ++ unwords (map show row')
                  | i <- [-n..n]
                  , let row = [0..n - abs i]
                  , let row' = row ++ tail (reverse row)
                  ]

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

class (Monad m) => MonadState s m | m -> s where
	get :: m s
	put :: s -> m ()

modify :: (MonadState s m) => (s -> s) -> m ()
modify f = do
	s <- get
	put (f s)

gets :: (MonadState s m) => (s -> a) -> m a
gets f = do
	s <- get
	return (f s)

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

instance Functor (State s) where
	fmap f m = State $ \s -> let
		(a, s') = runState m s
		in (f a, s')

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

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

instance MonadState s (State s) where
	get   = State $ \s -> (s, s)
	put s = State $ \_ -> ((), s)

evalState :: State s a -> s -> a
evalState m s = fst (runState m s)

execState :: State s a -> s -> s
execState m s = snd (runState m s)

mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
mapState f m = State $ f . runState m

withState :: (s -> s) -> State s a -> State s a
withState f m = State $ runState m . f

state = State

main :: IO ()
main = readLn >>= mapM_ putStrLn . solve

solve :: Int -> [String]
solve n = [ replicate (2 * (n - i)) ' ' ++ unwords (map show $ [0..i-1] ++ [i,i-1..0]) | i <- [0..n] ++ [n-1, n-2 .. 0] ]

import Data.List
import Control.Monad
main = do
  n <- readLn
  forM_ [-n.. -1] $ \i -> putStrLn $ str n (-i)
  forM_ [0..n] $ \i -> putStrLn $ str n i
str n i = replicate (2*i) ' ' ++
          unwords (map show $ [0..n-i] ++ [n-i-1,n-i-2..0])

import Data.List

-- createLine :: Int -> [Int]
-- createLine n =
--   r ++ tail (reverse r)
--   where r = take n [0..]

-- createTriangle :: Int -> [[Int]]
-- createTriangle n
--   | n == 0 = []
--   | otherwise = createLine n : createTriangle (n-1)

-- createDiamond :: Int -> [[Int]]
-- createDiamond n =
--   reverse (tail t) ++ t
--   where t = createTriangle n

-- spaces :: Int -> String
-- spaces n
--   | n == 0 = []
--   | otherwise = ' ' : spaces (n-1)

-- drawLine :: Int -> [Int] -> String
-- drawLine r l =
--   spaces ((length l)*2-1) ++ correctLine l ++ spaces ((length l)*2-1)
--   where
--     correctLine :: [Int] -> String
--     correctLine [] = ""
--     correctLine (n:ns) =

f n = [0..n] ++ [n-1, n-2..0]
g n x = (replicate (2 * (n - x)) ' ') ++ (intersperse ' ' $ concatMap show $ f x)
main = interact $ unlines . (\n -> map (g n) (f n)) . read

import Data.List
line n = intercalate " " . map show $ [0..n] ++ [n-1, n-2..0]
spacesLine m n = replicate (2*m-2*n) ' ' ++ line n
solve n = map (spacesLine n) $ [0..n] ++ [n-1, n-2..0]
main = fmap read getLine >>= mapM putStrLn . solve

import Data.List
main = do
    n <- read `fmap` getLine
    let s1 = reverse $ scanl (\s _ -> ' ':s) [] [1..n]
        s2 = scanl (\l i -> i:l) ['0'] $ take n ['1'..]
        s3 = zipWith (\a b@(_:bs) -> a ++ reverse bs ++ b) s1 s2
        s4 = s3 ++ tail (reverse s3)
    mapM_ (putStrLn . unwords . map (:[])) s4

r = read::String->Int
main=interact$stripLast.mirror.build.r
listToStr = unwords.map show
build n = f' n 0
  where f' n d
          | n < d = ""
          | otherwise = (replicate (2*(n-d)) ' ')
                        ++ listToStr [0..d]
                        ++ (if d == 0 then "" else " ")
                        ++ listToStr [(d-1),(d-2)..0]
                        ++ "\n"
                        ++ f' n (d+1)
mirror s = s ++ m' s
  where m' = unlines.tail.reverse.lines
stripLast s = take (length s - 1) s

{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE ForeignFunctionInterface, ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
import Foreign
import Foreign.C.Types
import Foreign.C.String
import Foreign.Marshal.Alloc
import Debug.Trace
import Data.List
import Control.Monad
import Control.Monad.ST
import qualified Data.Array.Unboxed as UB
import Data.Array.ST
import Data.Array
import Data.Char
class IOtyp a where scan' :: String -> a; show' :: a -> String
instance IOtyp Int where scan' = read; show' = show
instance IOtyp Char where scan' (x:_) = x;show' = (:[])
instance IOtyp Float where scan' = read;show' = show
instance IOtyp Double where scan' = read;show' = show
instance IOtyp Integer where scan' = read;show' = show
instance IOtyp String where scan' = id;show' = id
instance (IOtyp a,IOtyp b) => IOtyp (a,b) where scan' = (\(x:y:_) -> (scan' x,scan' y)).words;show' (x,y) = show' x++' ':show' y
instance (IOtyp a,IOtyp b,IOtyp c) => IOtyp (a,b,c) where scan' = (\(x:y:z:_) -> (scan' x,scan' y,scan' z)).words;show' (x,y,z) = show' x++' ':show' y++' ':show' z
foreign import ccall "stdio.h puts" c_puts :: CString -> IO ()
scan :: (IOtyp a) => IO a; scan = getLine>>=return.scan'
scans :: (IOtyp a) => Int -> IO [a]; scans = (flip replicateM) scan
scanlist :: (IOtyp a) => IO [a]; scanlist = getLine>>=return.map scan'.words
scanlists :: (IOtyp a) => Int -> IO [[a]]; scanlists = (flip replicateM) scanlist
puts :: (IOtyp a) => a -> IO (); puts ans = do {str<-newCString $ show' ans;c_puts str;free str}

solve :: Int -> String
solve n = rec $ [0..n]++[n-1,n-2..0]
    where
      rec [] = ""
      rec (x:xs) = now++(rec xs)
          where
            now = (take ((n-x)*2) (repeat ' '))++(intersperse ' ' (foldl (\st -> \i -> st++(show i)) "" $ [0..x]++[x-1,x-2..0]))++"\n"
main = do puts.init.solve =<< (scan :: IO Int)

import Data.List
import Control.Monad

main :: IO ()
main = do
    line <- getLine
    let n = read line :: Int
    let result = let list = forLine n n in (reverse $ tail list) ++ list
    mapM_ putStrLn result

forLine :: Int -> Int -> [String]
forLine n 0 = [replicate (n*2) ' ' ++ "0"]
forLine n x = let before = [0..(x-1)]; after = [(x-1), (x-2)..0] in [replicate ((n-x)*2) ' ' ++ (intersperse ' ' . concatMap (show)) (before ++ [x] ++ after)] ++ forLine n (x-1)

import Control.Applicative
import Data.List

main= do
	n<-  read<$> getLine ::IO Int
	let s = tail $ take (n+2) $ inits [0..n]
	let t = zipWith (++) s ([]: (map reverse s))
	let u = t++  tail (reverse t)
	let sp = ([replicate (i+i) ' '|i<-[0..n]]):: [[Char]]
	let sp1 = ((reverse sp ) ++ (tail sp) )::[[Char]]
 	let x= intercalate "\n" $ zipWith (++) sp1 (map (intercalate " " . map show) u)
	putStrLn x

{-
B. Present from Lena
=====================
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to n as the pattern. The digits will form a rhombus. The largest digit n should be located in the centre. The digits should decrease as they approach the edges. For example, for n = 5 the handkerchief pattern should look like that:

          0
        0 1 0
      0 1 2 1 0
    0 1 2 3 2 1 0
  0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
  0 1 2 3 4 3 2 1 0
    0 1 2 3 2 1 0
      0 1 2 1 0
        0 1 0
          0

Your task is to determine the way the handkerchief will look like by the given n.

Input
------
The first line contains the single integer n (2 ≤ n ≤ 9).

Output
------
Print a picture for the given n. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line.

Sample test(s)
---------------
input
2
output
    0
  0 1 0
0 1 2 1 0
  0 1 0
    0

input
3
output
      0
    0 1 0
  0 1 2 1 0
0 1 2 3 2 1 0
  0 1 2 1 0
    0 1 0
      0

-}
import Data.List (intercalate)

pattern :: Int -> [String]
pattern n = map helper (upAndDown n)
  where helper k = replicate (2 * (n - k)) ' ' ++ (intercalate " " . map show) (upAndDown k)

upAndDown :: Int -> [Int]
upAndDown 0 = [0]
upAndDown n = tmp ++ (tail . reverse) tmp
  where tmp = [0 .. n]

main = do
  input <- getLine
  let n = read input
  mapM_ putStrLn (pattern n)

import Data.List
import Data.Char
main = getLine >>= putStr.unlines.solve.read

abses m  = map ((m-).abs.(m-)) [0..2 * m]

line n m = intersperse ' ' $ concat [replicate (n - m) ' ', map intToDigit $ abses m]

solve n = map (line n) $ abses n

import Data.Char
import Data.List

toInt :: String -> Int
toInt = read

strSeq :: Int -> [ [Char] ]
strSeq n 	= 	[ prefix ++ ( intersperse ' ' ( x ++  (reverse . init ) x ) ) |
					let ls = ( enumFromTo 0 n ),
					y <- ls ++ ( reverse . init ) ls,
					let x = map intToDigit (enumFromTo 0 y),
					let prefix = replicate ( 2*(n - y) ) ' '
				]

main = do
	strN 	<- getLine
	putStr $ ( unlines . strSeq . toInt ) strN

{-# LANGUAGE FlexibleInstances #-}
module Main (printShit, printShitV, readShit, main) where
import Data.ByteString.Builder
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Maybe
import Data.List
import Data.Char
-- Meat

solve :: Int -> [String]
solve n = fmap (buildLine n) ([0..n]++reverse[0 .. n - 1])

buildLine :: Int -> Int -> String
buildLine total i = doublePad total $ innerString i

doublePad :: Int -> String -> String
doublePad n inner = spaces ++ inner
    where spaceLen = (4 * n + 1 - length inner) `quot` 2
          spaces = replicate spaceLen ' '

innerString :: Int -> String
innerString 0 = "0"
innerString maxDig = intersperse ' ' (['0'..c]++ reverse [ '0' .. pred c])
    where c = intToDigit maxDig

-- Shit
main :: IO ()
main = do
        [n] <- readShit
        printShitV $ solve n

class Shit a where
    printShit :: a -> IO ()
    printShitV :: a -> IO ()
    readShit :: IO a
    printShitV = printShit

instance  Shit [Int] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance  Shit [Integer] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance Shit [[Int]] where
    printShit = printShit . (fmap.fmap)  (toLazyByteString . integerDec .toInteger)
    readShit = fmap (fmap.fmap $ fromInteger.fst.fromJust. BS.readInteger) readShit

instance Shit [[Integer]] where
    printShit  = printShit . (fmap.fmap)  (toLazyByteString . integerDec)
    readShit = fmap (fmap.fmap $ fromInteger.fst.fromJust. BS.readInteger) readShit

instance Shit [String] where
    printShit = putStrLn. unwords
    printShitV = putStrLn . unlines
    readShit = fmap lines getContents

instance Shit [[String]] where
    printShit = putStrLn . unlines . fmap unwords
    readShit = fmap (fmap (fmap words) lines) getContents

instance Shit [BS.ByteString] where
    printShit = BS.putStrLn. BS.unwords
    printShitV = BS.putStrLn . BS.unlines
    readShit = fmap BS.lines BS.getContents

instance Shit [[BS.ByteString]] where
    printShit = BS.putStrLn . BS.unlines . fmap BS.unwords
    readShit = fmap (fmap (fmap BS.words) BS.lines) BS.getContents

make_row 0 = [0]
make_row x = [0..x] ++ [x-1, x-2 .. 0]

pprint :: Int -> ([[Int]] -> String)
pprint n = unlines . (map fl)
           where
             fl = fill . unwords . map show
             fill x = (spaces x) ++ x
             spaces:: String -> String
             spaces x = take ((2*n+1 - length x) `div` 2) (repeat ' ')

solve :: Int->String
solve x = pprint (2*x) [make_row x | x <- [0..x] ++ [x-1, x-2 .. 0]]

parse = (read::String->Int)

main = interact (solve . parse)

main = do
	n<-getLine
	let res1 = solve (read n) 0
	let res2 = init $ unlines $reverse $ init $ lines res1
	putStr $ res1
	putStrLn res2

solve n l | l <=n = spaces ((n-l)) ++ printer (generator l) ++ "\n" ++ solve n (l+1)
          | otherwise = ""

generator n = [0,1..n-1] ++ [n,n-1..0]

spaces n = replicate (2*n) ' '

printer s = init $ concat $  map (\x-> show x ++ " ") s

join _ [] = []
join c [x] = x
join c (x:xs) = x ++ [c] ++ join c xs

solve n = map tostr a
	where
		tostr lst = (take (n * 2 + 1 - length lst) $ repeat ' ') ++ (join ' ' $ map show lst)
		a = [ [0..i-1] ++ [i,i-1..0] | i <- [0..n-1] ++ [n,n-1..0] ]

main = getLine >>= putStrLn . join '\n' . solve . read

{-# 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.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

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

main = do
  n <- readLn

  let
    s = [[f i j | j <- [1..2*n+1]] | i <- [1..2*n+1]]
    f i j = if d <= n then intToDigit (n-d) else ' '
      where
        d = abs (i-n-1) + abs (j-n-1)

  putStr $ unlines $ map (dropWhileEnd isSpace . intersperse ' ') s

main = do
	io <- getLine
	let n = read io
	putStr $ work 0 (2*n)

work :: Int -> Int -> [Char]
work x 0 = pl 0 x ++ ['\n']
work x sp = pre ++ work (x+1) (sp-2) ++ pre
		where pre = replicate sp ' ' ++ pl 0 x ++ ['\n']

pl :: Int -> Int -> [Char]
pl x y
	| x == y = show x
	| otherwise = show x ++ [' '] ++ pl (x+1) y ++ [' '] ++ show x

import Data.List

printline n x = intersperse ' ' $ take (n - x) (repeat ' ') ++ concatMap show ( [0..x] ++ drop 1 ( reverse [0 .. x] ))
printthing x = mapM_ (putStrLn.(printline x)) ((\x-> x ++ (drop 1 $ reverse x)) [0..x])
main = getLine >>= return.read >>= printthing

import Monad
import Data.Char
import Debug.Trace

doSolve :: Int -> IO ()
doSolve n = doSolve' 0
    where
      with :: a -> [a] -> [a]
      with s (x:y:xs)  = x : s : (with s (y:xs))
      with s xs@(x:[]) = xs

      adjust :: [Int] -> String
      adjust lst = let len = length lst
                       tmp = (with ' ' $ map (chr . (+ ord '0')) lst)
                       ret =  replicate (2 * (n - len)) ' ' ++
                              tmp ++ (tail $ reverse tmp)
                   in ret

      doSolve' :: Int -> IO ()
      doSolve' i | i == n    = return ()
                 | otherwise = let lst = [0..i]
                               in do putStrLn $ adjust lst
                                     doSolve' $ i + 1
                                     when (n - 1 /= i) $ putStrLn $ adjust lst

main :: IO ()
main = do cs <- getContents
          let n = read cs :: Int
          doSolve $ n + 1

{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE OverlappingInstances #-}

module Main where

import           Control.Applicative
import           Control.Monad.State
import qualified Data.ByteString.Lazy.Char8 as B
import           Data.Function
import           Data.List
import           Data.Maybe
import           System.Environment
import           System.IO

type BString = B.ByteString

solve :: Int -> [String]
solve n = map make_line $ make_seq n
  where
    make_line :: Int -> String
    make_line n' = replicate (2 * (n - n')) ' ' ++ unwords (map show $ make_seq n')
    make_seq :: Int -> [Int]
    make_seq n = [0 .. n - 1] ++ [n, n - 1 .. 0]

process :: State Reader [String]
process = solve <$> parse

main :: IO ()
main = do
    args <- getArgs
    reader <- if not (null args) && head args == "MINE"
              then B.lines <$> (openFile "input.txt" ReadMode >>= B.hGetContents)
              else B.lines <$> B.getContents
    say $ evalState process reader

say :: Display a => a -> IO ()
say = putStrLn . display

toNum :: Num a => Bool -> a
toNum False = 0
toNum True  = 1

type Reader = [BString]

class EIO a where
    parse :: State Reader a

instance EIO Int where
    parse = state $ \(line:rest) -> (fst $ fromJust $ B.readInt line, rest)

instance EIO [Int] where
    parse = state $ \(line:rest) -> (map fst . mapMaybe B.readInt . B.words $ line, rest)

instance EIO BString where
    parse = state $ \(line:rest) -> (line, rest)

class Show a => Display a where
    display :: a -> String
    display = show

instance Display String where
    display = id

instance Display [a] => Display [[a]] where
    display = unlines . map display

instance Display a => Display [a] where
    display = unwords . map display

instance Display Int
instance Display Double

main = getLine >>= putStrLn . unlines .  func' . read

func' :: Integer -> [String]
func' n = cteateSector n ++  tail ( reverse ( cteateSector n ))

cteateSector :: Integer -> [String]
cteateSector n = map insertSpaces $ reverse [strCreate kk n ++ takeWhile(/=' ') (tail (reverse (strCreate kk n))) | kk <- [0..n]]
			where strCreate k n = concat [if (nn < k) then " " else show (nn-k )  | nn<-[0..n]]

insertSpaces :: String -> String
insertSpaces [h] = [h]
insertSpaces (h:str) = h : ' ' : insertSpaces str

main = do
	n<-getLine
	let res1 = solve (read n) 0
	let res2 = reverse $ init $ unlines $ init $ lines res1
	putStr $ res1
	putStrLn res2

solve n l | l <=n = spaces ((n-l)) ++ printer (generator l) ++ "\n" ++ solve n (l+1)
          | otherwise = ""

generator n = [0,1..n-1] ++ [n,n-1..0]

spaces n = replicate (2*n) ' '

printer s = init $ concat $  map (\x-> show x ++ " ") s

main = do
	n<-getLine
	let res1 = solve (read n) 0
	let res2 = reverse $ init $ unlines $ init $ lines res1
	putStr $ res1
	putStrLn res2

solve n l | l <=n = spaces ((n-l)) ++ printer (generator l) ++ spaces ((n-l))++ "\n" ++ solve n (l+1)
          | otherwise = ""

generator n = [0,1..n-1] ++ [n,n-1..0]

spaces n = replicate (2*n) ' '

printer s = init $ concat $  map (\x-> show x ++ " ") s

{-# 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.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

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

main = do
  n <- readLn

  let
    s = [[f i j | j <- [1..2*n+1]] | i <- [1..2*n+1]]
    f i j = if d <= n then intToDigit (n-d) else ' '
      where
        d = abs (i-n-1) + abs (j-n-1)

  putStr $ unlines $ map (intersperse ' ') s

main = getLine >>= putStrLn . unlines .  func' . read

func' :: Integer -> [String]
func' n = cteateSector n ++  tail ( reverse ( cteateSector n ))

cteateSector :: Integer -> [String]
cteateSector n = reverse [strCreate kk n ++ tail (reverse (strCreate kk n)) | kk <- [0..n]]
			where strCreate k n = unwords [if (nn < k) then " " else show (nn-k )  | nn<-[0..n]]

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Function
import Data.Ord
import Data.Bits
import Data.Monoid
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.Array.Unboxed as UA
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main= someFunc
someFunc::IO()
someFunc = (solve.concat =<< forM [1.. 1] ( \i -> map (fst.fromJust.C.readInteger).C.words<$>C.getLine))
solve [x]  = forM_ (map (\b-> unwords $ map (\a-> if a<0 then " " else show a) $ slv1 b x)  $ slv1 x x) $ \i -> putStrLn i
slv1 x y =  [x-y..x]++[x-1,x-2..x-y]

import Data.List
import Control.Monad

main :: IO ()
main = do
    line <- getLine
    let n = read line :: Int
    let result = let list = forLine n n in (reverse $ tail list) ++ list
    mapM_ print result

forLine :: Int -> Int -> [String]
forLine n 0 = [replicate (n*2) ' ' ++ "0"]
forLine n x = let before = [0..(x-1)]; after = [(x-1), (x-2)..0] in [replicate (n-x) ' ' ++ (intersperse ' ' . concatMap (show)) (before ++ [x] ++ after)] ++ forLine n (x-1)

import Data.List
import Control.Monad

main :: IO ()
main = do
    line <- getLine
    let n = read line :: Int
    let result = let list = forLine n n in (reverse $ tail list) ++ list
    mapM_ putStrLn result

forLine :: Int -> Int -> [String]
forLine n 0 = [replicate (n*2) ' ' ++ "0"]
forLine n x = let before = [0..(x-1)]; after = [(x-1), (x-2)..0] in [replicate (n-x) ' ' ++ (intersperse ' ' . concatMap (show)) (before ++ [x] ++ after)] ++ forLine n (x-1)

import Data.Char
import Data.List

toInt :: String -> Int
toInt = read

strSeq :: Int -> [ [Char] ]
strSeq n 	= 	[ prefix ++ ( intersperse ' ' ( x ++  (reverse . init ) x ) ) ++ prefix |
					let ls = ( enumFromTo 0 n ),
					y <- ls ++ ( reverse . init ) ls,
					let x = map intToDigit (enumFromTo 0 y),
					let prefix = replicate ( 2*(n - y) ) ' '
				]

main = do
	strN 	<- getLine
	putStr $ ( unlines . strSeq . toInt ) strN

import Data.Char
import Data.List

toInt :: String -> Int
toInt = read

strSeq :: Int -> [ [Char] ]
strSeq n 	= 	[ prefix ++ ( intersperse ' ' ( x ++  (reverse . init ) x ) ) ++ prefix |
					let ls = ( enumFromTo 0 n ),
					y <- ls ++ ( reverse . init ) ls,
					let x = map intToDigit (enumFromTo 0 y),
					let prefix = replicate ( 2*(n - y) ) ' '
				]

main = do
	strN 	<- getLine
	putStrLn $ ( unlines . strSeq . toInt ) strN

{-# LANGUAGE FlexibleInstances #-}
module Main (printShit, printShitV, readShit, main) where
import Data.ByteString.Builder
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Maybe
import Data.List
import Data.Char
-- Meat

solve :: Int -> [String]
solve n = fmap (buildLine n) ([0..n]++reverse[0 .. n - 1])

buildLine :: Int -> Int -> String
buildLine total i = doublePad total $ innerString i

doublePad :: Int -> String -> String
doublePad n inner = spaces ++ inner ++ spaces
    where spaceLen = (4 * n + 1 - length inner) `quot` 2
          spaces = replicate spaceLen ' '

innerString :: Int -> String
innerString 0 = "0"
innerString maxDig = intersperse ' ' (['0'..c]++ reverse [ '0' .. pred c])
    where c = intToDigit maxDig

-- Shit
main :: IO ()
main = do
        [n] <- readShit
        printShitV $ solve n

class Shit a where
    printShit :: a -> IO ()
    printShitV :: a -> IO ()
    readShit :: IO a
    printShitV = printShit

instance  Shit [Int] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance  Shit [Integer] where
    printShit = putStrLn . unwords . fmap show
    printShitV = putStrLn . unlines . fmap show
    readShit = fmap (fmap read . words) getLine

instance Shit [[Int]] where
    printShit = printShit . (fmap.fmap)  (toLazyByteString . integerDec .toInteger)
    readShit = fmap (fmap.fmap $ fromInteger.fst.fromJust. BS.readInteger) readShit

instance Shit [[Integer]] where
    printShit  = printShit . (fmap.fmap)  (toLazyByteString . integerDec)
    readShit = fmap (fmap.fmap $ fromInteger.fst.fromJust. BS.readInteger) readShit

instance Shit [String] where
    printShit = putStrLn. unwords
    printShitV = putStrLn . unlines
    readShit = fmap lines getContents

instance Shit [[String]] where
    printShit = putStrLn . unlines . fmap unwords
    readShit = fmap (fmap (fmap words) lines) getContents

instance Shit [BS.ByteString] where
    printShit = BS.putStrLn. BS.unwords
    printShitV = BS.putStrLn . BS.unlines
    readShit = fmap BS.lines BS.getContents

instance Shit [[BS.ByteString]] where
    printShit = BS.putStrLn . BS.unlines . fmap BS.unwords
    readShit = fmap (fmap (fmap BS.words) BS.lines) BS.getContents

make_row 0 = [0]
make_row x = [0..x] ++ [x-1, x-2 .. 0]

pprint :: Int -> ([[Int]] -> String)
pprint n = unlines . (map fl)
           where
             fl = fill . unwords . map show
             fill x = (spaces x) ++ x ++ (spaces x)
             spaces:: String -> String
             spaces x = take ((2*n+1 - length x) `div` 2) (repeat ' ')

solve :: Int->String
solve x = pprint (2*x) [make_row x | x <- [0..x] ++ [x-1, x-2 .. 0]]

parse = (read::String->Int)

main = interact (solve . parse)

r = read::String->Int
main=interact$stripLast.mirror.build.r
listToStr = unwords.map show
build n = f' n 0
  where f' n d
          | n < d = ""
          | otherwise = (replicate (2*(n-d)) ' ')
                        ++ listToStr [0..d]
                        ++ " "
                        ++ listToStr [(d-1),(d-2)..0]
                        ++ "\n"
                        ++ f' n (d+1)
mirror s = s ++ m' s
  where m' = unlines.tail.reverse.lines
stripLast s = take (length s - 1) s

r = read::String->Int
main=interact$stripLast.mirror.build.r
listToStr = unwords.map show
build n = f' n 0
  where f' n d
          | n < d = ""
          | otherwise = (replicate (2*(n-d)) ' ')
                        ++ listToStr [0..d]
                        ++ " "
                        ++ listToStr [(d-1),(d-2)..0]
                        ++ ['\n']
                        ++ f' n (d+1)
mirror s = s ++ m' s
  where m' = unlines.tail.reverse.lines
stripLast s = take (length s - 1) s

r = read::String->Int
main=interact$stripLast.mirror.build.r
listToStr = unwords.map show
build n = f' n 0
  where f' n d
          | n < d = ""
          | otherwise = (replicate (2*(n-d)) ' ')
                        ++ listToStr [0..d]
                        ++ " "
                        ++ listToStr [(d-1),(d-2)..0]
                        ++ " \n"
                        ++ f' n (d+1)
mirror s = s ++ m' s
  where m' = unlines.tail.reverse.lines
stripLast s = take (length s - 1) s


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

main = getLine >>= flip replicateM_ test . read

test = do
  n <- read <$> getLine
  s <- concat <$> replicateM n getLine
  putStrLn $ bool "NO" "YES" $ all (\c -> length (filter (== c) s) `mod` n == 0) ['a' .. 'z']

import qualified Data.Map as M

main = do
    t <- getInt
    sequence $ replicate t (do
        n <- getInt
        strings <- sequence $ replicate n getLine
        let concated = foldl (++) [] strings
            cnt = countLetters concated
            corrects = (map (\x -> (x `mod` n) == 0) cnt) :: [Bool]
        putStrLn (if foldl (&&) True corrects then "YES" else "NO")
        )

countLetters :: String -> [Int]
countLetters [] = []
countLetters str@(ch:_) = (length [x | x <- str, x == ch]):countLetters [x | x <- str, x /= ch]

getInt :: IO Int
getInt = do
    val <- getLine
    return $ read val

import Data.List ( sort, group )
import Control.Monad ( replicateM_, replicateM )

solve :: Int -> [String] -> Bool
solve n strs = all (\c -> mod (length c) n == 0) . group . sort $ concat strs

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    n <- read <$> getLine
    strs <- replicateM n getLine
    putStrLn (if  solve n strs  then  "YES"  else  "NO")

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

main = getLine >>= flip replicateM_ test . read

test = do
  n <- read <$> getLine
  s <- concat <$> replicateM n getLine
  print $ bool "NO" "YES" $ all (\c -> length (filter (== c) s) `mod` n == 0) ['a' .. 'z']


{-# LANGUAGE LambdaCase #-}
import Data.List
import qualified Data.ByteString.Char8 as B

main = B.interact exec
exec = B.pack . maybe "NO" (\(a, b) -> unlines ["YES", show a, show b]) . fn . map (maybe undefined fst . B.readInt) . tail . B.words
hi = 10^(9 :: Int) :: Int
fn xs = (\case { [] -> Just (hi, hi); [1] -> Just (1, hi); [y] | y > 0 -> Just (hi, y); [1, y] | and $ snd $ mapAccumL (\p x -> (x, abs (x - p) /= 1 || (x - 1) `quot` y == (p - 1) `quot` y)) (head xs) (tail xs) -> Just (hi, y); _ -> Nothing; } ) . snub . sort . map abs . (zipWith (-) =<< tail) $ xs

snub [] = []
snub (x:xs) = x:go x xs
  where
  go _ [] = []
  go x (y:ys)
     | x < y = y:go y ys
     | otherwise = go x ys

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | length moves == 2 && head moves /= 1 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then (if moves !! 0 == 1 then 1000000000 else moves !! 0) else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) ((head xs - 1) `mod` ud + 1, (head xs - 1) `div` ud + 1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

{-# LANGUAGE LambdaCase #-}
import Data.List

main = interact exec
exec = maybe "NO" (\(a, b) -> unlines ["YES", show a, show b]) . fn . map read . tail . words
fn = (\case { [1] -> Just (1 :: Int, 10^9 :: Int); [y] -> Just (10^9, y); [1, y] -> Just (10^9, y); _ -> Nothing; } ) . snub . sort . map abs . (zipWith (-) =<< tail)

snub [] = []
snub (x:xs) = x:go x xs
  where
  go _ [] = []
  go x (y:ys)
     | x < y = y:go y ys
     | otherwise = go x ys

{-# LANGUAGE LambdaCase #-}
import Data.List

main = interact exec
exec = maybe "NO" (\(a, b) -> unlines ["YES", show a, show b]) . fn . map read . tail . words
hi = 10^(9 :: Int) :: Int
fn = (\case { [] -> Just (hi, hi); [1] -> Just (1, hi); [y] -> Just (hi, y); [1, y] -> Just (hi, y); _ -> Nothing; } ) . snub . sort . map abs . (zipWith (-) =<< tail)

snub [] = []
snub (x:xs) = x:go x xs
  where
  go _ [] = []
  go x (y:ys)
     | x < y = y:go y ys
     | otherwise = go x ys

{-# LANGUAGE LambdaCase #-}
import Data.List
import qualified Data.ByteString.Char8 as B

main = B.interact exec
exec = B.pack . maybe "NO" (\(a, b) -> unlines ["YES", show a, show b]) . fn . map (maybe undefined fst . B.readInt) . tail . B.words
hi = 10^(9 :: Int) :: Int
fn xs = (\case { [] -> Just (hi, hi); [1] -> Just (1, hi); [y] -> Just (hi, y); [1, y] | and $ snd $ mapAccumL (\p x -> (x, abs (x - p) /= 1 || x `quot` (y + 1) == p `quot` (y + 1) )) (head xs) (tail xs) -> Just (hi, y); _ -> Nothing; } ) . snub . sort . map abs . (zipWith (-) =<< tail) $ xs

snub [] = []
snub (x:xs) = x:go x xs
  where
  go _ [] = []
  go x (y:ys)
     | x < y = y:go y ys
     | otherwise = go x ys

{-# LANGUAGE LambdaCase #-}
import Data.List

main = interact exec
exec = maybe "NO" (\(a, b) -> unlines ["YES", show a, show b]) . fn . map read . tail . words
hi = 10^(9 :: Int) :: Int
fn xs = (\case { [] -> Just (hi, hi); [1] -> Just (1, hi); [y] -> Just (hi, y); [1, y] | and $ snd $ mapAccumL (\p x -> (x, abs (x - p) /= 1 || x <= y)) (head xs) (tail xs) -> Just (hi, y); _ -> Nothing; } ) . snub . sort . map abs . (zipWith (-) =<< tail) $ xs

snub [] = []
snub (x:xs) = x:go x xs
  where
  go _ [] = []
  go x (y:ys)
     | x < y = y:go y ys
     | otherwise = go x ys

{-# LANGUAGE LambdaCase #-}
import Data.List
import qualified Data.ByteString.Char8 as B

main = B.interact exec
exec = B.pack . maybe "NO" (\(a, b) -> unlines ["YES", show a, show b]) . fn . map (maybe undefined fst . B.readInt) . tail . B.words
hi = 10^(9 :: Int) :: Int
fn xs = (\case { [] -> Just (hi, hi); [1] -> Just (1, hi); [y] | y > 0 -> Just (hi, y); [1, y] | and $ snd $ mapAccumL (\p x -> (x, abs (x - p) /= 1 || x `quot` (y + 1) == p `quot` (y + 1) )) (head xs) (tail xs) -> Just (hi, y); _ -> Nothing; } ) . snub . sort . map abs . (zipWith (-) =<< tail) $ xs

snub [] = []
snub (x:xs) = x:go x xs
  where
  go _ [] = []
  go x (y:ys)
     | x < y = y:go y ys
     | otherwise = go x ys

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then (if moves !! 0 == 1 then 1000000000 else moves !! 0) else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) (head xs `mod` ud, (head xs - 1) `div` ud + 1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | head moves /= 1 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then (if moves !! 0 == 1 then 1000000000 else moves !! 0) else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) ((head xs - 1) `mod` ud + 1, (head xs - 1) `div` ud + 1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (10000000000, 10000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 10000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then moves !! 0 else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) (1,1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show a) ++ " " ++ (show b))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then (if moves !! 0 == 1 then 1000000000 else moves !! 0) else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) (1,1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (10000000000, 10000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 10000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then moves !! 0 else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) (1,1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then 1000000000 else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) (1,1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then (if moves !! 0 == 1 then 1000000000 else moves !! 0) else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) ((head xs - 1) `mod` ud + 1, (head xs - 1) `div` ud + 1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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

import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (fix)
import Data.Array (Array, array, (!))
import qualified Data.Sequence as S
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

getInts :: IO [Int]
getInts = (map parseInt . BS8.words) <$> BS.getLine
    where parseInt = fst . fromJust . BS8.readInt

solve :: [Int] -> Maybe (Int, Int)
solve xs
    | length xs == 1 = Just (1000000000, 1000000000)
    | length moves > 2 = Nothing
    | head moves == 0 = Nothing
    | otherwise = if valid then Just (ud, 1000000000) else Nothing
    where gap = zipWith (-) xs (tail xs)
          moves = map head $ group $ sort $ map abs gap
          ud = if length moves == 1 then moves !! 0 else moves !! 1
          history = scanl (\(x,y) g-> if g == 1 || g == -1 then (x-g,y) else (x,y-g `div` ud)) (1,1) gap
          valid = length (filter (\(x,y) -> x < 1 || y < 1 || x > ud) history) == 0

printAns :: Maybe (Int, Int) -> IO ()
printAns Nothing = putStrLn "NO"
printAns (Just (a,b)) = putStrLn "YES" >> putStrLn ((show b) ++ " " ++ (show a))

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


main = do
  n <- readLn :: IO Int
  v <- fmap ((map (\x -> read x::Int)) . words) getLine
  [a,b] <- fmap ((map (\x -> read x::Int)) . words) getLine
  print $ sum $ (drop (a - 1)) $ take (b - 1) v

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

solve :: [[Int]] -> Int
solve [_, ds, [a, b]] = sum (drop (a - 1) $ take (b - 1) ds)
solve _ = undefined

main = do
  getLine
  ds <- fmap (map read . words) getLine
  [a,b] <- fmap (map read . words) getLine
  print $ sum $ take (b-a) $ drop (a-1) ds

import Prelude
import List
import Char

main = interact q

q = func.tail.lines

func::[String]->String
func = toString.solve.map (map (toInt).words)

fileInteract func inf ouf = do file <- readFile inf
                               writeFile ouf (func file)

toInt::String->Int
toInt = read

asInteger::String->Integer
asInteger = read

toString::Int->String
toString = show

solve::[[Int]]->Int
solve a = sum$take (c2-c1)$drop c1 b
    where
      b = a!!0
      c1 = (a!!1)!!0-1
      c2 = (a!!1)!!1-1

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

main = do
    line <- getLine
    num <- getnum
    [a,b] <- getnum
    print . sum . take (b-a) . drop (a-1) $ num

main = interact f
f input = output where
  [[n],d,[a,b]] = map (map (read::String->Int)) $ map words $ lines input
  output = show $ sum $ take (b-a) $ drop (a-1) d

import Control.Applicative
import Data.List

main= do
	getLine
	s<- map read. words <$> getLine ::IO [Int]
	[a,b]<- map read. words <$> getLine ::IO [Int]
	print $ sum $ drop (a-1) $ take (b-1) s

module Main where

import Control.Monad
import Data.List

main = do
    n <- read `liftM` getLine :: IO Int
    ds <- ( map read . words ) `liftM` getLine
    [a, b] <- ( map read . words ) `liftM` getLine

    putStrLn $ show $ sum $ take (b-a) $ drop (a-1) ds

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

solve :: [Int] -> Int
solve (n:d) = let a = d !! (n - 1)
                  b = d !! n
              in solve' 1 a b d
              where solve' :: Int -> Int -> Int -> [Int] -> Int
                    solve' _ _ _ []    = 0
                    solve' i a b (d:x) | a <= i && i < b = d + solve' (i + 1) a b x
                                       | otherwise       = solve' (i + 1) a b x

main = do
    n<-readLn :: IO Int
    arr <- fmap (map (\x -> read x::Int) . words) getLine
    [a,b] <- fmap (map (\x -> read x::Int) . words) getLine
    print $solve a b arr

solve a b arr = sum $drop (a-1) (take (b-1) (arr))

main = do
    n <- getLine
    s <- getLine
    ab <- getLine
    let [a, b] = map read (words ab)
    print (f (map read (words s)) (a - 1) (b - 2))
f :: [Int] -> Int -> Int -> Int
f x a b
    | a == b = x !! a
    | a == 0 = head x + (f (tail x) a (b - 1))
    | otherwise = f (tail x) (a - 1) (b - 1)

myInput :: Int -> IO String
myInput n = return (["11", "1 2 3 4 5 6 7 8 9 10", "1 11"] !! n)

solve :: Int -> Int -> [Int] -> Int
solve _ 1 _ = 0
solve 1 m (a:as) = a + solve 1 (m - 1) as
solve n m (a:as) = solve (n - 1) (m - 1) as

main = do
    s <- getLine
    s <- getLine
    let as = map (read::(String -> Int)) (words s)
    s <- getLine
    let [a, b] = map (read::(String -> Int)) (words s)
    print (solve a b as)

solve [xs, [a, b]] = sum $ map fst $ takeWhile (f b) $ dropWhile (f a) $ zip xs [1..]
    where f v = (/=v).snd

main = interact $ show.solve.map (map read.words).tail.lines

main = do
    n<-readLn :: IO Int
    arr <- fmap (map (\x -> read x::Int) . words) getLine
    [a,b] <- fmap (map (\x -> read x::Int) . words) getLine
    print $solve a b arr

solve a b arr = sum $take (b-1) (drop (a-1) arr)


import Data.Int

main = do interact $ show . gao . map read . tail . words where
	gao :: [Int32] -> Int
	gao a = let (s:b) = scanr1 (+) a in length $ filter (\i -> i + i == s) b

import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  getLine >>= return . map read . words

solve :: [Int] -> Int
solve xs  =  sum $ solve' (head xs, sum xs - head xs) $ tail xs
  where
    solve' _ []  =  []
    solve' (l, r) (x:xs)  =  (if l == r then 1 else 0) : solve' (l + x, r - x) xs

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

import Data.Int
import IO

main = do
	hSetBuffering stdin $ BlockBuffering (Just 1000000)
	hGetContents stdin >>= putStrLn . show . gao . map read . tail . words where
		gao :: [Int32] -> Int
		gao a = let (s:b) = scanr1 (+) a in length $ filter (\i -> i + i == s) b

countS :: [Int] -> [Int]
countS [x] = [x]
countS (x : xs) = (x + head ss) : ss
        where ss = countS xs

countWays [] _ = 0
countWays (s : ss) sum = if 2 * s == sum then ret + 1 else ret
        where ret = countWays ss sum

solve xs = countWays (drop 1 (countS xs)) (sum xs)

main = do
    n <- getLine
    line <- getLine
    let xs = map read $ words $ line
    putStrLn $ show $ solve xs

main = do getLine; getLine >>= putStrLn . show . length . gao . map read . words where
	gao a = let (s:b) = scanr1 (+) a in filter (\i -> True) b

main = do getLine; getLine >>= putStrLn . show . length . gao . map read . words where
	gao a = let b = scanr1 (+) a in filter ((==last b) . (*2)) $ init b


import Data.List

modfn 0 _ _ _ as = tail $ reverse as
modfn n i  k x as = modfn (n-1) ((i+1)`mod`k) k x ((x!!i) + head as:as)

main = do
  n <- readLn :: IO Int
  as <- getLine >>= return . map (read :: String -> Int ) . words
  let x = zipWith (-) as $ 0:as
      l = filter (\a -> let b = take n $ modfn n 0 a x [0] in b == as ) [1..n]
  print $ length l
  putStrLn $ concat $ intersperse " " $ map show l

    import Data.List
    import Control.Monad
    import Data.Maybe ( fromJust )
    import qualified Data.ByteString as BS
    import qualified Data.ByteString.Char8 as BS8

    fast_read_Int = fmap (map (fst . fromJust . BS8.readInt) . BS8.words) BS.getLine :: IO [Int]

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

    check :: [Int] -> Int -> Int -> Int -> Bool
    check l i n d
                | (i+d) == n = True
                | (l!!i) == (l!!(i+d)) = check l (i+1) n d
                | otherwise = False

    brut :: Int -> Int -> Int -> [Int] -> [Int] -> [Int]
    brut _ d 0 _ list = reverse list
    brut i d n l list = brut (mod (i+1) d) d (n-1) l ((head list + l!!i ):list)

    main = do
        x <- getLine
        let n = citesc_nr x
        a <- fast_read_Int
        let b = zipWith (-) a $ 0:a
        let l = filter (\i -> brut 0 i n b [0] == (0:a) ) [1..n]
        --print b
        --print $ brut 0 1 n b [0]
        print $ length l
        putStr $ join $ intersperse " " $ map show l

    {--
    main = do
        n <- readLn :: IO Int
        a <- fast_read_Int
        let b = zipWith (-) a $ 0:a
        let l = filter (\i -> check b 0 n i == True ) [1..n]
        print $ length l
        putStrLn $ join $ intersperse " " $ map show l
    --}

-- Codeforces 1043 B
import Data.List
findXs :: [Int] -> [Int]
findXs as = zipWith (-) as (0:as)

findLoop :: [Int] -> Int -> [Int]
findLoop xs len = map fst $ foldl filterHead lists xs
    where numbers = [1..len]
          lists = map (\n -> (n, cycle (take n xs))) numbers

filterHead :: [(Int, [Int])] -> Int -> [(Int, [Int])]
filterHead lists x = [(n, ls) | (n, (l:ls)) <- lists, l == x]

main :: IO ()
main = do
    num <- getLine
    list <- getLine
    let n = read num
    let xs = findXs $ map (read :: String -> Int) (words list)
    let loops = findLoop xs n
    putStrLn $ show $ length loops
    putStrLn $ unwords $ map show loops

import Data.List
import Control.Monad
import Data.Maybe ( fromJust )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

fast_read_Int = fmap (map (fst . fromJust . BS8.readInt) . BS8.words) BS.getLine :: IO [Int]

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

check :: [Int] -> Int -> Int -> Int -> Bool
check l i n d
            | (i+d) == n = True
            | (l!!i) == (l!!(i+d)) = check l (i+1) n d
            | otherwise = False

brut :: Int -> Int -> Int -> [Int] -> [Int] -> [Int]
brut _ d 0 _ list = reverse list
brut i d n l list = brut (mod (i+1) d) d (n-1) l ((head list + list!!i ):list)

main = do
    x <- getLine
    let n = citesc_nr x
    a <- fast_read_Int
    let b = zipWith (-) a $ 0:a
    let l = filter (\i -> brut 0 i n b [0] == 0:b ) [1..n]
    putStr $ join $ intersperse " " $ map show l

{--
main = do
    n <- readLn :: IO Int
    a <- fast_read_Int
    let b = zipWith (-) a $ 0:a
    let l = filter (\i -> check b 0 n i == True ) [1..n]
    print $ length l
    putStrLn $ join $ intersperse " " $ map show l
--}


module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              , "abc\\def\\ij"
              , "abc\\jk\\lm","de"
              ]
pathList1   = [ "C:\\folder1\\file1.txt" ]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]

pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]
pathList4 = [ "F:\\g\\a\\f\\i\\k\\i\\f\\f\\f\\h\\c\\c\\f\\i\\c\\f\\b\\b\\d\\j\\a\\e\\g\\d\\d\\f\\c\\k\\i\\f\\g\\f\\e\\d\\b\\b\\e\\f\\g\\h\\b\\f\\h\\i\\chv.27"
            ,"C:\\i\\f\\j\\h\\g\\f\\i\\h\\c\\b\\c\\e\\a\\f\\k\\h\\b\\j\\g\\j\\b\\f\\j\\a\\b\\k\\d\\d\\a\\d\\b\\f\\c\\d\\h\\e\\j\\f\\f\\e\\d\\h\\i\\k\\x.vq"
            ,"E:\\i\\d\\h\\c\\a\\g\\d\\h\\i\\e\\i\\a\\e\\c\\d\\a\\a\\i\\i\\e\\e\\b\\g\\g\\d\\g\\j\\b\\..."
            ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map length) . (map $ filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map f . map split . sort)

f (x1:x2:xs) = (x1++x2) :xs

--maxSubDirNum = maximum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)
maxSubDirNum = maximum . (map length ) . (map tail) . (map (filter (not . elem '.'))) .  (map T.flatten . map buildDir . groupPath . map f . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" (dirNum) fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf
import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))
buildTree = (map T.flatten . map buildDir . groupPath . map f . map split . sort)

maxFileNum = (maximum) . (map length) . (map $ filter (elem '.')) . buildTree

f (x1:x2:xs) = (x1++x2) :xs

maxSubDirNum = maximum . (map length ) . (map tail) . (map (filter (not . elem '.'))) .  buildTree

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" (dirNum) fileNum)
          }

import Data.List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set

split l =
    let (first, second) = break (\x -> x == '\\') l in
    case second of
        h:t -> first : (split t)
        _   -> [first]

toPairs [] = []
toPairs (h:t)
    | null t = []
    | otherwise = (h, head t) : (toPairs t)

upd k v m =
    case Map.lookup k m of
        Just s  -> Map.update (\x -> Just (Set.insert v s)) k m
        Nothing -> Map.insert k (Set.singleton v) m

dfs1 m k =
    case Map.lookup k m of
        Just s  -> foldl' (+) 1 (map (dfs1 m) (Set.elems s))
        Nothing -> 0

dfs2 m k =
    case Map.lookup k m of
        Just s  -> foldl' (+) 0 (map (dfs2 m) (Set.elems s))
        Nothing -> 1

solve input =
    let list = concat (map (toPairs.drop 1.inits.split) (map (\x -> "/\\" ++ x) (lines input))) in
    let mp = foldl' (\m (first, second) -> upd first second m) (Map.empty) list in
    case Map.lookup ["/"] mp of
        Just s  -> let disks = Set.elems s in
            let rootFolders = concat (map (\x -> case Map.lookup x mp of Just y -> Set.elems y; Nothing -> []) disks) in
            let one = show (foldl' (max) 0 (map (\x -> (dfs1 mp x) - 1) rootFolders)) in
            let two = show (foldl' (max) 0 (map (dfs2 mp) rootFolders)) in
            one ++ " " ++ two
        Nothing -> "Nothing"

main = interact(solve)

import qualified Data.Set as Set
import qualified Data.List as List
splitPath s = let
    sP (r:rs) (s:ss)
        | s == '\\' = sP ([]:r:rs) ss
        | otherwise = sP ((s:r):rs) ss
    sP r [] = r in
    map reverse $ reverse $ sP [reverse $ take 3 s] $ drop 3 s
f (dirs, files, maxdirs, maxfiles, curDir) l@((d:ds):ls)
    | curDir /= d = f (Set.empty, 0, max maxdirs (Set.size dirs),
            max maxfiles files, d) l
    | otherwise = f (f1 dirs [] ds, files+1, maxdirs, maxfiles, curDir) ls
f (dirs, files, maxdirs, maxfiles, curDir) [] =
            (max maxdirs (Set.size dirs), max maxfiles files)
f1 dirs _ [_] = dirs
f1 dirs h (d:ds) = f1 (Set.insert (d:h) dirs) (d:h) ds

main = do
    l <- (map splitPath . List.sort . lines) `fmap` getContents
    let (maxdirs, maxfiles) = f (Set.empty, 0, 0, 0, "") l
    putStrLn $ unwords $ map show [maxdirs, maxfiles]

module Main where
import Control.Applicative
import Data.List
import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))
pathList    = ["abc\\def","abc\\def\\gh","abc\\def\\ij","abc\\jk\\lm","de"]

main :: IO ()
main = do { ns <- lines <$> getContents
          ; print ((map buildDir . groupPath . map split . sort) ns)
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              , "abc\\def\\ij"
              , "abc\\jk\\lm","de"
              ]
pathList1   = [ "C:\\folder1\\file1.txt" ]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]

pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]
pathList4 = [ "F:\\g\\a\\f\\i\\k\\i\\f\\f\\f\\h\\c\\c\\f\\i\\c\\f\\b\\b\\d\\j\\a\\e\\g\\d\\d\\f\\c\\k\\i\\f\\g\\f\\e\\d\\b\\b\\e\\f\\g\\h\\b\\f\\h\\i\\chv.27"
            ,"C:\\i\\f\\j\\h\\g\\f\\i\\h\\c\\b\\c\\e\\a\\f\\k\\h\\b\\j\\g\\j\\b\\f\\j\\a\\b\\k\\d\\d\\a\\d\\b\\f\\c\\d\\h\\e\\j\\f\\f\\e\\d\\h\\i\\k\\x.vq"
            ,"E:\\i\\d\\h\\c\\a\\g\\d\\h\\i\\e\\i\\a\\e\\c\\d\\a\\a\\i\\i\\e\\e\\b\\g\\g\\d\\g\\j\\b\\..."
            ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map $ length . filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map split . sort)

--maxSubDirNum = maximum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)
maxSubDirNum = maximum . (map length ).  (map (filter (not . elem '.'))) .  (map T.flatten . map buildDir . groupPath . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" (dirNum-2) fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              ,"abc\\def\\ij"
              ,"abc\\jk\\lm","de"
              ]
pathList1   = ["C:\\folder1\\file1.txt"]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]
pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map $ length . filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map split . sort)

maxSubDirNum = sum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" dirNum fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              , "abc\\def\\ij"
              , "abc\\jk\\lm","de"
              ]
pathList1   = [ "C:\\folder1\\file1.txt" ]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]

pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]
pathList4 = [ "F:\\g\\a\\f\\i\\k\\i\\f\\f\\f\\h\\c\\c\\f\\i\\c\\f\\b\\b\\d\\j\\a\\e\\g\\d\\d\\f\\c\\k\\i\\f\\g\\f\\e\\d\\b\\b\\e\\f\\g\\h\\b\\f\\h\\i\\chv.27"
            ,"C:\\i\\f\\j\\h\\g\\f\\i\\h\\c\\b\\c\\e\\a\\f\\k\\h\\b\\j\\g\\j\\b\\f\\j\\a\\b\\k\\d\\d\\a\\d\\b\\f\\c\\d\\h\\e\\j\\f\\f\\e\\d\\h\\i\\k\\x.vq"
            ,"E:\\i\\d\\h\\c\\a\\g\\d\\h\\i\\e\\i\\a\\e\\c\\d\\a\\a\\i\\i\\e\\e\\b\\g\\g\\d\\g\\j\\b\\..."
            ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map $ length . filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map split . sort)

--maxSubDirNum = maximum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)
maxSubDirNum = n
    where n = maximum . (map length) .  (map  (concatMap $(filter $ not . null) . (filter (not . elem '.'))) ) .  (map T.levels . map buildDir . groupPath . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" (dirNum-2) fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Data.Maybe
-- import Data.Tree
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

main :: IO ()
main = do { ns <- lines <$> getContents
          ; print  (map (f . split) ns)
          }

module Main where
import Control.Applicative
import Data.List
import Data.Maybe

main :: IO ()
main = do { ns <- lines <$> getContents
          ; print ns
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              , "abc\\def\\ij"
              , "abc\\jk\\lm","de"
              ]
pathList1   = [ "C:\\folder1\\file1.txt" ]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]

pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]
pathList4 = [ "F:\\g\\a\\f\\i\\k\\i\\f\\f\\f\\h\\c\\c\\f\\i\\c\\f\\b\\b\\d\\j\\a\\e\\g\\d\\d\\f\\c\\k\\i\\f\\g\\f\\e\\d\\b\\b\\e\\f\\g\\h\\b\\f\\h\\i\\chv.27"
            ,"C:\\i\\f\\j\\h\\g\\f\\i\\h\\c\\b\\c\\e\\a\\f\\k\\h\\b\\j\\g\\j\\b\\f\\j\\a\\b\\k\\d\\d\\a\\d\\b\\f\\c\\d\\h\\e\\j\\f\\f\\e\\d\\h\\i\\k\\x.vq"
            ,"E:\\i\\d\\h\\c\\a\\g\\d\\h\\i\\e\\i\\a\\e\\c\\d\\a\\a\\i\\i\\e\\e\\b\\g\\g\\d\\g\\j\\b\\..."
            ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map $ length . filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map split . sort)

--maxSubDirNum = maximum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)
maxSubDirNum = maximum . (map length ).  (map $ tail . tail) . (map (filter (not . elem '.'))) .  (map T.flatten . map buildDir . groupPath . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" (dirNum) fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              , "abc\\def\\ij"
              , "abc\\jk\\lm","de"
              ]
pathList1   = [ "C:\\folder1\\file1.txt" ]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]

pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]
pathList4 = [ "F:\\g\\a\\f\\i\\k\\i\\f\\f\\f\\h\\c\\c\\f\\i\\c\\f\\b\\b\\d\\j\\a\\e\\g\\d\\d\\f\\c\\k\\i\\f\\g\\f\\e\\d\\b\\b\\e\\f\\g\\h\\b\\f\\h\\i\\chv.27"
            ,"C:\\i\\f\\j\\h\\g\\f\\i\\h\\c\\b\\c\\e\\a\\f\\k\\h\\b\\j\\g\\j\\b\\f\\j\\a\\b\\k\\d\\d\\a\\d\\b\\f\\c\\d\\h\\e\\j\\f\\f\\e\\d\\h\\i\\k\\x.vq"
            ,"E:\\i\\d\\h\\c\\a\\g\\d\\h\\i\\e\\i\\a\\e\\c\\d\\a\\a\\i\\i\\e\\e\\b\\g\\g\\d\\g\\j\\b\\..."
            ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map $ length . filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map split . sort)

--maxSubDirNum = maximum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)
maxSubDirNum = maximum . (map length ).  (map $ tail . tail) . (map (filter (not . elem '.'))) .  (map T.flatten . map buildDir . groupPath . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" (dirNum-2) fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Text.Printf

import Data.Function (on)
import Data.Maybe
import qualified Data.Tree  as T
import qualified Data.Map as M

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

f xs = ((init xs),(last xs))
g (x,a) (ys,bs) = (([x] `union` ys), ([a] `union` bs))

buildDir = T.unfoldTree phi
    where phi (n,ps) = (delete '\\' n, groupPath $ filter (not . null) ps)

-- binapp o f x y = f x `o` f y
groupPath   = map grouping . groupBy (on eqpath head)
grouping ss = (head . head $ ss, map tail ss)
eqpath      = on (==) (fst . (break ('\\'==)))

pathList    = [ "abc\\def"
              , "abc\\def\\gh"
              ,"abc\\def\\ij"
              ,"abc\\jk\\lm","de"
              ]
pathList1   = ["C:\\folder1\\file1.txt"]
pathList2   = [ "C:\\folder1\\folder2\\folder3\\file1.txt"
              , "C:\\folder1\\folder2\\folder4\\file1.txt"
              , "D:\\folder1\\file1.txt"
              ]
pathList3   = [ "C:\\file\\file\\file\\file\\file.txt"
              , "C:\\file\\file\\file\\file2\\file.txt"
              ]

z = map (filter (not . elem '.'))

maxFileNum = (maximum) . (map $ length . filter (elem '.')) . (map T.flatten . map buildDir . groupPath . map split . sort)

maxSubDirNum = maximum . (map $ sum . map length) . (map  (map (filter (not . elem '.')) . tail . tail) .map T.levels . map buildDir . groupPath . map split . sort)

main :: IO ()
main = do { ns <- lines <$> getContents
          ; let dirNum = maxSubDirNum ns in
            let fileNum = maxFileNum ns in
            (printf "%d %d\n" dirNum fileNum)
          }

module Main where
import Control.Applicative
import Data.List
import Data.Maybe

splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
   where
     (a, b) = break p xs

split :: String -> [String]
split = splitBy (=='\\')

main :: IO ()
main = do { ns <- lines <$> getContents
          ; print (map split ns)
          }


main = interact $ format . solve . lines
solve [a,b] = map vowel a == map vowel b
vowel = (`elem` "aeiou")
format True = "Yes"
format False= "No"

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

trim = B.filter (/='\r')
aeiou = "aeiou"
aeiou' = ['a'..'z'] \\ aeiou
main = ((\x y ->
    if  (B.length x == B.length y) && (all (== True) $
            B.zipWith
                (\x y ->
                    (x `elem` aeiou  && y `elem` aeiou) ||
                    (x `elem` aeiou' && y `elem` aeiou')
                ) x y )
        then "YES"
        else "NO"
    ) <$> (trim <$> B.getLine) <*> (trim <$> B.getLine) ) >>= putStrLn

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

trim = B.filter (/=' ')
aeiou = "aeiou"
aeiou' = ['a'..'z']\\aeiou
main = ((\x y ->
    if all (== (B.length x == B.length y)) $ B.zipWith (\x y -> (x `elem` aeiou && y `elem` aeiou) || (x `elem` aeiou' && y `elem` aeiou')) x y
        then "YES"
        else "NO"
    ) <$> (trim <$> B.getLine) <*> (trim <$> B.getLine) ) >>= putStrLn

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

aeiou = "aeiou"
aeiou' = ['a'..'z']\\aeiou
main = ((\x y ->
    if all (== (B.length x == B.length y)) $ B.zipWith (\x y -> (x `elem` aeiou && y `elem` aeiou) || (x `elem` aeiou' && y `elem` aeiou')) x y
        then "YES"
        else "NO"
    ) <$> B.getLine <*> B.getLine ) >>= putStrLn


import Control.Arrow
main = interact $ show . solve . lines
solve [a,b] = go (reverse a) (reverse b) where
  go (a:as) (b:bs) | a == b = go as bs
  go as bs = length as + length bs

import Data.List

toint s = (read s) :: Integer

doit [] = 0
doit ((x,y):xs) =
	if x == y then
		1 + doit xs
	else
		0

solve::String -> String
solve ss =
	let [s,t] = map reverse (words ss) in
	show (length s + length t - 2 * doit (zip s t)) ++ "\n"

main = do
  interact $ solve

-- Codeforces 1005B
import Data.List

deleteFromRight :: (String, String) -> (String, String)
deleteFromRight ([], s2) = ([], s2)
deleteFromRight (s1, []) = (s1, [])
deleteFromRight (x1:xs1, x2:xs2)
    | x1 == x2 = deleteFromRight (xs1, xs2)
    | otherwise = (x1:xs1, x2:xs2)

rest :: (String, String) -> Int
rest (s1, s2) = (\(a, b) -> (length a) + (length b)) $ deleteFromRight (reverse s1, reverse s2)

main :: IO ()
main = do
    s1 <- getLine
    s2 <- getLine
    print (rest (s1, s2))

f [] a = length a
f a [] = length a
f (a:as) (b:bs) | a==b = f as bs
f as bs = length as + length bs

main = do
    s <- getLine
    t <- getLine
    print $ f (reverse t) (reverse s)

import Data.List

f::String->String->Int
f "" _ =0
f _ ""=0
f (x:xs) (y:ys)
      |x/=y=0
      |otherwise=2 + (f xs ys)

main = do
       e1<-getLine
       e2<-getLine
       let t=(length e1)+(length e2)
       print $ t - (f (reverse e1) (reverse e2))

main = interact $ pureMain

pureMain :: String -> String
pureMain x = show $ length s + length t - 2 * f (reverse s) (reverse t)
  where
    [s, t] = lines x
    f _ [] = 0
    f [] _ = 0
    f (a:as) (b:bs) | a == b = 1 + f as bs
                    | otherwise = 0

import Control.Arrow
main = interact $ show . solve . lines
solve [a,b] = uncurry (+) $ length *** length $ unzip $ dropWhile (uncurry (==)) $ zip (reverse a) (reverse b)


{-# LANGUAGE LambdaCase #-}

import Control.Applicative (liftA2)
import Control.Arrow ((>>>))
import Control.Monad (replicateM)
import Control.Monad.State (State, evalState, get, gets, put)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.List
import Data.Maybe

main :: IO ()
main = C.interact $ runScanner (numberOf input) >>> map (solve >>> output) >>> C.unlines

type Input = (Int, String)

type Output = [Int]

input :: Scanner Input
input = pair int str

output :: Output -> C.ByteString
output = C.pack . unwords . map show

solve :: Input -> Output
solve (n, s)
  | isJust iz = let r = h + fromJust iz in [1, r + 1, 1, r]
  | odd n = [h + 2, n, h + 1, n - 1]
  | s !! (h - 1) == '0' = [h + 1, n, h, n]
  | otherwise = [h + 1, n, h, n - 1]
  where
    h = n `div` 2
    iz = elemIndex '0' (drop h s)

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

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

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

peek :: Scanner C.ByteString
peek = gets head

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

str :: Scanner String
str = C.unpack <$> bstr

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

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

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

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

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

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

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

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

(><) = times

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

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

showB :: Show a => a -> C.ByteString
showB = C.pack . show

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

count1 :: String -> Int
count1 = foldr (\a b -> if a == '1' then (b+1) else b) 0

calc :: Int -> String -> (Int,Int,Int,Int)
calc l x =
  let m = (l `div` 2)
      index0 = findIndex (\a -> a == '0') x
  in
    case index0 of
      Just k | k < m -> (k+2,l,k+1,l)
      Just k | otherwise -> (1,k+1,1,k)
      Nothing -> (1,l-1,2,l)

main = do
  line <- getLine
  let t = read line :: Int
  forM_ [1..t] $ \_ -> do
    line <- getLine
    let l = read line :: Int
    line <- getLine
    let (l1,r1,l2,r2) = calc l line
    putStr $ show l1
    putStr " "
    putStr $ show r1
    putStr " "
    putStr $ show l2
    putStr " "
    putStrLn $ show r2

import Data.List
import Data.Char (digitToInt)

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf x l  = take x l : chunksOf x (drop x l)

toArr :: Read a => String -> [a]
toArr = map read . words

type InType  = String
type OutType = ((Int, Int), (Int, Int))

inpLines :: Int
inpLines = 2

solve :: InType -> OutType
solve n
  | null zeroes = ((1, len - 1), (2, len))
  | i > len `div` 2 = ((1, i), (1, i - 1))
  | otherwise = ((i, len), (i + 1, len))
  where
    zeroes = elemIndices '0' n
    len = length n
    i = last zeroes + 1

receive :: [String] -> InType
receive [_, n] = n
receive _   = error "unexpected input"

main :: IO ()
main = interact $
  unlines . map (showAns . solve . receive) . chunksOf inpLines . tail . lines
    where
      showAns ((l1, r1), (l2, r2)) = unwords (map show [l1,r1,l2,r2])

import Data.List
import Data.Char (digitToInt)

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf x l  = take x l : chunksOf x (drop x l)

toArr :: Read a => String -> [a]
toArr = map read . words

type InType  = String
type OutType = ((Int, Int), (Int, Int))

inpLines :: Int
inpLines = 2

solve :: InType -> OutType
solve n
  | null zeroes = ((1, len - 1), (2, len))
  | i - 1 > len `div` 2 = ((1, i), (1, i - 1))
  | otherwise = ((i, len), (i + 1, len))
  where
    zeroes = elemIndices '0' n
    len = length n
    i = last zeroes + 1

receive :: [String] -> InType
receive [_, n] = n
receive _   = error "unexpected input"

main :: IO ()
main = interact $
  unlines . map (showAns . solve . receive) . chunksOf inpLines . tail . lines
    where
      showAns ((l1, r1), (l2, r2)) = unwords (map show [l1,r1,l2,r2])

import Data.List
import Data.Char (digitToInt)

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf x l  = take x l : chunksOf x (drop x l)

toArr :: Read a => String -> [a]
toArr = map read . words

type InType  = String
type OutType = ((Int, Int), (Int, Int))

inpLines :: Int
inpLines = 2

solve :: InType -> OutType
solve n
  | null zeroes = ((1, len - 1), (2, len))
  | i > len `div` 2 = ((1, i), (1, i - 1))
  | otherwise = ((i, len), (i + 1, len))
  where
    zeroes = elemIndices '0' n
    len = length n
    i = last zeroes

receive :: [String] -> InType
receive [_, n] = n
receive _   = error "unexpected input"

main :: IO ()
main = interact $
  unlines . map (showAns . solve . receive) . chunksOf inpLines . tail . lines
    where
      showAns ((l1, r1), (l2, r2)) = unwords (map show [l1,r1,l2,r2])


main = interact $ show . solve . pairs . map read . tail . words
pairs (a:b:xs) = (a,b) : pairs xs
pairs [] = []
solve = maximum . map (uncurry (+))

f :: (Integer, Integer) -> Integer
f (a, b) = a + b

convert :: [a] -> [(a,a)]
convert [] = []
convert (x1:x2:xs) = (x1,x2):convert xs

main = interact $   (show.maximum.map f.convert.tail.map read.words)

{-# LANGUAGE Safe #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.State
import Control.Applicative
import Control.Arrow
import Data.List
import Data.Array
import Data.Array.IO.Safe
import Data.Array.ST.Safe
import Data.IORef
import Data.STRef
import Data.Maybe
import Data.Bits
import Data.Ord
import Data.Ratio
import Data.Int
import Data.Char
import qualified Data.IntMap as IM
import qualified Data.ByteString.Char8 as BS
import qualified Data.Set as Set
import qualified Data.Sequence as Sq
import Data.Tuple
import Data.Function
import Text.Printf
--import Debug.Trace
import Numeric

readIntBS = fst . fromJust . BS.readInt

infixr 1 ?
(?) :: Bool -> (a,a) -> a
(?) b t = (if b then fst else snd) t

main = do
  n <- readLn :: IO Int
  ps <- map (map readIntBS . BS.words) . BS.lines <$> BS.getContents :: IO [[Int]]

  let m = maximum [x+y | [x,y] <- ps]

  print $ m

-- Codeforces 1047 B
main :: IO ()
main = do
    n <- getLine >>= return.read :: IO Int
    number <-getContents >>= return.(take n).lines
                         >>= return.map (sum.(map read).words):: IO [Int]
    putStrLn $ show $ maximum number

import Data.List
import Data.Char

f::String->Int
f x=let (a:b:[])=map read (words x)::[Int]
    in a+b

main = do
       e1<-getLine
       es<-getContents
       let xs=lines es
       putStr $ show $ maximum $ map f xs

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

get = fmap fst . mapMaybe C.readInt . C.words <$> C.getLine

main = sol <$> (flip replicateM get =<< readLn) >>= print

sol :: [[Int]] -> Int
sol = maximum . fmap sum

import           Control.Monad

main :: IO ()
main = do
  n <- (read :: String -> Int) <$> getLine
  print =<< maximum . map (sum . map (read :: String -> Int) . words) <$> replicateM n getLine


{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Char

main = do
  getLine
  str <- getLine
  let ns = map (\n -> (read n) :: Integer) $ words str
  printResult $ findCircle ns

findCircle :: [Integer] -> Maybe [Integer]
findCircle ns =
  let (n1:n2:n3:ns') = reverse (sort ns) in
  let ns'' = n3 : n1 : n2 : ns' in
  if n1 < (n2+n3) then Just ns'' else Nothing

printResult :: Maybe [Integer] -> IO ()
printResult (Just ns) = putStrLn "YES" >> (putStrLn $ unwords $ map show ns)
printResult Nothing = putStrLn "NO"

import Data.List

printInt::Int->IO()
printInt x=do
    putStr(show x)
    putChar(' ')
printArr t=mapM_ printInt t

main::IO()
main=do
    n<-readLn::IO Int
    s<-getLine
    let t=sort((map read (words s))::[Int])
    let last1=t!!(n-1)
    let last2=t!!(n-2)
    let last3=t!!(n-3)
    if(last1>=last2+last3)
    then putStrLn "NO"
    else do
        putStrLn "YES"
        printArr (fst (splitAt (n-3) t))
        printInt last2
        printInt last1
        printInt last3

import Data.List

solve :: [Int] -> Maybe [Int]
solve l
   | a < b + c = Just $ b:a:c:ls
   | otherwise = Nothing
   where a:b:c:ls = reverse . sort $ l

toIntList :: String -> [Int]
toIntList = (map read) . words

fromIntList :: [Int] -> String
fromIntList = unwords . (map show)

main :: IO ()
main = do
    _ <- getLine
    l <- toIntList <$> getLine
    let res = solve l
        out = case res of
                 Just l -> "YES\n" ++ fromIntList l
                 Nothing -> "NO"
    putStrLn out

import Control.Monad
import Data.List

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

takeOdd [] = []
takeOdd [x] = [x]
takeOdd (x:_:xs) = x:takeOdd xs

takeEven [] = []
takeEven (_:xs) = takeOdd xs

output = putStrLn <$> unwords <$> map show

main = do
  getLine
  a <- input :: IO [Int]
  let s@(x:y:z:_) = reverse $ sort a
  if x >= y + z then
    putStrLn "NO"
  else do
    putStrLn "YES"
    output $ takeEven s ++ reverse (takeOdd s)

{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Char

main = do
  _ <- getLine
  str <- getLine
  let ns = map (\n -> (read n) :: Int) $ words str
  printResult $ findCircle ns

findCircle :: [Int] -> Maybe [Int]
findCircle ns =
  let (n1:n2:n3:ns') = reverse (sort ns) in
  let ns'' = n3 : n1 : n2 : ns' in
  if isGood ns'' then Just ns'' else Nothing

isGood :: [Int] -> Bool
isGood (x1:x2:x3:[]) = (x1 < (x2 + x3)) && (x2 < (x1 + x3)) && (x3 < (x1+x2))
isGood (x1:xs) = isGood' x1 xs
isGood' f (x1:x2:[]) = x2 < (x1 + f)
isGood' f (x1:x2:xs) = x2 < (x1 + (head xs)) && isGood' f (x2 : xs)

printResult :: Maybe [Int] -> IO ()
printResult (Just ns) = putStrLn "YES" >> (putStrLn $ unwords $ map show ns)
printResult Nothing = putStrLn "NO"

{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Char

main = do
  getLine
  str <- getLine
  let ns = map (\n -> (read n) :: Integer) $ words str
  printResult $ findCircle ns

findCircle :: [Integer] -> Maybe [Integer]
findCircle ns =
  let (n1:n2:n3:ns') = reverse (sort ns) in
  let ns'' = n3 : n1 : n2 : ns' in
  if n1 >= (n2+n3) then Just ns'' else Nothing

printResult :: Maybe [Integer] -> IO ()
printResult (Just ns) = putStrLn "YES" >> (putStrLn $ unwords $ map show ns)
printResult Nothing = putStrLn "NO"

{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Char

main = do
  _ <- getLine
  str <- getLine
  let ns = map (digitToInt . head) $ words str
  printResult $ findCircle ns

findCircle :: [Int] -> Maybe [Int]
findCircle ns =
  let (n1:n2:n3:ns') = reverse (sort ns) in
  let ns'' = n3 : n1 : n2 : ns' in
  if isGood ns'' then Just ns'' else Nothing

isGood :: [Int] -> Bool
isGood (x1:xs) = isGood' x1 xs
isGood' f (x1:x2:[]) = x2 < (x1 + f)
isGood' f (x1:x2:xs) = x2 < (x1 + (head xs)) && isGood' f (x2 : xs)

printResult :: Maybe [Int] -> IO ()
printResult (Just ns) = putStrLn "YES" >> (putStrLn $ foldr (\n str -> (intToDigit n) : str) [] ns)
printResult Nothing = putStrLn "NO"

{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Char

main = do
  _ <- getLine
  str <- getLine
  let ns = map (\n -> (read n) :: Int) $ words str
  printResult $ findCircle ns

findCircle :: [Int] -> Maybe [Int]
findCircle ns =
  let (n1:n2:n3:ns') = reverse (sort ns) in
  let ns'' = n3 : n1 : n2 : ns' in
  if isGood ns'' then Just ns'' else Nothing

isGood :: [Int] -> Bool
isGood (x1:xs) = isGood' x1 xs
isGood' f (x1:x2:[]) = x2 < (x1 + f)
isGood' f (x1:x2:xs) = x2 < (x1 + (head xs)) && isGood' f (x2 : xs)

printResult :: Maybe [Int] -> IO ()
printResult (Just ns) = putStrLn "YES" >> (putStrLn $ unwords $ map show ns)
printResult Nothing = putStrLn "NO"

{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Char

main = do
  _ <- getLine
  str <- getLine
  let ns = map (digitToInt . head) $ words str
  printResult $ findCircle ns

findCircle :: [Int] -> Maybe [Int]
findCircle ns =
  let (n1:n2:n3:ns') = reverse (sort ns) in
  let ns'' = n3 : n1 : n2 : ns' in
  if isGood ns'' then Just ns'' else Nothing

isGood :: [Int] -> Bool
isGood (x1:xs) = isGood' x1 xs
isGood' f (x1:x2:[]) = x2 < (x1 + f)
isGood' f (x1:x2:xs) = x2 < (x1 + (head xs)) && isGood' f (x2 : xs)

printResult :: Maybe [Int] -> IO ()
printResult (Just ns) = putStrLn "YES" >> (putStrLn $ foldr (\n str -> (intToDigit n) : ' ' : str) [] ns)
printResult Nothing = putStrLn "NO"


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

solve :: IO ()
solve = readInts >> readInts >>=
    print . length . filter id . zipWith (==) [1..] . scanl1 max

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

main :: IO ()
main = flip replicateM_ solve . head =<< readInts

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

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

import qualified Data.Set as Set

mainFun :: SP Builder
mainFun = do
  t <- getInt
  fmap mconcat $ replicateM t $ do
    n <- getInt
    p <- replicateM n getInt
    let
      ss = scanl' (flip Set.insert) (Set.singleton 0) p
      oks = [() | v <- ss, Set.size v == Set.findMax v + 1]
    pure $ putInts [length oks - 1]

type SP = State P.ByteString
type S = StateT P.ByteString

dropSpace :: P.ByteString -> P.ByteString
dropSpace = P.dropWhile (<= ' ') -- not exactly right, but close enough

--getNext :: Monad m => S m P.ByteString
--getNext = state $ P.span (> ' ') . dropSpace

getInt :: Monad m => S m Int
getInt = state $ fromJust . P.readInt . dropSpace

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

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


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

main = do
	[ [ c1, c2, c3, c4 ] , _, as, bs ] <- replicateM 4 $ do
		map read . words <$> getLine

	let
		f a = min ( c1 * a ) c2

	print $ minimum [ c4, c3 * 2, min c3 ( sum ( map f as ) ) + min c3 ( sum ( map f bs ) ) ]

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

main = do
	[ [ c1, c2, c3, c4 ] , _, as, bs ] <- replicateM 4 $ do
		map read . words <$> getLine

	let
		f a = min ( c1 * a ) c2

	print $ minimum [ c4, c3 * 2, c3 + sum ( map f as ) , c3 + sum ( map f bs ), sum ( map f as ) + sum ( map f bs ) ]

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

main = do
	[ [ c1, c2, c3, c4 ] , _, as, bs ] <- replicateM 4 $ do
		map read . words <$> getLine

	let
		f a = min ( c1 * a ) c2

	print $ min c4 $ min c3 ( sum ( map f as ) ) + min c3 ( sum ( map f bs ) )

import Control.Monad (liftM)
import Data.Char (ord)
import Data.List (intercalate)

solve :: [Int] -> [Int] -> [Int] -> Int
solve [c1, c2, c3, c4] as ts  =  min c4 (min as' c3 + min ts' c3)
  where
    as'  =  sum $ map (\a -> min (a * c1) c2) as
    ts'  =  sum $ map (\t -> min (t * c1) c2) ts

main :: IO ()
main  =  do
    cs  <-  reads
    getLine
    as  <-  reads
    ts  <-  reads
    print $ solve cs as ts

  where
    reads :: Num a => IO [a]
    reads  =  liftM (map read . words) getLine
      where
        read ('-':s)  =  (-1) * read s
        read s  =  read' 0 s
        read' a ""  =  a
        read' a (c:s)  =  read' (10 * a + fromIntegral (ord c - ord '0')) s

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

import Control.Applicative

solve :: [Int] -> Int -> Int -> [Int] -> [Int] -> Int
solve (c1:(c2:(c3:[c4]))) n m a1 a2
  = min c4 (optimal1 + optimal2)
    where
      optimal1 = min c3 ( sum $ map get_better a1 )
      optimal2 = min c3 ( sum $ map get_better a2 )

      get_better x = min (x*c1) c2

main :: IO ()
main = do
  lin1 <- getLine
  let costs = map read (words lin1)

  [n,m] <- map read <$> words <$> getLine

  a1 <- map read <$> words <$> getLine
  a2 <- map read <$> words <$> getLine

  print $ solve costs n m a1 a2

minNum k n
	| n == 0 && k == 1 = 0
	| n == 0 = -1
	| otherwise = (10 ^ (k-1) ) + (n-1)

find x
	| x>=0  = show x
	| otherwise = "No solution"

parse :: String -> [Int]
parse l = map read $ words l

cal :: [Int] -> [Int] -> [Int] -> Int
cal cs bs ts =
	let c4 = cs !! 3
	in
	  min c4 $ (cal1 cs bs) + (cal1 cs ts)

cal1::  [Int] -> [Int] -> Int
cal1 cs bs =
	let c3 = cs !! 2
	in
	  min c3 $ foldl1 (+) $ (cal2 cs bs)

cal2::  [Int] -> [Int] -> [Int]
cal2 cs bs =
	let c1 = cs !! 0
	    c2 = cs !! 1
	in map (\x -> min (c1 * x) c2 ) bs

main = do
	l1 <- getLine
	l2 <- getLine
	l3 <- getLine
	l4 <- getLine
	putStr $ show $ cal (parse l1) (parse l3) (parse l4)

multa :: [Int] -> Int -> [Int]
multa list a = [a * x | x <- list]

minlista :: [Int] -> Int -> [Int]
minlista list a = [min a x | x <- list]

getans :: [Int] -> [Int] -> [Int] -> Int
getans bs tr (c1:c2:c3:c4:_) = min c4 ((min (sum (minlista (multa bs c1) c2)) c3) + min (sum (minlista (multa tr c1) c2)) c3)

main = do
	c14 <- getLine
	nm <- getLine
	buses <- getLine
	trolley <- getLine
	putStr $ show $ getans (map read (words buses)) (map read (words trolley)) (map read (words c14))