import Control.Monad
import Data.Int
import Data.Maybe
import Data.Ratio
import Text.Printf
import qualified Data.ByteString.Char8 as B

intLine :: IO [Integer]
intLine = fmap (map (fromIntegral . fst . fromJust . B.readInt) . B.words) B.getLine

main = do
    [n,w,v,u] <- intLine
    ts <- replicateM (fromIntegral n) $ do
        [x,y] <- intLine
        return $ x % v - y % u;
    let t = if minimum ts >= 0 then 0 else max 0 (maximum ts)
    let res = t + w % u
    putStrLn $ printf "%.12f" $ (fromRational res :: Double)

module Main where

import Control.Monad
import Control.Monad.State
import Data.Int
import Data.List (foldl', sortBy)
import Text.Printf
import qualified Data.ByteString.Lazy.Char8 as L

type Velocity = Int64

data Point = Point { getX :: Int64
                   , getY :: Int64
                   } deriving (Show, Eq)

ordByY :: Point -> Point -> Ordering
ordByY (Point x1 y1) (Point x2 y2) | y1 < y2 = LT
                                   | y1 > y2 = GT
                                   | otherwise = compare x1 x2

data Bus = Bus [Point] Velocity
         deriving (Show, Eq)

data Test = Test { bus :: Bus
                 , pedestrian :: Velocity
                 , width :: Int64
                 } deriving (Show, Eq)

fastPath :: Test -> Double
fastPath (Test (Bus ps v) u w) | all inTime ps = (fromIntegral w) / (fromIntegral u)
                               | otherwise = -1.0
    where inTime (Point x y) = y * v <= x * u

slowPath :: Test -> Double
slowPath (Test (Bus ps v) u w) = pt + (fromIntegral $ w - py) / (fromIntegral u)
    where (py, pt) = foldl' update (0, 0.0) (sortBy ordByY ps)

          update :: (Int64, Double) -> Point -> (Int64, Double)
          update (py, pt) (Point bx by) = (by, max bt pt')
              where bt = (fromIntegral bx) / (fromIntegral v)
                    pt' = pt + (fromIntegral $ by - py) / (fromIntegral u)

solve :: Test -> Double
solve test | fp > -1.0 = fp
           | otherwise = sp
    where fp = fastPath test
          sp = slowPath test

test0 = Test { bus = Bus [Point 1 2, Point 3 1, Point 4 3, Point 3 4, Point 1 4] 1
             , pedestrian = 2
             , width = 5
             }

type Tokenizer = State [L.ByteString]

nextInt :: Tokenizer Int
nextInt = state $ \(s:ss) -> case L.readInt s of
                               Just (x, _) -> (x, ss)
                               Nothing -> error $ "Can't parse Int:" ++ (show s)

nextInt64 :: Tokenizer Int64
nextInt64 = liftM fromIntegral nextInt

go :: Tokenizer Double
go = do
  n <- nextInt
  w <- nextInt64
  v <- nextInt64
  u <- nextInt64
  ps <- replicateM n (liftM2 Point nextInt64 nextInt64)
  return $ solve Test { bus = Bus ps v, pedestrian = u, width = w }

main :: IO ()
main = do
  input <- L.getContents
  let result = evalState go (L.words input)
  printf "%.7f\n" result

import Control.Monad
import Data.Int
import Data.Maybe
import Data.Ratio
import Text.Printf
import qualified Data.ByteString.Char8 as B

intLine :: IO [Integer]
intLine = fmap (map (fromIntegral . fst . fromJust . B.readInt) . B.words) B.getLine

main = do
    [n,w,v,u] <- intLine
    ts <- replicateM (fromIntegral n) $ do
        [x,y] <- intLine
        return $ fromIntegral x - fromIntegral v * y % u;
    let t = if minimum ts >= 0 then 0 else maximum ts
    let res = t + w % u
    putStrLn $ printf "%.12f" $ (fromRational res :: Double)

import Control.Monad
import Data.Int
import Data.Maybe
import Data.Ratio
import Text.Printf
import qualified Data.ByteString.Char8 as B

intLine :: IO [Integer]
intLine = fmap (map (fromIntegral . fst . fromJust . B.readInt) . B.words) B.getLine

main = do
    [n,w,v,u] <- intLine
    ts <- replicateM (fromIntegral n) $ do
        [x,y] <- intLine
        return $ fromIntegral x - fromIntegral v * y % u;
    let t = if minimum ts >= 0 then 0 else max 0 (maximum ts)
    let res = t + w % u
    putStrLn $ printf "%.12f" $ (fromRational res :: Double)


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

{-# language NoMonomorphismRestriction #-}

{-# options_ghc -Weverything                     #-}

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

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

import Prelude
import Control.Monad

main :: IO ()
main = do
    t <- readLn
    tcs <- replicateM t readTC

    (putStr . unlines . map solve) tcs

readTC = do
    [l, r] <- readIListLn
    return (l, r)

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

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

solve (l, r) = show $ sum . take 20 $ ps
  where
    ps = zipWith (flip (-)) (iterate (`div` 10) l) (iterate (`div` 10) r)

import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $
  lines >>> drop 1 >>> map (words >>> map read >>> solve >>> show) >>> unlines

solve :: [Int] -> Int
solve [l, r] = cost r - cost l
  where
    cost = sum . takeWhile (> 0) . iterate (`div` 10)

--prewritten code: https://github.com/antma/algo
{-# LANGUAGE BangPatterns #-}
{-# Options_GHC -O2 #-}
import Data.ByteString.Builder
import Data.Monoid
import System.IO
import qualified Data.ByteString.Char8 as C
import Data.Time (diffTimeToPicoseconds, utctDayTime, getCurrentTime)
import Data.Char
import Data.Int

ru, ri :: C.ByteString -> Int

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

cnt :: Int -> Int64
cnt x = f (fromIntegral x) x
  where
    f !acc 0 = acc
    f !acc !x = let !t = div x 10 in f (acc + fromIntegral t) t

test' :: Int -> Int -> Int64
test' l r = cnt r - cnt l

nl = char7 '\n'
test :: Builder -> Int -> [Int] -> Builder
test buf 0 _ = buf
test buf nt (a:b:xs) = test (buf <> int64Dec (test' a b) <> nl) (pred nt) xs

solve :: [Int] -> Builder
solve (nt:xs) = test mempty nt xs

main :: IO ()
main = hPutBuilder stdout . solve . map ri . C.words =<< C.getContents


{-# LANGUAGE BlockArguments #-}

import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.List

main :: IO ()
main = do
  [t] <- getInts
  replicateM_ t do
    [n, _] <- getInts
    matrix <- replicateM n getInts
    let steps = free matrix `min` free (transpose matrix)
        winner = ["Vivek", "Ashish"] !! mod steps 2
    putStrLn winner
  where
    free = length . filter (not . elem 1)

getInts :: IO [Int]
getInts = go <$> BS.getLine
  where
    go s = case BS.readInt s of
      Nothing -> []
      Just (x, s') -> x : go (BS.drop 1 s')

import Control.Monad
import Data.Array

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

routine :: IO ()
routine = do
	[n,m] <- (map read.words) <$> getLine
	xs <- replicateM n ((map read.words) <$> getLine)
	putStrLn $ if solve n m xs then "Vivek" else "Ashish"

solve :: Int -> Int -> [[Int]] -> Bool
solve n m xs = even $ min ns ms
	where
		arr = listArray ((1,1),(n,m)) $ concat xs
		ns = length $ filter not [ any (== 1) [ arr!(i,j) | j <- [1..m]] | i <- [1..n]]
		ms = length $ filter not [ any (== 1) [ arr!(i,j) | i <- [1..n]] | j <- [1..m]]

import           Control.Monad
import           Data.List

main :: IO()
main = do
  t <- readLn
  replicateM_ t solve

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

countOptions :: [[Int]] -> Int
countOptions []     = 0
countOptions (x:xs) = (if all (==0) x then 1 else 0) + countOptions xs

solve :: IO()
solve = do
  [n,m] <- readInts
  r <- replicateM n readInts
  let c = transpose r
  let rZeros = countOptions r
  let cZeros = countOptions c
  let zeros = min rZeros cZeros
  putStrLn (if even zeros then "Vivek" else "Ashish")

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

import Data.List

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

split = _split [] ""

(|>) = flip ($)

boolToInt bool
  | bool = 1
  | otherwise = 0

main = do
  input <- getContents
  let _:vals = read <$> split input :: [Int]
      res = vals |> unfoldr (\case
        []  -> Nothing
        n:m:rest0   ->
          let (cells, rest1) = splitAt (n*m) rest0
              rows = cells |> unfoldr (\case
                []  -> Nothing
                ls  -> Just $ splitAt m ls
                )
              numAvailableRows = sum $ boolToInt <$> isZeroRows where
                isZeroRows = all (== 0) <$> rows
              numAvailableCols = sum $ zeroCols where
                zeroCols = (\x -> 1-x) <$> oneCols
                oneCols = foldl (\acc ls -> uncurry max <$> zip acc ls) allZeros rows where
                  allZeros = replicate m 0
              numTurns = min numAvailableRows numAvailableCols
              subres = if numTurns `mod` 2 == 1 then "Ashish" else "Vivek"
          in Just (subres, rest1)
        )
  sequence_ (putStrLn <$> res)

import Control.Monad
import Data.List

solve :: [[Int]] -> String
solve x = let free_rows = countTrue (map isArrayFree x)
              free_cols = countTrue (map isArrayFree $ transpose x) in
                  if even (min free_rows free_cols)
                      then "Vivek"
                      else "Ashish"

countTrue :: [Bool] -> Int
countTrue = length . filter (==True)

isArrayFree :: [Int] -> Bool
isArrayFree x = not $ elem 1 x

main = do
  t <- read <$> getLine
  replicateM_ t testCase

testCase = do
    (n:_)  <- map read . words <$> getLine
    matrix <- replicateM n (map read . words <$> getLine)
    putStrLn (solve matrix)

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

import Data.List

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

split = _split [] ""

(|>) = flip ($)

boolToInt bool
  | bool = 1
  | otherwise = 0

main = do
  input <- getContents
  let _:vals = read <$> split input :: [Int]
      res = vals |> unfoldr (\case
        []  -> Nothing
        n:m:rest0   ->
          let (cells, rest1) = splitAt (n*m) rest0
              rows = cells |> unfoldr (\case
                []  -> Nothing
                ls  -> Just $ splitAt m ls
                )
              numAvailableRows = sum $ boolToInt <$> isZeroRows where
                isZeroRows = all (== 0) <$> rows
              numAvailableCols = sum $ zeroCols where
                zeroCols = (\x -> 1-x) <$> oneCols
                oneCols = foldl (\acc ls -> uncurry max <$> zip acc ls) allZeros rows where
                  allZeros = replicate n 0
              numTurns = min numAvailableRows numAvailableCols
              subres = if numTurns `mod` 2 == 1 then "Ashish" else "Vivek"
          in Just (subres, rest1)
        )
  sequence_ (putStrLn <$> res)


import           Control.Monad

isInside :: (Int, Int) -> (Int, Int) -> Bool
isInside (n, d) (x, y) = su >= down && su <= up && di >= right && di <= left
  where
    su = x + y
    di = x - y
    down = d
    up = (2 * n - d)
    left = d
    right = (-d)

main :: IO ()
main = do
  [n, d] <- map (read :: String -> Int) . words <$> getLine
  m <- (read :: String -> Int) <$> getLine
  replicateM_ m $ do
    [x, y] <- map (read :: String -> Int) . words <$> getLine
    putStrLn $ if isInside (n, d) (x, y) then "YES" else "NO"

main = getContents >>= putStr . unlines . solve . map read . words

solve (n:d:0:_) = []
solve (n:d:m:x:y:xys) = (contain n d x y) : solve (n:d:m - 1:xys)

contain n d x y = if d <= x + y && x + y <= 2 * n - d && -d <= x - y && x - y <= d
                  then "YES"
                  else "NO"

import Control.Applicative
import Control.Monad

cross (x1,y1) (x2,y2) = x1*y2-x2*y1
sub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
inRect n d x y = all (>=0) t || all (<=0) t
  where
    t = [cross (-1,1) ((x,y) `sub` (d,0)), cross (1,1) ((x,y) `sub` (0,d)),
         cross (1,-1) ((x,y) `sub` (n-d,n)), cross (-1,-1) ((x,y) `sub` (n,n-d))]

main = do
  [n,d] <- map read . words <$> getLine :: IO [Int]
  m <- readLn :: IO Int

  replicateM m $ do
    [x,y] <- map read . words <$> getLine :: IO [Int]
    putStrLn $ if inRect n d x y then "YES" else "NO"


import Control.Monad
import Control.Applicative

import Data.List
import Data.Maybe

filt [] ys = Just ys
filt (x:xs) [] = Nothing
filt (x:xs) (y:ys) =
    if y <= x
        then filt (x:xs) ys >>= return . (y:)
        else filt xs ys

solve (c0, ja0, jd0) = max s1 s2
    where
        [c, ja, jd] = map sort [c0, ja0, jd0]

        s1 = sum $ zipWith (\a b -> max 0 $ a - b) (reverse c) ja

        s2 = fromMaybe 0 $ do
            c' <- filt jd c
            return $ if length c' >= length ja && and (zipWith (<=) (reverse ja) (reverse c'))
                        then sum c' - sum ja
                        else 0

input = do
    [n, m] <- map read . words <$> getLine
    j <- replicateM n $ (\[p, s] -> (p == "ATK", read s)) . words <$> getLine
    let (ja0, jd0) = partition fst j
    c <- replicateM m $ read <$> getLine
    return (c, map snd ja0, map snd jd0)

main = print . solve =<< input

import Data.List
import Data.Maybe

import Control.Monad
import Control.Applicative

filt [] ys = Just ys
filt (x:xs) [] = Nothing
filt (x:xs) (y:ys) =
    if y <= x
        then filt (x:xs) ys >>= return . (y:)
        else filt xs ys

solve (c0, ja0, jd0) =
    max s1 s2
        where
            [c, ja, jd] = map sort [c0, ja0, jd0]

            s1 = sum $ zipWith (\a b -> max 0 $ a - b) (reverse c) ja

            s2 = fromMaybe 0 $ do
                c' <- filt jd c
                return $ if length c' >= length ja && and (zipWith (<=) (reverse ja) (reverse c'))
                            then sum c' - sum ja
                            else 0

readInput = do
    [n, m] <- map read . words <$> getLine
    j <- replicateM n $ words <$> getLine >>= \[p, s] -> return (p == "ATK", read s)
    let (ja0, jd0) = partition fst j
    c <- replicateM m $ read <$> getLine
    return (c, map snd ja0, map snd jd0)

main = print . solve =<< readInput

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

main = do
    [n,m] <- map read . words <$> getLine
    jiro <- replicateM n (parseJiro <$> getLine)
    ciel <- replicateM m (parseCiel <$> getLine)
    print $ solve jiro ciel

parseJiro :: String -> (Bool,Int)
parseJiro ('A':'T':'K':' ':s) = (True,read s)
parseJiro ('D':'E':'F':' ':s) = (False,read s)

parseCiel :: String -> Int
parseCiel = read

solve jiro ciel | canDefeatAll = max a1 a2
                | otherwise    = a1
    where
        attacks = sort [ s | (True,s) <- jiro]
        defenses = sort [ s | (False,s) <- jiro ]
        canDefeatAll = all (uncurry (<=)) $ zip (reverse $ sort (attacks ++ map succ defenses)) (reverse $ sort ciel)
        a1 = sum $ map (\(a,b) -> if a > b then a - b else 0)
                 $ zip (reverse (sort ciel)) attacks
        a2 = sum (remove defenses (sort ciel)) - sum attacks

remove [] ciel = ciel
remove (x:xs) ciel = remove xs (deleteBy (<) x ciel)

import Data.List (foldl', sort)
import qualified Data.IntMap as Map
import Data.Maybe (maybeToList)
import Data.Function (on)
import Control.Monad

-- x is supposed to be descending, att is so too
-- def is supposed to be ascending
rush :: ([Int], [Int]) -> [Int] -> Int
rush (att, []) x = let planA = (sum . takeWhile (>= 0) . zipWith (-) x) (att ++ repeat 0)
                       planB = (sum . takeWhile (>= 0) . zipWith (-) x) (reverse att ++ repeat 0)
                   in  max planA planB
rush (att, def) x = let x' = concat . maybeToList $ x `kill` def
                        x'' = take (length att) x in
                    (max `on` rush (att, [])) x' x''

kill :: [Int] -> [Int] -> Maybe [Int]
kill x def = let xs = foldl' cons Map.empty x
                 cons m y = Map.insertWith (+) y 1 m
                 xs' = foldl' f (return xs) def
                 f ms d = ms >>= \s -> let att = Map.lookupGT d s in fmap (del s . fst) att
                 del = flip $ Map.update (\y -> if y == 1 then Nothing else Just (y - 1))
                 toList  = Map.foldlWithKey' (\l y c -> l  ++ replicate c y) []
             in fmap (reverse . toList) xs'

main :: IO ()
main = do
  m:n:_ <- fmap (map read . words) getLine
  jiro <- fmap (map words) (replicateM m getLine)
  fox <- fmap (map read) (replicateM n getLine)
  let jiros = jiroParse jiro
--  print jiros
  print $ rush (jiroParse jiro) (reverse (sort fox))

jiroParse :: [[String]] -> ([Int], [Int])
jiroParse s = let (att, def) = categorize s ([],[])
                  in (reverse (sort att), sort def)

categorize :: [[String]] -> ([Int], [Int]) -> ([Int], [Int])
categorize [] x = x
categorize (("ATK":x:_):rest) (atk, def) = categorize rest (read x : atk, def)
categorize ((_:x:_):rest) (atk, def) = categorize rest (atk, read x : def)

import Data.List
import Data.Maybe

import Control.Monad
import Control.Applicative

filt [] ys = Just ys
filt (x:xs) [] = Nothing
filt (x:xs) (y:ys) =
    if y <= x
        then filt (x:xs) ys >>= (return . (y:))
        else filt xs ys

solve (c0, ja0, jd0) =
    max s1 s2
        where
            [c, ja, jd] = map sort [c0, ja0, jd0]

            s1 = sum $ zipWith (\a b -> max 0 $ a - b) (reverse c) ja

            s2 = fromMaybe 0 $ do
                c' <- filt jd c
                return $ if length c' >= length ja && and (zipWith (<=) ja $ reverse c')
                            then sum c' - sum ja
                            else 0

readInput = do
    [n, m] <- map read . words <$> getLine
    j <- replicateM n $ words <$> getLine >>= \[p, s] -> return (p == "ATK", read s)
    let (ja, jd) = partition fst j
    c <- replicateM m $ read <$> getLine
    return (c, map snd ja, map snd jd)

main = print . solve =<< readInput

import Data.List (foldl', sort)
import qualified Data.IntMap as Map
import Data.Maybe (maybeToList)
import Data.Function (on)
import Control.Monad

-- x is supposed to be descending, att is so too
-- def is supposed to be ascending
rush :: ([Int], [Int]) -> [Int] -> Int
rush (att, []) x = let-- planA = (sum . takeWhile (> 0) . zipWith (-) x) (att ++ repeat 0)
                       planB = (sum . takeWhile (> 0) . zipWith (-) x) (reverse att ++ repeat 0)
                   in  planB
rush (att, def) x = let x' = concat . maybeToList $ x `kill` def
                        x'' = take (length att) x in
                    (max `on` rush (att, [])) x' x''

kill :: [Int] -> [Int] -> Maybe [Int]
kill x def = let xs = foldl' cons Map.empty x
                 cons m y = Map.insertWith (+) y 1 m
                 xs' = foldl' f (return xs) def
                 f ms d = ms >>= \s -> let att = Map.lookupGT d s in fmap (del s . fst) att
                 del = flip $ Map.update (\y -> if y == 1 then Nothing else Just (y - 1))
                 toList  = Map.foldlWithKey' (\l y c -> l  ++ replicate c y) []
             in fmap (reverse . toList) xs'

main :: IO ()
main = do
  m:n:_ <- fmap (map read . words) getLine
  jiro <- fmap (map words) (replicateM m getLine)
  fox <- fmap (map read) (replicateM n getLine)
  print $ rush (jiroParse jiro) (reverse (sort fox))

jiroParse :: [[String]] -> ([Int], [Int])
jiroParse s = let (att, def) = categorize s ([],[])
                  in (reverse (sort att), sort def)

categorize :: [[String]] -> ([Int], [Int]) -> ([Int], [Int])
categorize [] x = x
categorize (("ATK":x:_):rest) (atk, def) = categorize rest (read x : atk, def)
categorize ((_:x:_):rest) (atk, def) = categorize rest (atk, read x : def)

import Data.List (foldl', sort)
import qualified Data.IntMap as Map
import Data.Maybe (maybeToList)
import Data.Function (on)
import Control.Monad

-- x is supposed to be descending, att is so too
-- def is supposed to be ascending
rush :: ([Int], [Int]) -> [Int] -> Int
rush (att, []) x = let-- planA = (sum . takeWhile (> 0) . zipWith (-) x) (att ++ repeat 0)
                       planB = (sum . takeWhile (>= 0) . zipWith (-) x) (reverse att ++ repeat 0)
                   in  planB
rush (att, def) x = let x' = concat . maybeToList $ x `kill` def
                        x'' = take (length att) x in
                    (max `on` rush (att, [])) x' x''

kill :: [Int] -> [Int] -> Maybe [Int]
kill x def = let xs = foldl' cons Map.empty x
                 cons m y = Map.insertWith (+) y 1 m
                 xs' = foldl' f (return xs) def
                 f ms d = ms >>= \s -> let att = Map.lookupGT d s in fmap (del s . fst) att
                 del = flip $ Map.update (\y -> if y == 1 then Nothing else Just (y - 1))
                 toList  = Map.foldlWithKey' (\l y c -> l  ++ replicate c y) []
             in fmap (reverse . toList) xs'

main :: IO ()
main = do
  m:n:_ <- fmap (map read . words) getLine
  jiro <- fmap (map words) (replicateM m getLine)
  fox <- fmap (map read) (replicateM n getLine)
  print $ rush (jiroParse jiro) (reverse (sort fox))

jiroParse :: [[String]] -> ([Int], [Int])
jiroParse s = let (att, def) = categorize s ([],[])
                  in (reverse (sort att), sort def)

categorize :: [[String]] -> ([Int], [Int]) -> ([Int], [Int])
categorize [] x = x
categorize (("ATK":x:_):rest) (atk, def) = categorize rest (read x : atk, def)
categorize ((_:x:_):rest) (atk, def) = categorize rest (atk, read x : def)


main = interact in2out

in2out input
  = output
  where
    output = show $ maximum l2
    items = [read x::Int | x<-tail$words input]
    l1 = 1:tl1
    l2 = 1:tl2
    (tl1,tl2) = solve l1 l2 items
    solve (x1:t1) (x2:t2) (a:b:rest)
      | a == b
      = ((x1+1):a1, (x2+1):a2)
      | a < b
      = ((x1+1):a1, (x1+1):a2)
      | a > b
      = (1:a1, (x2+1):a2)
      where
        (a1,a2) = solve t1 t2 (b:rest)
    solve _ _ _
      = ([],[])

import Data.List
import Data.Array
main = do
    n <- readIO =<< getLine :: IO Int
    l <- (map read . words) `fmap` getLine :: IO [Int]
    let gl = map (\s'@(s:ss) -> (s, length s')) $ group l
        lgl = length gl
        arr = array (1, lgl) $ zip [1..] gl
        f (l, r) res | l > 1 && fst (arr ! (l - 1)) < fst (arr ! l) = f (l - 1, r) (res
                        + snd (arr ! (l - 1)))
                     | r < lgl && fst (arr ! (r + 1)) < fst (arr ! r) = f (l, r+1) (res
                        + snd (arr ! (r + 1)))
                     | otherwise = res
        r = maximum $ map (\i -> f (i, i) (snd $ arr ! i)) [1..lgl]
    print r

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))

h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) .  tail . inits) $xs)

main :: IO ()
main =
    do {_ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
    ; let ns' = g'' ns in print $ (+1) $ (maximum ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) .  tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
    ; let ns' = g'' ns in print $ (+1) $ (maximum ns')
    }

import Data.List;

gao' :: [Int] -> Int -> Int
gao' xs i = go (reverse $ take (i - 1) xs) n + go (drop i xs) n + 1 where
	n :: Int
	n = last $ take i xs
	go :: [Int] -> Int -> Int
	go [] _ = 0
	go (x:xs) n
		| n >= x = 1 + go xs x
		| otherwise = 0

gao :: [Int] -> String
gao xs = show $ maximum $ map (gao' xs) [1 .. length xs]

main' :: String -> String
main' input = unlines [gao $ map read $ words $ last $ lines input]

main = interact main'

module Main where

import Control.Monad

main = do
    getLine
    sections <- ( map read . words ) `liftM` getLine

    let (_, _, best) = compute sections
    putStrLn $ show best

compute :: [Int] -> (Int, Int, Int)
compute (s1:s2:ss) =
    let (up', down', best') = compute (s2:ss)
        up = 1 + if s1 <= s2 then up' else down'
        down = if s1 >= s2 then down' + 1 else 1
        best = max best' up
    in (up, down, best)
compute _ = (1, 1, 1)

module Main where
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (on)

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

--h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
h xs = head . groupBy (>) $ xs
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

-- 増加しているどうかを調べる
g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

-- 増加している箇所長さを取得
g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) .  tail . inits) $xs)

main :: IO ()
main =
    do {_:ns <- (map ((+0) . read)).(concatMap split)<$>lines<$>getContents
    ; let ns' = g'' ns in print $ (+1) $ (maximum ns')
    }

import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe
import Data.Array
import IO

foldl' :: (a -> b -> a) -> a -> [b] -> a
foldl' _ a [] = a
foldl' f a (x:xs) = a' `seq` foldl' f a' xs
	where a' = f a x

left hs x = d ! x
	where
		d = array (0,n-1) [(i, left' i) | i <- [0..n-1]]
		n = length hs
		left' 0 = 0
		left' i
			| (hs !! (i-1)) <= (hs !! i) = d ! (i-1)
			| otherwise = i

right hs x = d ! x
	where
		d = array (0,n-1) [(i, right' i) | i <- reverse [0..n-1]]
		n = length hs
		right' i
			| i == n-1 = n-1
			| (hs !! (i+1)) <= (hs !! i) = d ! (i+1)
			| otherwise = i

main = do
	hs <- C.hGetContents stdin >>= return . (map (fst . fromJust . C.readInt)) . tail . C.words
	let n = length hs
	putStrLn . show $ foldl' max 0 [right hs i - left hs i + 1 | i <- [0..n-1]]
	-- print [lowest hs i 1 - lowest hs i (-1) + 1 | i <- [0..n-1]]

main :: IO ()
main = do
        n <- getLine
        s <- getLine
        let ss = map (read :: String -> Int) (words s)
        print (solve ss)

solve :: [Int] -> Int
solve s = maximum (zipWith (\x y -> x + y + 1) (f s 0) (reverse (f (reverse s) 0)))

f :: [Int] -> Int -> [Int]
f [] a = []
f [x] a = [a]
f (x : y : xs) a
        | x <= y = a : (f (y : xs) (a + 1))
        | otherwise = a : (f (y : xs) 0)

main :: IO ()
main = do
	n <- getLine
	s <- getLine
	let ss = map (read :: String -> Int) (words s)
	print (solve ss)

solve :: [Int] -> Int
solve s = fst (findmaxind (zipWith (\x y -> x + y + 1) (f s 0) (reverse (f (reverse s) 0))))

f :: [Int] -> Int -> [Int]
f [] a = []
f [x] a = [a]
f (x : y : xs) a
	| x <= y = a : (f (y : xs) (a + 1))
	| otherwise = a : (f (y : xs) 0)

findmaxind :: [Int] -> (Int, Int)
findmaxind [] = (-1, -1)
findmaxind [x] = (x, 0)
findmaxind (x : xs)
	| x > fst maxt = (x, 0)
	| otherwise = (fst maxt, snd maxt + 1)
	where maxt = findmaxind xs

notLess :: [Int] -> [Int]
notLess [] = []
notLess (x:xs) = notLess_ 0 0 (x:xs)
    where notLess_ _ _ [] = []
          notLess_ pred count (x:xs) = if (pred > x) then 1:(notLess_ x 1 xs) else ((count + 1):notLess_ x (count + 1) xs)

solve :: [Int] -> Int
solve a = maximum d - 1
    where
        d = zipWith (+) (notLess a) (reverse (notLess (reverse a)))

main = do
    n <- readLn::(IO Int)
    line <- getLine
    a <- return (map read (words line))
    putStr (show (solve a))

import Data.List
solve = maximum . flip phaseA 0 . group
phaseA (x:y:zs) a | head x < head y = phaseA (y:zs) $! a + length x
                  | otherwise       = phaseB (y:zs) $! a + length x
phaseA [y] a = [a + length y]
phaseB (x:y:zs) a | head x > head y = phaseB (y:zs) $! a + length x
                  | otherwise       = (a+length y) : phaseA (y:zs) (length x)
phaseB [y] a = [a + length y]
main = do
  getLine
  print . solve . map (read :: String -> Int) . words =<< getLine

import Data.Array

left a (-1) = 0
left a i
    | a ! i <= a ! (i + 1) = 1 + left a (i - 1)
    | otherwise = 0

right a i
    | snd (bounds a) < i = 0
    | a ! i <= a ! (i - 1) = 1 + right a (i + 1)
    | otherwise = 0

doJob a (-1) = 0
doJob a i = max (doJob a (i - 1)) (1 + (left a (i - 1)) + (right a (i + 1)))

solve input =
    let (nn:[l]) = lines input in
    let n = (read nn)::Int in
    let a = listArray (0, n - 1) ((map (\x -> (read x)::Int) . words) l) in

    show (doJob a (n - 1))

main = interact(solve)

module Main where
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (on)

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = head . groupBy (>) $ xs
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)
-- 増加しているどうかを調べる
g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

-- 増加している箇所長さを取得
g' xs = (map (toInteger . length)) . (map (head . groupBy (<))) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) .  tail . inits) $xs)

main :: IO ()
main =
    do {_:ns <- (map ((+0) . read)).(concatMap split)<$>lines<$>getContents
    ; let ns' = g'' ns in print $ (+1) $ (maximum ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . tails $ xs)  ((g' . (map reverse) .  inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  ((maximum ns'),ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) . tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  (+1) . fromJust $ (findIndex (==(maximum ns')) ns')
    }

module Main where
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (on)

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = head . groupBy (>) $ xs
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)
-- 増加しているどうかを調べる
g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

-- 増加している箇所長さを取得
g' xs = (map (toInteger . length)) . (map (head . groupBy (>))) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) .  tail . inits) $xs)

main :: IO ()
main =
    do {_:ns <- (map ((+0) . read)).(concatMap split)<$>lines<$>getContents
    ; let ns' = g'' ns in print $ (+1) $ (maximum ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) . tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  ((maximum ns'),ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) . tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  (maximum ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . tails $ xs)  ((g' . (map reverse) . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $ (+1) . fromJust $ (findIndex (==(maximum ns')) ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) . tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  (fromJust $ (findIndex (==(maximum ns')) ns'),ns)
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . tails $ xs)  ((g' . (map reverse) . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  fromJust $ (findIndex (==(maximum ns')) ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . tails $ xs)  ((g' . (map reverse) .  inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $ (+1) $ (maximum ns')
    }

module Main where
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (on)

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = head . groupBy (>) $ xs
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)
-- 増加しているどうかを調べる
g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

-- 増加している箇所長さを取得
g' xs = (map (toInteger . length)) . (map (head . groupBy (>=))) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) .  tail . inits) $xs)

main :: IO ()
main =
    do {_:ns <- (map ((+0) . read)).(concatMap split)<$>lines<$>getContents
    ; let ns' = g'' ns in print $ (+1) $ (maximum ns')
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) . tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  ((+1) . fromJust $ (findIndex (==(maximum ns')) ns'),ns)
    }

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 (==' ')

gr :: [a] -> [[a]]
gr xs = zipWith (++)  (tails xs) (inits xs)

f :: (Num a, Ord a) => [a] -> (Int, a)
f (n:ns) = ((length  xs) , (head xs))
  where
   xs = (takeWhile (<=n)  (n:ns))
f [] = (0,0)

find' x xs = find (==x) xs

h xs = (takeWhile (> -1))  $ (zipWith  (-) xs (tail xs))
--h' :: [Int] -> [Int]
h' xs = zipWith (+) (h . reverse . inits $ xs) (h . tails $ xs)

g :: [Integer] -> [Integer]
g xs = zipWith (-) xs (tail xs)

g' xs = (map (toInteger . length)) . (map (takeWhile (>=0))) .(map g) $ xs
g'' xs = zipWith (+) (g' . init . tails $ xs)  ((g' . (map reverse) . tail . inits) $xs)

--(flip find' ns )
main :: IO ()
main =
    do { -- _ : ns <- (map ((+0) . read)) <$> (concatMap split) <$> lines <$> readFile "input.txt"
         _ : ns <-  (map ((+0) . read)) . (concatMap split) <$> lines <$> getContents
       --; let n' =  snd . maximum . (map f) .  gr $ ns
       --; let n' =  h $ ns
    ; let ns' = g' . tails $ ns in print $  (fromJust $ (findIndex (==(maximum ns')) ns'),ns')
    }

lowest hs 0 _ = 0
lowest hs x d
	| length hs - 1 == x = x
	| (hs !! x+d) <= (hs !! x) = lowest hs (x+d) d
	| otherwise = x

main = do
	n <- getLine >>= return . read :: IO Int
	hs <- getLine >>= return . map read . words :: IO [Int]
	putStrLn . show $ foldl max 0 [lowest hs i (1) - lowest hs i (-1) | i <- [0..n-1]]


{-# LANGUAGE TupleSections #-}

import Control.Monad
import qualified Data.List as List
import Data.Sequence
import qualified Data.Set as Set

solve :: Int -> Integer -> [Int] -> Seq (Integer, Int) -> Set.Set Int -> (Integer, [Int])
solve 0 c ans _  _ = (c, ans)
solve m c ans qs s = solve m' (c + dist) ans' qs' s'
  where ((dist, pos) :< qs'') = viewl qs
        new_elts = List.filter (flip Set.notMember s . snd) [(dist+1, pos-1), (dist+1, pos+1)]
        qs' = foldl (|>) qs'' new_elts
        s' = foldr (Set.insert . snd) s new_elts
        (ans', m') = if dist == 0 then (ans, m) else (pos : ans, m - 1)

main :: IO ()
main = do
  (_:m:_) <- map read . words <$> getLine
  trees   <- map read . words <$> getLine
  let (cost, ans) = solve m 0 [] (fromList $ map (0,) trees) (Set.fromList trees)
  print cost
  mapM_ (putStr . show >=> const (putStr " ")) ans >> putStrLn ""

{-# LANGUAGE TupleSections #-}

import Control.Monad
import qualified Data.List as List
import Data.Sequence
import qualified Data.Set as Set

solve :: Int -> Integer -> [Int] -> Seq (Int, Int) -> Set.Set Int -> (Integer, [Int])
solve 0 c ans _  _ = (c, ans)
solve m c ans qs s = solve m' c' ans' qs' s'
  where ((dist, pos) :< qs'') = viewl qs
        new_elts = List.take m $ List.filter (flip Set.notMember s . snd) [(dist+1, pos-1), (dist+1, pos+1)]
        qs' = foldl (|>) qs'' new_elts
        s' = foldr (Set.insert . snd) s new_elts
        ans' = map snd new_elts ++ ans
        l = List.length new_elts
        m' = m - l
        c' = c + (fromIntegral $ (dist+1) * l)

main :: IO ()
main = do
  (_:m:_) <- map read . words <$> getLine
  trees   <- map read . words <$> getLine
  let (cost, ans) = solve m 0 [] (fromList $ map (0,) trees) (Set.fromList trees)
  print cost
  mapM_ (putStr . show >=> const (putStr " ")) ans >> putStrLn ""

{-# LANGUAGE TupleSections #-}

import Control.Monad
import qualified Data.List as List
import Data.Sequence
import qualified Data.Set as Set

solve :: Int -> Integer -> [Int] -> Seq (Int, Int) -> Set.Set Int -> (Integer, [Int])
solve 0 c ans _  _ = (c, ans)
solve m c ans qs s = solve m' (c + (fromIntegral dist)) ans' qs' s'
  where ((dist, pos) :< qs'') = viewl qs
        new_elts = List.filter (flip Set.notMember s . snd) [(dist+1, pos-1), (dist+1, pos+1)]
        qs' = foldl (|>) qs'' new_elts
        s' = foldr (Set.insert . snd) s new_elts
        (ans', m') = if dist == 0 then (ans, m) else (pos : ans, m - 1)

main :: IO ()
main = do
  (_:m:_) <- map read . words <$> getLine
  trees   <- map read . words <$> getLine
  let (cost, ans) = solve m 0 [] (fromList $ map (0,) trees) (Set.fromList trees)
  print cost
  mapM_ (putStr . show >=> const (putStr " ")) ans >> putStrLn ""

{-# LANGUAGE TupleSections #-}

import Control.Monad
import qualified Data.List as List
import Data.Sequence
import qualified Data.Set as Set

solve :: Int -> Int -> [Int] -> Seq (Int, Int) -> Set.Set Int -> (Int, [Int])
solve 0 c ans _  _ = (c, ans)
solve m c ans qs s = solve m' (c + dist) ans' qs' s'
  where ((dist, pos) :< qs'') = viewl qs
        new_elts = List.filter (flip Set.notMember s . snd) [(dist+1, pos-1), (dist+1, pos+1)]
        qs' = foldl (|>) qs'' new_elts
        s' = foldr (Set.insert . snd) s new_elts
        (ans', m') = if dist == 0 then (ans, m) else (pos : ans, m - 1)

main :: IO ()
main = do
  (_:m:_) <- map read . words <$> getLine
  trees   <- map read . words <$> getLine
  let (cost, ans) = solve m 0 [] (fromList $ map (0,) trees) (Set.fromList trees)
  print cost
  mapM_ (putStr . show >=> const (putStr " ")) ans >> putStrLn ""


{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

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

main :: IO ()
main = do
  [n,m,h] <- map readInt . words <$> getLine
  as <- unfoldr (runStateT rIntS) <$> BS.getLine
  bs <- unfoldr (runStateT rIntS) <$> BS.getLine
  forM_ bs $ \ !b -> do
    hs <- unfoldr (runStateT rIntS) <$> BS.getLine
    putStrLn $ unwords $ map show
      $ zipWith (\h a -> if h==0 then 0 else min a b) hs as

rInt :: StateT BSL.ByteString Maybe Int
rInt = StateT $ BSL.readInt . BSL.dropWhile (<'!')
rIntS :: StateT BS.ByteString Maybe Int
rIntS = StateT $ BS.readInt . BS.dropWhile (<'!')

#define D(f,r,d) f::Integral a=>a->d;f=fromIntegral;r::String->d;r=read
#define C(f,r,g,h,d) D(f,r,d);g,h::RealFrac a=>a->d;g=floor;h=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

{-# INLINE wrA #-}
wrA = A.writeArray
{-# INLINE rdA #-}
rdA = A.readArray
{-# INLINE mdA #-}
mdA arr f i = do
  ai <- rdA arr i
  let fai = f ai
  wrA arr i fai
  return (ai,fai)
{-# INLINE mdA' #-}
mdA' arr f i = do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
{-# INLINE swapA #-}
swapA arr i j = do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai

#define N(f,g,a,m)\
  f :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e); f=A.newArray;\
  g :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e); g=A.newArray_
#define C(a,m)
N(newIOA,newIOA_,IOArray,IO)
N(newSTA,newSTA_,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m),
N(newIOUA,newIOUA_,IOUArray,IO)
N(newSTUA,newSTUA_,STUArray s,ST s)
#undef C
#undef N

import Control.Monad
import Data.List

solve ::[Int]           -> [Int] -> [Int] ->[[Int]] ->[[Int]]
solve [breite,tiefe,hoehe] front    seite    oben =foben oben $   ffront fseite
	where
		fseite :: [[Int]]
		fseite =map (replicate breite) seite
		ffront :: [[Int]] -> [[Int]]
		ffront g = map (zipWith min front) g

foben :: [[Int]]->[[Int]]->[[Int]]
foben oben g2 = zipWith helper oben g2
	where
		helper :: [Int] -> [Int] -> [Int]
		helper oben z2 = zipWith (\o z -> if o==1 then z else 0) oben z2

main :: IO ()
main = do
	[tiefe,breite,hoehe] <- fmap ((map read).words) getLine
	front <- fmap ((map read).words) getLine
	seite <- fmap ((map read).words) getLine
	oben <- replicateM tiefe $ fmap ((map read).words) getLine
	putStrLn $           intercalate "\n" $ map (concat.(intersperse " ")) $ map (map show) $ solve [breite,tiefe,hoehe] front seite oben
	return ()


import Data.List
import Data.Char
import Data.Maybe
import Data.Tuple
import Debug.Trace
import Data.Array
import Data.Array.IO
import Control.Monad
import Control.Applicative
import Control.Arrow
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M
import Text.Printf

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

readInt2 :: B.ByteString -> (Int, Int)
readInt2 = l2t . map readInt . B.words
l2t [x,y] = (x,y)

(|>) x f = f x; infixl 1 |>
car = head
cadr = head . tail

main :: IO ()
main = do
    ls <- B.lines <$> B.getContents
    let (n, k) = car ls |> readInt2
        xs = cadr ls |> B.words |> map (fst . fromJust . B.readInt)
        xs' = map (\(x, i) -> x - i * k) $ zip xs [0 .. ]
        m = majority $ filter (> 0) xs'
        cs = xs' |> zip [1 .. ]
                 |> filter (\e -> (snd e) /= m)
                 |> map (diff m)

    -- print xs'
    -- print m
    print $ length cs
    forM_ cs putStrLn

majority ls =
    head $ snd $ head $ reverse $ sort $ map (\l -> (length l, l)) $ group $ sort ls

diff m (i, x) =
    (if x < m then "+ " else "- ") ++ show i ++ " " ++ show (abs (m - x))

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

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

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

main = do
    (n,k) <- readIntPair
    as <- readInts
    let l = head $ sortBy cmpLen $ do
        i <- [1..1000]
        let result = [i + k * j | j <- [0..]]
        return $ filter ((/=0).snd) $ zip [1..] $ zipWith (-) result as
    print $ length l
    forM_ l $ \(i,x) ->
        putStrLn $ unwords [(if x > 0 then "+" else "-"),show i,show $ abs x]

import Data.List
import Data.Char
import Data.Maybe
import Data.Tuple
import Debug.Trace
import Data.Array
import Data.Array.IO
import Control.Monad
import Control.Applicative
import Control.Arrow
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M
import Text.Printf

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

readInt2 :: B.ByteString -> (Int, Int)
readInt2 = l2t . map readInt . B.words
l2t [x,y] = (x,y)

(|>) x f = f x; infixl 1 |>
car = head
cadr = head . tail

main :: IO ()
main = do
    ls <- B.lines <$> B.getContents
    let (n, k) = car ls |> readInt2
        xs = cadr ls |> B.words |> map (fst . fromJust . B.readInt)
        xs' = map (\(x, i) -> x - i * k) $ zip xs [0 .. ]
        m = majority xs'
        cs = xs' |> zip [1 .. ]
                 |> filter (\e -> (snd e) /= m)
                 |> map (diff m)

    print $ length cs
    forM_ cs putStrLn

majority ls =
    head $ snd $ head $ reverse $ sort $ map (\l -> (length l, l)) $ group $ sort ls

diff m (i, x) =
    (if x < m then "+ " else "- ") ++ show i ++ " " ++ show (abs (m - x))


{-# LANGUAGE Safe #-}

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

main :: IO ()
main =  interact $  lines >>> drop 0 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (_ : as : rest) = (itoline . solve . linetois) as : tcio rest where
    linetoi = read
    itoline = show
    linetois = (map linetoi) . words
    istoline = unwords . (map itoline)
    linestoiss = (map linetois) . lines
    isstolines = unlines . (map istoline)

solve :: [Integer] -> Integer
solve (a:[]) = a
solve as = maximum $ map (uncurry (+)) $ zip (0:ls) rs where
  [ls,rs] = map concat $ map ($ ((zipWith (+)), chunk as)) (map uncurry [scanl1, scanr1])
  chunk (a:[]) = [[a,0]]
  chunk (a:b:xs) = [a,b] : chunk xs

{-# LANGUAGE Safe #-}

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

main :: IO ()
main =  interact $  lines >>> drop 0 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (_ : as : rest) = (itoline . solve . linetois) as : tcio rest where
    linetoi = read
    itoline = show
    linetois = (map linetoi) . words

solve :: [Integer] -> Integer
solve (a:[]) = a
solve as = maximum $ add (0:ls) rs where
  [ls,rs] = map (\f-> concat $ f add $ chunk as) [scanl1, scanr1]
  add xs ys = map (uncurry (+)) $ zip xs ys
  chunk (a:[]) = [[a,0]]
  chunk (a:b:xs) = [a,b] : chunk xs

{-# LANGUAGE Safe #-}

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

main :: IO ()
main =  interact $  lines >>> drop 0 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (_ : as : rest) = (itoline . solve . linetois) as : tcio rest where
    linetoi = read
    itoline = show
    linetois = (map linetoi) . words
    istoline = unwords . (map itoline)
    linestoiss = (map linetois) . lines
    isstolines = unlines . (map istoline)

solve :: [Integer] -> Integer
solve (a:[]) = a
solve as = maximum $ map (uncurry (+)) $ zip (0:ls) rs where
  [ls,rs] = fmap concat [lss, rss]
  lss = scanl1 (zipWith (+)) $ chunk as;
  rss = scanr1 (zipWith (+)) $ chunk as;
  chunk (a:[]) = [[a,0]]
  chunk (a:b:xs) = [a,b] : chunk xs

{-# LANGUAGE Safe #-}

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

main :: IO ()
main =  interact $  lines >>> drop 0 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (_ : as : rest) = (itoline . solve . linetois) as : tcio rest where
    linetoi = read
    itoline = show
    linetois = (map linetoi) . words
    istoline = unwords . (map itoline)
    linestoiss = (map linetois) . lines
    isstolines = unlines . (map istoline)

split :: [Integer] -> ([Integer],[Integer])
split [] = ([],[])
split (x:zs) = ((x:ys), xs) where (xs,ys) = split zs

merge :: [Integer] -> [Integer] -> [Integer]
merge [] ys = ys
merge (x:xs) ys = x:(merge ys xs)

solve :: [Integer] -> Integer
solve (a:[]) = a
solve as = maximum $ map (uncurry (+)) $ zip (0:ls) rs where
  ls = merge lsum1 lsum2
  lsum1 = scanl1 (+) odds
  lsum2 = scanl1 (+) evens
  rs = merge rsum1 rsum2
  rsum1 = scanr1 (+) odds
  rsum2 = scanr1 (+) evens
  (odds,evens) = split as

{-# LANGUAGE Safe #-}

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

main :: IO ()
main =  interact $  lines >>> drop 0 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (_ : as : rest) = (itoline . solve . linetois) as : tcio rest where
    linetoi = read
    itoline = show
    linetois = (map linetoi) . words
    istoline = unwords . (map itoline)
    linestoiss = (map linetois) . lines
    isstolines = unlines . (map istoline)

split :: [Integer] -> ([Integer],[Integer])
split [] = ([],[])
split (x:zs) = ((x:ys), xs) where (xs,ys) = split zs

merge :: [Integer] -> [Integer] -> [Integer]
merge [] ys = ys
merge (x:xs) ys = x:(merge ys xs)

solve :: [Integer] -> Integer
solve (a:[]) = a
solve as = maximum $ map (uncurry (+)) $ zip ls (drop 1 rs) where
  ls = merge lsum1 lsum2
  lsum1 = scanl1 (+) odds
  lsum2 = scanl1 (+) evens
  rs = merge rsum1 rsum2
  rsum1 = scanr1 (+) odds
  rsum2 = scanr1 (+) evens
  (odds,evens) = split as

{-# LANGUAGE Safe #-}

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

main :: IO ()
main =  interact $  lines >>> drop 0 >>> tcio >>> unlines where
  tcio :: [String] -> [String]
  tcio [] = []
  tcio (_ : as : rest) = (itoline . solve . linetois) as : tcio rest where
    linetoi = read
    itoline = show
    linetois = (map linetoi) . words
    istoline = unwords . (map itoline)
    linestoiss = (map linetois) . lines
    isstolines = unlines . (map istoline)

split :: [Int] -> ([Int],[Int])
split [] = ([],[])
split (x:zs) = ((x:ys), xs) where (xs,ys) = split zs

merge :: [Int] -> [Int] -> [Int]
merge [] ys = ys
merge (x:xs) ys = x:(merge ys xs)

solve :: [Int] -> Int
solve (a:[]) = a
solve as = maximum $ map (uncurry (+)) $ zip ls (drop 1 rs) where
  ls = merge lsum1 lsum2
  lsum1 = scanl1 (+) odds
  lsum2 = scanl1 (+) evens
  rs = merge rsum1 rsum2
  rsum1 = scanr1 (+) odds
  rsum2 = scanr1 (+) evens
  (odds,evens) = split as


import Control.Applicative

main = do n <- read <$> getLine
          putStrLn $ unwords $ map show $ solve n

solve :: Integer -> [Integer]
solve n = [(minimum ms),(maximum ms)]
   where ms = map (\ [x,y,z] -> (x+1)*(y+2)*(z+2)-n) $ concatMap (\x -> map (\y -> [(div n x),y,(div x y)]) $ f x) $ f n

f :: Integer -> [Integer]
f n = f' $ filter ((0==).mod n) $ takeWhile (\x -> x*x <= n) [1..]
   where f' ns = ns ++ (map (div n) ns)

{-
import Test.QuickCheck
import qualified Data.Set as S
import qualified Math.NumberTheory.Primes as P
import Data.List

propDivisors :: Positive Int -> Bool
propDivisors (Positive n') = sort divs == sort (divisors n)
  where
    n = (n' `mod` 10^9)+1
    divs = map fromIntegral $ S.elems $ P.divisors (fromIntegral n)

mainTest = quickCheck propDivisors
-}

import Data.Int

triples n = [ (x,y,z) |
                x <- takeWhile (\x -> x*x*x <= n) [1..],
                n `rem` x == 0,
                let q = n `div` x,
                y <- takeWhile (\x -> x*x <= q) [x..],
                q `rem` y == 0,
                let z = q `div` y ]

stolen n x y z = [ (x'+1)*(y'+2)*(z'+2) - n', (z'+1)*(y'+2)*(x'+2) - n' ]
  where n' = fromIntegral n
        x' = fromIntegral x
        y' = fromIntegral y
        z' = fromIntegral z

solve n = (minimum ds, maximum ds)
  where trips = triples n
        ds = concatMap (\(x,y,z) -> stolen n x y z) (triples n) :: [Int64]

mainSolve = do
  n <- fmap read getLine :: IO Int
  let (a,b) = solve n
  putStrLn $ show a ++ " " ++ show b

main = mainSolve

--142A

import Data.Array
import Data.List (group, genericLength)

isDividor :: Integral a => a -> a -> Bool
isDividor m n  =  m `rem` n == 0

primes :: Integral a => [a]
primes  =  2 : filter isPrime [3, 5 ..]

isPrime :: Integral a => a -> Bool
isPrime n  =  all (not . isDividor n) (takeWhile (\x -> x * x <= n) primes)

factorize :: Integral a => a -> [a]
factorize 0  =  []
factorize n  =  factorize' n primes
  where
    factorize' 1 _  =  []
    factorize' n (p:ps)
      | r == 0  =  p : factorize' q (p:ps)
      | q < p  =  [n]  -- i.e. p * p > n
      | otherwise  =  factorize' n ps
      where
        (q, r)  =  quotRem n p

factorizeP :: Integral a => a -> [(a, a)]
factorizeP  =  map (\x -> (head x, genericLength x)) . group . factorize

dividers :: Integral a => a -> [a]
dividers n  =  dividers' empty
  where
    fs  =  factorizeP n
    k   =  length fs
    empty  =  accumArray undefined 0 (1,k) []
    dividers' array  =  d : case (nextArray 1) of
        Nothing  ->  []
        Just array'  ->  dividers' array'
      where
        d  =  product $ zipWith (^) (map fst fs) (elems array)
        nextArray i
          | i > k  =  Nothing
          | array ! i == (snd $ fs !! (i-1))  =  nextArray (i+1)
          | otherwise  =  Just $ array // ((i, array ! i + 1) : [(j, 0) | j <- [1..i-1]])

solve :: Integer -> (Integer, Integer)
solve n  =  (min - n, max - n)
  where
    min  =  minimum all
    max  =  maximum all
    all  =  [v y z | y <- ds, z <- ds, y <= z, y * z <= n, n `mod` (y * z) == 0]
    ds   =  dividers n
    v y z  =  (x + 1) * (y + 2) * (z + 2)
      where
        x  =  n `div` (y * z)

printP :: Show a => (a, a) -> IO ()
printP (a,b)  =  putStrLn $ show a ++ " " ++ show b

main :: IO ()
main  =  readLn >>= printP . solve

import Control.Applicative

main = do n <- read <$> getLine
          putStrLn $ unwords $ map show $ solve n

solve :: Integer -> [Integer]
solve n = map (subtract n) [(minimum ms),(maximum ms)]
   where ms = concatMap ((\ [x,y] -> concatMap (g.(y:)) (f x))) $ f n

f :: Integer -> [[Integer]]
f n = map (\x -> [(div n x),x]) $ (1:) $ filter ((0==).mod n) $ takeWhile (\x-> x*x <= n) primes

g [x,y,z] = [(x+1)*(y+2)*(z+2),(y+1)*(z+2)*(x+2),(z+1)*(x+2)*(y+2)]

primes = 2 : filter (\x -> isPrime x primes) [3,5..]
isPrime n (x:xs) = (n < x*x) || ((mod n x /= 0) && (isPrime n xs))

s n=show(minimum l)++" "++show(maximum l) where l=[(x+1)*(y+2)*(z+2)-n|a<-[1..1000],b<-[1..1000],let c=n`div`a`div`b,c*a*b==n,(x,y,z)<-[(a,b,c),(b,a,c),(c,b,a)]]
main=interact$unwords.map show.s.read


module Main (main) where

import           Data.List
import qualified Data.Map.Lazy as Map

type MapType = Map.Map Integer Integer

readIntegers :: String -> [Integer]
readIntegers = map read . words

solve :: [Integer] -> [Integer]
solve lst = reverse ans where
    insert' :: [Integer] -> MapType -> MapType
    insert' [] mp = mp
    insert' (x:xs) mp = insert' xs (Map.insertWith (+) x 1 mp)
    mmap = insert' lst (Map.fromList [])
    findFirst :: [Integer] -> MapType -> Integer
    findFirst (x:xs) mp = res where
        fit = ((mod x 2 > 0) || (Map.lookup (div x 2) mp) == Nothing) && ((Map.lookup (x * 3) mp) == Nothing)
        res = if fit then x else findFirst xs mp
    first = findFirst lst mmap
    solve' :: MapType -> Int -> Integer -> [Integer] -> [Integer]
    solve' mp 0 x res = res
    solve' mp cnt x res = solve' mp' (cnt-1) next (x:res) where
        t = Map.lookup (x*2) mp
        next = if t /= Nothing && t > (Just 0) then x * 2 else (div x 3)
        mp' = Map.insertWith (-) 1 next mp
    ans = solve' mmap (length lst) first []

main :: IO ()
main = putStrLn . toStr. solve . readIntegers  =<< (getLine >> getLine)
    where toStr = unwords . map show

module Main where

import Data.Int

takeElements :: [a] -> [(a, [a])]
takeElements [] = []
takeElements (x:xs) = (x, xs) : [(y, x:ys) | (y, ys) <- takeElements xs]

solve :: [Int64] -> [[Int64]]
solve = go []
  where
    go :: [Int64] -> [Int64] -> [[Int64]]
    go [] zs = do
      (y, ys) <- takeElements zs
      go [y] ys
    go ls [] = [reverse ls]
    go ls@(l:_) zs = do
      (i, is) <- takeElements zs
      if i == (l * 2) || (l `mod` 3 == 0 && i * 3 == l) then go (i:ls) is
      else []

main :: IO ()
main = do
  _ <- getLine
  xs <- fmap (map read . words) getLine
  let result = head $ solve xs
  putStrLn $ unwords (map show result)

import Data.List
import Data.Function

main = do
  n <-readLn
  as <- fmap (map read . words) getLine
  putStrLn $ unwords $ map show $ solve n as

solve :: Int -> [Integer] -> [Integer]
solve n as =
  map fst $ sortBy(compare`on`f) $ map factorize as
  where
    f (k,(c2,c3)) = (-c3,c2)

factorize :: Integer -> (Integer, (Integer,Integer))
factorize n =
  (n,(c2,c3))
  where
    (c2,n') = go 0 2 n
    (c3,_ ) = go 0 3 n'

    go c p k = let (q,r) = k`divMod`p
               in if r==0 then go (c+1) p q else (c,k)

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe              #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Applicative
import           Control.Monad
import           Data.Array.Unboxed
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Char8 as B
import           Data.Char
import           Data.Function
import           Data.Int
import qualified Data.IntMap.Strict    as IM
import qualified Data.IntSet           as IS
import           Data.List
import qualified Data.Map.Strict       as M
import           Data.Monoid
import           Data.Ord
import qualified Data.Set              as S
import           Data.Tuple
import           Data.Word

readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
readInteger :: B.ByteString -> Integer
readInteger bs=case B.readInteger bs of{Just(n,_)->n;_->error$"readInteger error : bs = "++show bs;}

main :: IO ()
main = do
    n <- readLn
    xs <- unfoldr (B.readInteger.B.dropWhile isSpace) <$> B.getLine
    putStrLn.unwords.map show $ solve n xs

solve :: Int -> [Integer] -> [Integer]
solve n xs = head [hist | x<-xs, Just hist <- pure $ dfs x [x] (S.delete x $ S.fromList xs)]
  where
    dfs _ hist unused | S.null unused = Just $ reverse hist
    dfs cur hist unused = case quotRem cur 3 of
        (q, 0) | !c2 <- 2 * cur, S.member q unused && S.member c2 unused ->
         dfs q (q:hist) (S.delete q unused)
            <|> dfs c2 (c2:hist) (S.delete c2 unused)
               | S.member q unused -> dfs q (q:hist) (S.delete q unused)
        (_, _) | !c2 <- 2 * cur, S.member c2 unused -> dfs c2 (c2:hist) (S.delete c2 unused)
        _ -> Nothing


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

import Control.Applicative
import Control.Monad
import Control.Monad.ST
import qualified Data.ByteString.Char8 as B
import Data.List
import Data.Tuple
import Data.Array.Base
import Data.Array.ST (runSTUArray)
import Data.Either
import Data.Ix
import Debug.Trace

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

main :: IO ()
main = do
  [n,x] <- map read.words <$> getLine
  xs <- map readInt.B.words <$> B.getLine
  putStr.unlines.map show.sort $ solve n x xs

solve :: Int -> Int -> [Int] -> [Int]
solve n x xs = map (xi+) $ filter (unsafeAt dp) [0..n-xi]
   where
     gr = mkGraph n edges
     edges = zip xs [1..]
     (ls,[xi]) = partitionEithers.sort.map (elemSmart x.childs gr) $ roots n edges
     !dp = gen n ls

gen :: Int -> [Int] -> UArray Int Bool
gen n sorted = runSTUArray $ do
  dp <- newArray (0,n) False :: ST s (STUArray s Int Bool)
  unsafeWrite dp 0 True
  forM sorted $ \x-> do
    forM [n-x,n-x-1..0] $ \i-> do
      dpi <- unsafeRead dp i
      when dpi $ do
        unsafeWrite dp (i+x) True
  return dp

elemSmart :: Vertex -> [Vertex] -> Either Int Int
elemSmart x vs
  | Just i <- elemIndex x vs = Right $! i+1
  | otherwise = Left $! length vs

type Graph = UArray Vertex Vertex
type Vertex = Int
type Edge = (Vertex,Vertex)

mkGraph :: Int -> [Edge] -> Graph
mkGraph n es = runSTUArray $ do
  gr <- newArray (0,n) 0  :: ST s (STUArray s Vertex Vertex)
  forM es $ \ (from,to) -> do
    unsafeWrite gr from to
  return gr

mkRevGraph :: Int -> [Edge] -> Graph
mkRevGraph n es = runSTUArray $ do
  gr <- newArray (0,n) 0  :: ST s (STUArray s Vertex Vertex)
  forM es $ \ (to,from) -> do
    unsafeWrite gr from to
  return gr

roots :: Int -> [Edge] -> [Vertex]
roots n es = filter ((0==).unsafeAt revgr) [1..n]
   where
     !revgr = mkRevGraph n es

childs :: Graph -> Vertex -> [Vertex]
childs gr v = takeWhile (0<) $ iterate (unsafeAt gr) v


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

import Control.Applicative
import Control.Monad
import System.IO
import Data.Array.Base
import Data.Array.IO (IOUArray,IOArray)
import qualified Data.ByteString.Char8 as B

readInt bs = case B.readInt bs of Just (n,_) -> n
getIntLn  handle = fmap (map readInt.B.words) $ B.hGetLine     handle
getIntAll handle = fmap (map readInt.B.words) $ B.hGetContents handle

main = do
  hin  <- openFile "input.txt"  ReadMode
  hout <- openFile "output.txt" WriteMode

  (n:xs) <- getIntAll hin

  cnt <- newArray (0,5000) 0  :: IO (IOUArray Int Int)
  ans <- newArray (0,5000) [] :: IO (IOArray Int [Int])

  forM_ (zip[1..]xs) $ \(i,x) -> do
    unsafeRead cnt x >>= unsafeWrite cnt x.(1+)
    unsafeRead ans x >>= unsafeWrite ans x.(i:)
  isValid <- all even <$> mapM (unsafeRead cnt) [1..5000]

  if isValid
     then hPutStr hout.parse.concat =<< mapM (unsafeRead ans) [1..5000]
     else hPrint hout $ -1

  hClose hin
  hClose hout

parse :: [Int] -> String
parse (i:j:rest) = shows i" " ++ shows j "\n" ++ parse rest
parse _ = ""

import Control.Applicative
import Data.List
import Data.Function
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import qualified Data.IntMap.Strict as I

myprint::[String]->[Int]->[String]
myprint  x [] =  x
myprint q (x:y:z) =  myprint ( ( show x ++ " " ++ show y++   "\n"):q) z

main=do
	n<-B.readFile "input.txt"
	let s = tail $ map (fst.fromJust.B.readInt)  $ B.words  n:: [Int]
	let t= foldl (\acc z->I.insertWith (++) (fst z ) [snd z] acc) I.empty $ zip s [1..]::I.IntMap [Int]
	let t1= I.elems t
	writeFile "output.txt"  $if any (odd) (map length t1) then "-1\n" else concat $ reverse $ myprint [""] $ concat t1

import Control.Monad (liftM, replicateM)
import Data.Array
import Data.Array.ST
import Data.Char (ord)
import Data.List (intercalate)
import Prelude hiding (reads)

inputFile  =  "input.txt"
outputFile  =  "output.txt"

reads :: String ->  [Int]
reads  =  map read . words
  where
    read s  =  read' 0 s
    read' a ""  =  a
    read' a (c:s)  =  read' (10*a + ord c - ord '0') s

prints :: Show a => [a] -> String
prints  =  intercalate " " . map show

showAns :: [(Int, Int)] -> String
showAns []  =  "-1"
showAns xs  =  unlines $ map showPair xs
  where
    showPair (x, y)  =  concat [show x, " ", show y]

solve :: [Int]  ->  [(Int, Int)]
solve as
    | existSolve  =  concatMap (getPairs . (array !)) [1..size]
    | otherwise  =  []
  where
    size  =  5000
    array :: Array Int [Int]
    array  =  runSTArray $ do
        array  <-  newArray (1, size) []
        mapM_ (update array) $ zip [1..] as
        return array
      where
        update array (i, a)  =  do
            is  <-  readArray array a
            writeArray array a (i:is)
    existSolve  =  all (even . length . (array !)) [1..size]
    getPairs []  =  []
    getPairs (a:b:as)  =  (a,b) : getPairs as

main :: IO ()
main  =  do
    input  <-  readFile inputFile
    let as  =  tail $ reads input
    let output  =  showAns $ solve as
    writeFile outputFile output

import Control.Monad (liftM)
import Data.Array
import System.IO (IOMode(..), openFile, hPrint, hPutStrLn)

solve :: Int -> [Int] -> Maybe [(Int, Int)]
solve n xs
  | length xs' == n  =  Just xs'
  | otherwise  =  Nothing
  where
    xs'  =  solve' e $ zip [1..] xs
    e    =  listArray (1,5000) [0,0..]
    solve' _ []  =  []
    solve' a ((n, x) : xs)
      | a ! x > 0  =  (a ! x, n) : solve' a' xs
      | otherwise  =  solve' a'' xs
      where
        a'   =  a // [(x, 0)]
        a''  =  a // [(x, n)]

printAns Nothing  =  print (-1)
printAns (Just xs)  =  do
    hOutput  <-  openFile "output.txt" WriteMode
    mapM_ (print' hOutput) xs
  where
    print' hOutput (a, b)  =  hPutStrLn hOutput $ concat [show a, " ", show b]

main :: IO ()
main  =  do
    lines'  <-  liftM lines $ readFile "input.txt"
    let n   =  read $ lines' !! 0
    let xs  =  map read $ words $ lines' !! 1
--    n   <-  readLn
--    xs  <-  liftM (map read . words) getLine
    printAns $ solve n xs

import Control.Monad (liftM)
import Data.Array
import System.IO (IOMode(..), hClose, hPrint, hPutStrLn, openFile)

solve :: Int -> [Int] -> Maybe [(Int, Int)]
solve n xs
  | length xs' == n  =  Just xs'
  | otherwise  =  Nothing
  where
    xs'  =  solve' e $ zip [1..] xs
    e    =  listArray (1,5000) [0,0..]
    solve' _ []  =  []
    solve' a ((n, x) : xs)
      | a ! x > 0  =  (a ! x, n) : solve' a' xs
      | otherwise  =  solve' a'' xs
      where
        a'   =  a // [(x, 0)]
        a''  =  a // [(x, n)]

printAns Nothing  =  print (-1)
printAns (Just xs)  =  do
    hOutput  <-  openFile "output.txt" WriteMode
    mapM_ (print' hOutput) xs
    hClose hOutput
  where
    print' hOutput (a, b)  =  hPutStrLn hOutput $ concat [show a, " ", show b]

main :: IO ()
main  =  do
    lines'  <-  liftM lines $ readFile "input.txt"
    let n   =  read $ lines' !! 0
    let xs  =  map read $ words $ lines' !! 1
--    n   <-  readLn
--    xs  <-  liftM (map read . words) getLine
    printAns $ solve n xs


import Monad (liftM)

parse :: Read a => String -> (a, a)
parse line  =  case words line of
    [s1, s2]  ->  (read s1, read s2)

f i j k
  | k == 1  =  if i `mod` 2 == 1 && j `mod` 2 == 1 then 0 else 1
  | i > j  =  f j i k
  | i `mod` (k+1) == 0  =  2 + (i+j) `mod` 2
  | otherwise  =  (i + j + min i' j') `mod` 2
  where
    (i', j')  =  (i `div` (k+1), j `div` (k+1))

solve :: Int -> (Int, Int) -> String
solve k (n, m)  =  if f n m k == 0 then "-" else "+"

main :: IO ()
main  =  do
    ls  <-  liftM lines $ readFile "input.txt"
    let (t, k)  =  parse . head $ ls
    let xs  =  map parse . take t . tail $ ls
    writeFile "output.txt" $ unlines $ map (solve k) xs

main = do
  x <- readFile "input.txt"
  writeFile "output.txt" (f x)
f input = output where
  output = unlines $ a q
  [t,k]:q = map (map (read::String->Integer)) $ map words $ lines input
  a [] = []
  a ([n,m]:xs) = (answer n m):a xs
  answer n m
    | mod x (k+1) == 0 = "+"
    | mod (div x (k+1)) 2 == 0 || k==1 = if y then "-" else "+"
    | otherwise                        = if y then "+" else "-"
   where
     x = min n m
     y = mod (n+m) 2 == 0

main = do
  x <- readFile "input.txt"
  writeFile "output.txt" (f x)
f input = output where
  output = unlines $ a q
  [t,k]:q = map (map (read::String->Integer)) $ map words $ lines input
  a [] = []
  a ([n,m]:xs) = (answer n m):a xs
  answer n m
    | mod x (k+1) == 0 = "+"
    | mod (div x (k+1)) 2 == 0 || k==0 = if y then "-" else "+"
    | otherwise                        = if y then "+" else "-"
   where
     x = min n m
     y = mod (n+m) 2 == 0

main = do
  x <- readFile "input.txt"
  writeFile "output.txt" (f x)
f input = output where
  output = unlines $ a q
  [t,k]:q = map (map (read::String->Int)) $ map words $ lines input
  a [] = []
  a ([n,m]:xs) = (answer n m):a xs
  answer n m
    | mod x (k+1) == 0 = "+"
    | mod (div x (k+1)) 2 == 0 = if y then "-" else "+"
    | otherwise                = if y then "+" else "-"
   where
     x = min n m
     y = mod (n+m) 2 == 0

main = do
  x <- readFile "input.txt"
  writeFile "output.txt" (f x)
f input = output where
  output = unlines $ a q
  [t,k]:q = map (map (read::String->Integer)) $ map words $ lines input
  a [] = []
  a ([n,m]:xs) = (answer n m):a xs
  answer n m
    | mod x (k+1) == 0 = "+"
    | mod (div x (k+1)) 2 == 0 = if y then "-" else "+"
    | otherwise                = if y then "+" else "-"
   where
     x = min n m
     y = mod (n+m) 2 == 0


import Control.Monad
import qualified Data.Map as M

main :: IO ()
main = do
    t <- read <$> getLine
    replicateM_ t $ do
        _ <- read <$> getLine :: IO Int
        bfs <- map read . words <$> getLine :: IO [Int]
        print $ solve (M.singleton 1 0) bfs (tail bfs) 0

solve :: M.Map Int Int -> [Int] -> [Int] -> Int -> Int
solve h _ [] _ = maximum $ M.elems h
solve h (x:xs) (y:ys) p
                    | y > p = solve (M.insert y (h M.! x + 1) h) (x:xs) ys y
                    | otherwise = solve h xs (y:ys) 0
solve _ _ _ _ = error "Solve called with invalid params"

import Control.Arrow ((>>>))
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  C.interact $
  C.lines >>>
  drop 1 >>>
  map (C.words >>> map readInt) >>>
  process >>> map (show >>> C.pack) >>> C.unlines
  where
    readInt = C.readInt >>> fromMaybe undefined >>> fst

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve = maximum . foldl f [0] . blocks . tail
  where
    f (d:ds) l = ds ++ replicate l (d + 1)

blocks :: [Int] -> [Int]
blocks [_] = [1]
blocks (x:x':xs)
  | x < x' = (1 + head ls) : tail ls
  | otherwise = 1 : ls
  where
    ls = blocks (x' : xs)

import Control.Arrow ((>>>))
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  C.interact $
  C.lines >>>
  drop 1 >>>
  map (C.words >>> map readInt) >>>
  process >>> map (show >>> C.pack) >>> C.unlines
  where
    readInt = C.readInt >>> fromMaybe undefined >>> fst

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve xs = go bs [0]
    -- f l ds = tail ds ++ replicate l (1 + head ds)
  where
    bs = blocks (tail xs)
    go [] ds = maximum ds
    -- go _ ds = maximum ds
    go (l:ls) ds = go ls (tail ds ++ replicate l (1 + head ds))

blocks :: [Int] -> [Int]
blocks [_] = [1]
blocks (x:x':xs)
  | x < x' = (1 + head ls) : tail ls
  | otherwise = 1 : ls
  where
    ls = blocks (x' : xs)

import Control.Arrow ((>>>))
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  C.interact $
  C.lines >>>
  drop 1 >>>
  map (C.words >>> map readInt) >>>
  process >>> map (show >>> C.pack) >>> C.unlines
  where
    readInt = C.readInt >>> fromMaybe undefined >>> fst

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve = maximum . foldl bfs [0] . blocks' . tail
  where
    bfs (d:ds) l = ds ++ replicate l (d + 1)

blocks :: [Int] -> [Int]
blocks [_] = [1]
blocks (x:x':xs)
  | x < x' = (1 + head ls) : tail ls
  | otherwise = 1 : ls
  where
    ls = blocks (x' : xs)

blocks' :: [Int] -> [Int]
blocks' xs = foldr f [1] $ zipWith (<) xs (tail xs)
  where
    f True (l:ls) = 1 + l : ls
    f False ls = 1 : ls

import Control.Arrow ((>>>))
import Data.Bool (bool)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  C.interact $
  C.lines >>>
  drop 1 >>>
  map (C.words >>> map readInt) >>>
  process >>> map (show >>> C.pack) >>> C.unlines
  where
    readInt = C.readInt >>> fromMaybe undefined >>> fst

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve = maximum . foldl bfs [0] . blocks (<) . tail
  where
    bfs (d:ds) l = ds ++ replicate l (d + 1)

blocks :: (a -> a -> Bool) -> [a] -> [Int]
blocks f xs = foldr (bool appl incl) [1] $ zipWith f xs (tail xs)
  where
    appl = (1 :)
    incl (l:ls) = 1 + l : ls

import Control.Arrow ((>>>))
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  C.interact $
  C.lines >>>
  drop 1 >>>
  map (C.words >>> map readInt) >>>
  process >>> map (show >>> C.pack) >>> C.unlines
  where
    readInt = C.readInt >>> fromMaybe undefined >>> fst

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve xs = go (length xs) bs [0]
  where
    bs = blocks (tail xs)
    go _ [] ds = maximum ds
    go 0 _ ds = maximum ds
    go n (l:ls) ds = go (n - 1) ls (tail ds ++ replicate l (1 + head ds))

blocks :: [Int] -> [Int]
blocks [_] = [1]
blocks (x:x':xs)
  | x < x' = (1 + head ls) : tail ls
  | otherwise = 1 : ls
  where
    ls = blocks (x' : xs)

import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $
  lines >>>
  drop 1 >>> map (words >>> map read) >>> process >>> map show >>> unlines

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve xs = go (length xs) bs [0]
  where
    bs = blocks (tail xs)
    go _ [] ds = maximum ds
    go 0 _ ds = maximum ds
    go n (l:ls) ds = go (n - 1) ls (tail ds ++ replicate l (1 + head ds))

blocks :: [Int] -> [Int]
blocks [_] = [1]
blocks (x:x':xs)
  | x < x' = (1 + head ls) : tail ls
  | otherwise = 1 : ls
  where
    ls = blocks (x' : xs)

import Control.Arrow ((>>>))
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  C.interact $
  C.lines >>>
  drop 1 >>>
  map (C.words >>> map readInt) >>>
  process >>> map (show >>> C.pack) >>> C.unlines
  where
    readInt = C.readInt >>> fromMaybe undefined >>> fst

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve xs = maximum $ foldl f [0] bs
  where
    bs = blocks (tail xs)
    f ds l = tail ds ++ replicate l (1 + head ds)
    --go [] ds = maximum ds
    --go (l:ls) ds = go ls (tail ds ++ replicate l (1 + head ds))

blocks :: [Int] -> [Int]
blocks [_] = [1]
blocks (x:x':xs)
  | x < x' = (1 + head ls) : tail ls
  | otherwise = 1 : ls
  where
    ls = blocks (x' : xs)

import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $
  lines >>>
  drop 1 >>> map (words >>> map read) >>> process >>> map show >>> unlines

process :: [[Int]] -> [Int]
process [] = []
process (_:xs:xss) = solve xs : process xss

solve :: [Int] -> Int
solve xs = (+ 1) . length . filter id $ zipWith (>) xs (tail xs)