You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.The poorness of a segment is defined as the absolute value of sum of the elements of segment.

The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence. The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).

Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.

NoteFor the first case, the optimal value of x is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of x is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.5
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.8
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = {-xs' `deepseq`-} do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.8
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = {-xs' `deepseq`-} do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . maxSum . map (subtract mid) $ xs
		{-putStrLn .show . f $ (lo + hi) / 2-}
	{-else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as-}
	else do
		if ((<) `on` maxSum) (map (subtract mid) xs) (map (\x -> mid - x) xs)
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		{-xs' = map (subtract mid) xs-}
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.8
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.8
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . maxSum $ xs'
	{-else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as-}
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.0
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.90
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.8
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= {-m `seq` pref `seq`-} maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.75
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.9
	then do
		putStrLn . show . maxSum $ xs'
		{-putStrLn .show . f $ (lo + hi) / 2-}
	{-else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as-}
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = {-xs' `deepseq`-} do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		{-putStrLn . show . maxSum $ xs'-}
		putStrLn .show . f $ (lo + hi) / 2
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as
	{-else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as-}

{-# OPTIONS_GHC -O2 -optc-O2 #-}
import Prelude hiding (getLine)
import Data.List (unfoldr)
import Control.Applicative ((<$>))
import Data.ByteString (ByteString, getLine)
import qualified Data.ByteString.Char8 as B

parse :: ByteString -> [Int]
parse = unfoldr $ B.readInt . B.dropWhile (== ' ')

solve :: [Double] -> Double
solve xs = cost . (/ 2) . uncurry (+) . (!! 100) $ iterate step range
    where
    range = (minimum xs, maximum xs)
    step (lo, hi)
        | cost i < cost k = (lo, k)
        | otherwise       = (i, hi)
        where
        i = (hi + 2 * lo) / 3
        k = (2 * hi + lo) / 3

    cost x = foldr go (\_ _ acc -> acc) xs 0 0 0
        where
        go i run a b c = run a' b' . max c $ max a' b'
            where
            a' = max 0 $ a + i - x
            b' = max 0 $ b - i + x

main = getLine >> getLine >>= print . solve . map fromIntegral . parse

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.95
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) . map (\x -> r - x) $ xs
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock
import Data.List (foldl')

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` maxSum) xs' $ map negate xs'
		maxSum l = uncurry max . foldl' (\(p, m) v -> (max v (p + v), max m p)) (0, head l) $ l

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock
import Data.List (foldl')

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		{-f r = let xs' = map (subtract r) xs in (max `on` maxSum) xs' $ map negate xs'
		maxSum l = uncurry max . foldl' (\(p, m) v -> (max v (p + v), max m p)) (0, head l) $ l-}
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.5
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.90
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		{-f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)-}
		f r = let xs' = map (subtract r) xs in xs' `seq` (max `on` (maxSum (head xs') 0)) xs' (map negate xs')
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.95
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) . map (\x -> r - x) $ xs
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) ((max 0 pref) + a) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.85
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) . map (\x -> r - x) $ xs
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) ((max 0 pref) + a) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.95
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.90
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		{-f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)-}
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' (map negate xs')
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.90
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		{-f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) (map (\x -> r - x) xs)-}
		f r = let xs' = map (subtract r) xs in xs `seq` (max `on` (maxSum (head xs') 0)) xs' (map negate xs')
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock
import Data.List (foldl')

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = (max `on` (maxSum (head xs - r) 0)) (map (subtract r) xs) . map (\x -> r - x) $ xs
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) ((max 0 pref) + a) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock
import Data.List (foldl')

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` maxSum) xs' $ map negate xs'
		maxSum l = uncurry max . foldl (\(p, m) v -> (max v (p + v), max m p)) (0, head l) $ l
		{-f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	if length xs > 2000
	then return ()
	else solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.85
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.7
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function
import Data.Time.Clock

main = do
	tbegin <- getCurrentTime
	input <- fmap words getContents
	let xs = map read . tail $ input
	solve tbegin xs (minimum xs) (maximum xs)
solve tbegin xs lo hi = do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.85
	then do
		putStrLn . show . f . (* 0.5) $ (hi + lo)
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as

{-main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-6	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as-}

import Data.Function

main = interact $ show . solve . map read . words
solve (_:xs) = solve' xs (minimum xs) (maximum xs)
solve' xs lo hi
	| hi - lo < 1e-5	=	f ((hi + lo) / 2)
	| otherwise			=	let
								mid1 = (2 * lo + hi) / 3
								mid2 = (lo + 2 * hi) / 3
							in	if f mid1 > f mid2
								then	solve' xs mid1 hi
								else	solve' xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in (max `on` (maxSum (head xs') 0)) xs' $ map negate xs'
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= maxSum (max m pref) (max a (pref + a)) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = xs' `deepseq` do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 0.6
	then do
		putStrLn . show . maxSum $ xs'
	else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as

import Data.Function (on)
import Data.Time.Clock
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as ByteString
import Control.DeepSeq

main = do
	tbegin <- getCurrentTime
	input <- ByteString.getContents
	let xs = map (fromIntegral . fst . fromJust . ByteString.readInt) . tail . ByteString.words $ input
	solve tbegin xs (minimum xs) (maximum xs)

solve :: UTCTime -> [Double] -> Double -> Double -> IO ()
solve tbegin xs lo hi = {-xs' `deepseq`-} do
	tcurrent <- getCurrentTime
	if (diffUTCTime tcurrent tbegin) > 1.75
	then do
		{-putStrLn . show . maxSum $ xs'-}
		putStrLn .show . f $ (lo + hi) / 2
	else do
		let mid1 = (2 * lo + hi) / 3
		let mid2 = (lo + 2 * hi) / 3
		if f mid1 > f mid2
		then solve tbegin xs mid1 hi
		else solve tbegin xs lo mid2
	where
		f r = let xs' = map (subtract r) xs in xs' `deepseq` (max `on` (maxSum (head xs') 0)) xs' (map negate xs')
		maxSum m pref []		= max m pref
		maxSum m pref (a:as)	= m `seq` pref `seq` maxSum (max m pref) ((max 0 pref) + a) as
	{-else do
		if ((<) `on` maxSum) xs' (map negate xs')
		then solve tbegin xs lo mid
		else solve tbegin xs mid hi
	where
		mid = (lo + hi) / 2
		xs' = map (subtract mid) xs
		maxSum l = maxSum' (head l) 0 l
		maxSum' m pref []		= max m pref
		maxSum' m pref (a:as)	= m `seq` pref `seq` maxSum' (max m pref) ((max 0 pref) + a) as-}


One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.

In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteYou don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game).

{-# LANGUAGE MultiParamTypeClasses,FlexibleContexts,FlexibleInstances,TypeSynonymInstances,BangPatterns,RankNTypes #-}

import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Control.Arrow
import Debug.Trace

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

readInt = B.readInt >>> fmap fst >>> fromMaybe 0
readsInt = map readInt . B.words <$> B.getLine
readLine :: Read a => IO a
readLine = read <$> getLine
readsLine :: Read a => IO [a]
readsLine = 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)
rect a b = rect2 0 0 a b
rect2 i j a b = ((i,j),(a,b))

itof :: Int -> Double
itof = fromIntegral
casti = fromIntegral

maximize f l = snd $ maximumBy cmpFst [(f x,x) | x <- l]
minimize f l = snd $ minimumBy cmpFst [(f x,x) | x <- l]

infixr 2 <||>
infixr 3 <&&>
p1 <||> p2 = (p1 &&& p2) >>> uncurry (||)
p1 <&&> p2 = (p1 &&& p2) >>> uncurry (&&)

main = do
    n <- fromIntegral . readInt <$> B.getLine
    l <- readsInt
    print $ solve n $ map fromIntegral $ sort l

solve :: Int64 -> [Int64] -> Int64
solve n l = binarySearch predicate (last l)
    where
    s = sum l
    predicate k = n * k - s >= k

binarySearch p a | p a = a
                 | otherwise = go a $ head $ dropWhile (not . p) [a*2^i | i <- [1..]]
    where
    go a b | b - a <= 1 = b
           | p m        = go a m
           | otherwise  = go m b
        where m = (a+b) `div` 2

module Main where
import System.IO
import Data.Maybe
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

solve :: Integer -> [Integer] -> Integer
solve n as = max x y
	where x = ceiling $ (fromIntegral $ sum as) / (fromIntegral $ n-1)
	      y = maximum as

main :: IO ()
main = do
	ls <- B.getLine
	let n = readInteger ls
	ls <- B.getLine
	putStrLn . (++ "\n") . show . solve n . map readInteger . C.words $ ls

readInteger :: C.ByteString -> Integer
readInteger = fst . fromJust . C.readInteger

import Data.Int

main = do
	n <- readLn :: IO Int64
	s <- getLine
	let a = map (read :: String -> Int64) $ words s
	print (max (maximum a) $ div (sum a + n - 2) (n-1))

import Control.Monad
import Data.Int

main = do
  n <- readLn :: IO Int64
  a <- (map (read :: String->Int64) . words) `liftM` getLine
  print $ max (div ((sum a) + (n-2)) (n-1) ) (maximum a)

{-# OPTIONS_GHC -O2 #-}

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

main :: IO ()
main = do
  n <- toInteger . maybe 0 fst . B.readInt <$> B.getLine
  print . solve n . sort . map (toInteger . maybe 0 fst . B.readInt) . B.words =<< B.getContents

solve :: Integer -> [Integer] -> Integer
solve n as = f 0 as
  where
  m = last as
  f c (a : as)
    | a <= c = m
    | a == m = q + min 1 r + c
    | otherwise = f (c + m - a) as
    where
    (q, r) = (n * (m - c)) `divMod` (n - 1)

module Main where

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

main = do
    getLine
    l <- map read <$> words <$> getLine :: IO [Int64]
    print $ solve' l

solve' xs = let m = maximum xs
                l = fromIntegral $ length xs
                s = sum xs
            in max m $ (s -1) `div` (l-1) + 1

--solve [] = 0
--solve [x] = x
solve l = let m1 = minimum l
              l' = delete m1 l
          in f m1 l'
    where f 0 l = maximum l
          f m l = let m' =  minimum l
                      d = m' - m + 1
                  in d + f (m-1) (m : (map (\x -> x - d) $ delete m' l))

module Main where
import Data.List (sortBy)
import qualified Data.ByteString.Char8 as BS
import Data.Maybe

rush :: [Integer] -> Integer
rush l = let s = sortBy (\x y -> op (x `compare` y))  l
             op EQ = EQ
             op GT = LT
             op LT = GT
         in binsearch (test s) (head s) (2 * head s)

test :: [Integer] -> Integer -> Ordering
test l x = test' l (x,0)
  where test' [] (t,_)
          | t > 0 = LT
          | otherwise = EQ
        test' (h:rest) (need,found)
          | need <= 0 = EQ
          | otherwise = let r = need - (h - found) in test' rest (need - r,found + r)

binsearch :: (Integer -> Ordering) -> Integer -> Integer -> Integer
binsearch f l u
  | l >= u = l
  | t == EQ && f (m - 1) == LT = m
  | t == LT = binsearch f (m + 1) u
  | otherwise = binsearch f l (m - 1)
    where m = (l + u) `div` 2
          t = f m

main :: IO ()
main = do
  l <- fmap (map (fst . fromJust . BS.readInteger) . tail . BS.words) BS.getContents
  print $ rush l

getans :: [Integer] -> Integer -> Integer
getans a n =
	let operations = n * (maximum a) - sum a
	    f op n max = op + (n * (div (max - op) (n - 1))) + (if (mod (max - op) (n - 1) > 0) then (mod (max - op) (n - 1) + 1) else 0)
	in (if (operations >= maximum a) then (maximum a) else (f operations n (maximum a)))

main = do
	n <- getLine
	input <- getLine
	putStr $ show $ getans (map read (words input) :: [Integer]) (read n :: Integer)

module Main where
import System.IO
import Data.Maybe
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

solve :: Int -> [Int] -> Int
solve n as = max x y
	where x = ceiling $ (fromIntegral $ sum as) / (fromIntegral $ n-1)
	      y = maximum as

main :: IO ()
main = do
	ls <- B.getLine
	let n = readInt ls
	ls <- B.getLine
	putStrLn . (++ "\n") . show . solve n . map readInt . C.words $ ls

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

import Data.Int

main = do
	n <- readLn :: IO Int64
	s <- getLine
	let a = map (read :: String -> Int64) $ words s
	print (min (maximum a) $ div (sum a + n - 2) (n-1))

import Control.Monad

main = do
  n <- readLn :: IO Int
  a <- (map (read :: String->Int) . words) `liftM` getLine
  print $ max (div ((sum a) + (n-2)) (n-1) ) (maximum a)

{-# OPTIONS_GHC -O2 #-}

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

main :: IO ()
main = do
  n <- maybe 0 fst . B.readInt <$> B.getLine
  print . solve n . sort . map (maybe 0 fst . B.readInt) . B.words =<< B.getContents

solve :: Int -> [Int] -> Int
solve n as = f 0 as
  where
  m = last as
  f c (a : as)
    | a <= c = m
    | a == m = q + min 1 r + c
    | otherwise = f (c + m - a) as
    where
    (q, r) = (n * (m - c)) `divMod` (n - 1)

module Main where

import Control.Applicative
import Data.List
import Data.Int

main = do
    getLine
    l <- map read <$> words <$> getLine :: IO [Int64]
    print $ solve l

solve [] = 0
solve [x] = x
solve l = let m1 = minimum l
              l' = delete m1 l
              m2 = minimum l'
              m3 = maximum $ m1:map (\x -> x - m2) l'
          in m2 + m3

module Main where

import Control.Applicative
import Data.List

main = do
    getLine
    l <- map read <$> words <$> getLine
    let n = length l
        s = sum l
    print $ (s - 1) `div` (n-1) + 1

getans :: [Int] -> Int -> Int
getans a n =
	let operations = n * (maximum a) - sum a
	    f op n max = op + (n * (div (max - op) (n - 1))) + (if (mod (max - op) (n - 1) > 0) then (mod (max - op) (n - 1) + 1) else 0)
	in (if (operations >= maximum a) then (maximum a) else (f operations n (maximum a)))

main = do
	n <- getLine
	input <- getLine
	putStr $ show $ getans (map read (words input) :: [Int]) (read n :: Int)


Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (l ≤ r) are integers. We’ll say that string t complies with rule (p, l, r), if the number of occurrences of string t in string p lies between l and r, inclusive. For example, string "ab", complies with rules ("ab", 1, 2) and ("aab", 0, 1), but does not comply with rules ("cd", 1, 2) and ("abab", 0, 1).A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (|s| is a length of s) is string slsl + 1... sr.Consider a number of occurrences  of string t in string p as a number of pairs of integers l, r (1 ≤ l ≤ r ≤ |p|) such that p[l... r] = t.We’ll say that string t is good if it complies with all n rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string s. Two substrings s[x... y] and s[z... w] are cosidered to be distinct iff s[x... y] ≠ s[z... w].

The first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers pi, li, ri, separated by single spaces (0 ≤ li ≤ ri ≤ |pi|). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters. The input limits for scoring 30 points are (subproblem G1):    0 ≤ n ≤ 10.  The length of string s and the maximum length of string p is  ≤ 200.  The input limits for scoring 70 points are (subproblems G1+G2):    0 ≤ n ≤ 10.  The length of string s and the maximum length of string p is  ≤ 2000.  The input limits for scoring 100 points are (subproblems G1+G2+G3):    0 ≤ n ≤ 10.  The length of string s and the maximum length of string p is  ≤ 50000.

Print a single integer — the number of good substrings of string s.

ltntlnen
3
n 0 0
ttlneenl 1 4
lelllt 1 1

NoteThere are three good substrings in the first sample test: «aab», «ab» and «b».In the second test only substrings «e» and «t» are good.

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

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

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

main :: IO ()
main = do
  cs <- head.B.words <$> B.getLine
  n <- readLn
  plrs <- replicateM n $ do
    [p,l,r] <- B.words <$> B.getLine
    return (p,readInt l,readInt r)
  print $ solve cs plrs

solve :: B.ByteString -> [(B.ByteString,Int,Int)] -> Int
solve cs plrs = length . unique . sort . filter isGood $ subs cs
   where
     isGood str = (not $ B.null str) && all (match str) plrs
     match str (p,l,r) = l<=n && n<=r
      where
        n = length.filter (B.isPrefixOf str) $ B.tails p

subs :: B.ByteString -> [B.ByteString]
subs bs = B.tails bs >>= B.inits

unique :: [B.ByteString] -> [B.ByteString]
unique (x:y:xs)
  | x==y = unique (x:xs)
  | otherwise = x : unique (y:xs)
unique xs = xs


You are given an integer $$$n$$$ ($$$n > 1$$$).Your task is to find a sequence of integers $$$a_1, a_2, \ldots, a_k$$$ such that:  each $$$a_i$$$ is strictly greater than $$$1$$$;  $$$a_1 \cdot a_2 \cdot \ldots \cdot a_k = n$$$ (i. e. the product of this sequence is $$$n$$$);  $$$a_{i + 1}$$$ is divisible by $$$a_i$$$ for each $$$i$$$ from $$$1$$$ to $$$k-1$$$;  $$$k$$$ is the maximum possible (i. e. the length of this sequence is the maximum possible). If there are several such sequences, any of them is acceptable. It can be proven that at least one valid sequence always exists for any integer $$$n > 1$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 10^{10}$$$). It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^{10}$$$ ($$$\sum n \le 10^{10}$$$).

For each test case, print the answer: in the first line, print one positive integer $$$k$$$ — the maximum possible length of $$$a$$$. In the second line, print $$$k$$$ integers $$$a_1, a_2, \ldots, a_k$$$ — the sequence of length $$$k$$$ satisfying the conditions from the problem statement. If there are several answers, you can print any. It can be proven that at least one valid sequence always exists for any integer $$$n > 1$$$.

import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $ words >>> drop 1 >>> map (read >>> solve >>> showAns) >>> unlines
  where
    showAns xs = show (length xs) ++ "\n" ++ unwords (map show xs)

type I64 = Integer

solve :: I64 -> [I64]
solve n
  | null fs =
    case root 1 n n of
      Nothing -> [n]
      Just r -> [r, r]
  | otherwise =
    foldl1 (zipWith (*)) $
    map (\(p, f) -> replicate (k - f) 1 ++ replicate f p) $ (extra, 1) : fs
  where
    fs = factorize n
    extra = foldl (\v (p, m) -> iterate (`div` p) v !! m) n fs
    k = maximum (map snd fs)

primes :: [I64]
primes = filter prime [2 .. 3120]
  where
    prime p = all ((/= 0) . (p `mod`)) [2 .. p - 1]

factorize :: I64 -> [(I64, Int)]
factorize n = filter ((> 0) . snd) . map f $ primes
  where
    f p = (p, mult n p)
    mult u d
      | u `mod` d == 0 = 1 + mult (u `div` d) d
      | otherwise = 0

root :: I64 -> I64 -> I64 -> Maybe I64
root l r n
  | l > r = Nothing
  | mid * mid == n = Just mid
  | mid * mid > n = root l (mid - 1) n
  | mid * mid < n = root (mid + 1) r n
  where
    mid = (l + r) `div` 2

import Data.List

factors :: Integer -> [Integer]
factors 1 = []
factors n = if b == [] then [n] else (head b) : factors (n`div`(head b))
  where a = takeWhile (\x -> x*x <= n) [2..n]
        b = filter (\x -> n`mod`x==0) a

solve :: Integer -> String
solve n = (show lenAns) ++ "\n" ++ (unwords $ map show $ ans)
  where fact = reverse $ sort $ map (\a -> (length a, head a)) $ group $ factors n
        lenAns = fst $ head fact
        f cur sm (x:xs)
          | cur == fst x = f cur (sm*(snd x)) xs
          | otherwise = sm : (f (cur-1) sm (x:xs))
        f cur sm [] = sm : (f (cur-1) sm [])
        ans = take lenAns $ f lenAns 1 fact

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

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GADTs, DeriveFunctor, OverloadedStrings #-}

import qualified Data.ByteString.Char8 as B8
import Text.Printf
import Data.Functor
import Control.Arrow ((>>>))
import Control.Monad (forM_)
import Data.Int (Int64)
import Data.List (group, reverse)

readIntB8 :: B8.ByteString -> Maybe Int
readIntB8 = B8.readInt >>> fmap fst

readIntegerB8 :: B8.ByteString -> Maybe Integer
readIntegerB8 = B8.readInteger >>> fmap fst

primeFactors :: Int64 -> [Int64]
primeFactors n = primeFactors' n [2 .. sqrtN]
    where
        sqrtN = floor . sqrt $ realToFrac n

        primeFactors' :: Int64 -> [Int64] -> [Int64]
        primeFactors' n is@(i : otherIs)
            | n `mod` i == 0 = i : primeFactors' (n `div` i) is
            | otherwise      = primeFactors' n otherIs
        primeFactors' n _
            | n == 1         = []
            | otherwise      = [n]

solve :: Int64 -> [Int64]
solve n = reverse result
    where
        result = foldr1 (zipWith (*)) resizedGroups
        resizedGroups = map (\ps -> take k (ps ++ repeat 1)) primeGroups
        k = maximum . map length $ primeGroups
        primeGroups = group $ primeFactors n

mainCase :: IO ()
mainCase = do
    Just n <- B8.getLine <&> readIntegerB8 <&> fmap fromInteger
    let answer = solve n
    printf "%d\n" $ length answer
    forM_ answer $ printf "%lld "
    printf "\n"

main :: IO ()
main = do
    Just t <- B8.getLine <&> readIntB8
    forM_ [1 .. t] $ const mainCase

{-# LANGUAGE Safe #-}

import safe Control.Arrow ((>>>))
--import safe Data.List (sort)
import Data.List
import Data.Maybe (listToMaybe)
import Data.Function (on)

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

solve :: Integer -> [[Integer]]
solve n  = [[toInteger $ y], fs] where
  fs =  reverse $ (n `div` (x^(y-1))) : (take (y-1) $ repeat x)
  (x,y) = maximumBy (compare `on` snd) $ primeFac n

primeFac :: Integer -> [(Integer,Int)]
primeFac n = map (\xs -> (head xs, length xs)) $ group $ primeFac' n 2 where
  primeFac' n l = unfoldr  primePow (n,l)
  primePow (m,l) = listToMaybe [(p, (m `div` p, p)) | p<- jumpatsqrt l m, m `mod` p == 0]
  jumpatsqrt a b = takeWhile ((<=b).(^2)) [a..b] ++ (if b>1 then [b] else [])

import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $ words >>> drop 1 >>> map (read >>> solve >>> showAns) >>> unlines
  where
    showAns xs = show (length xs) ++ "\n" ++ unwords (map show xs)

type I64 = Integer

solve :: I64 -> [I64]
solve n
  | null fs =
    let r = root 1 n n
     in if r * r == n
          then [r, r]
          else [n]
  | otherwise =
    foldl1 (zipWith (*)) $
    map (\(p, f) -> replicate (k - f) 1 ++ replicate f p) fs
  where
    fs = factorize n
    k = maximum (map snd fs)

primes :: [I64]
primes = filter prime [2 .. 3120]
  where
    prime p = all ((/= 0) . (p `mod`)) [2 .. p - 1]

factorize :: I64 -> [(I64, Int)]
factorize n = filter ((> 0) . snd) . map f $ primes
  where
    f p = (p, mult n p)
    mult u d
      | u `mod` d == 0 = 1 + mult (u `div` d) d
      | otherwise = 0

root :: I64 -> I64 -> I64 -> I64
root l r n
  | l >= r = l
  | mid * mid == n = mid
  | mid * mid > n = root l (mid - 1) n
  | mid * mid < n = root (mid + 1) r n
  where
    mid = (l + r) `div` 2

import Control.Arrow ((>>>))

main :: IO ()
main =
  interact $ words >>> drop 1 >>> map (read >>> solve >>> showAns) >>> unlines
  where
    showAns xs = show (length xs) ++ "\n" ++ unwords (map show xs)

type I64 = Integer

solve :: I64 -> [I64]
solve n
  | null fs =
    let r = round . sqrt . fromInteger $ n
     in if r * r == n
          then [r, r]
          else [n]
  | otherwise =
    foldl1 (zipWith (*)) $
    map (\(p, f) -> replicate (k - f) 1 ++ replicate f p) fs
  where
    fs = factorize n
    k = maximum (map snd fs)

primes :: [I64]
primes = filter prime [2 .. 3120]
  where
    prime p = all ((/= 0) . (p `mod`)) [2 .. p - 1]

factorize :: I64 -> [(I64, Int)]
factorize n = filter ((> 0) . snd) . map f $ primes
  where
    f p = (p, mult n p)
    mult u d
      | u `mod` d == 0 = 1 + mult (u `div` d) d
      | otherwise = 0

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GADTs, DeriveFunctor, OverloadedStrings #-}

import qualified Data.ByteString.Char8 as B8
import Text.Printf
import Data.Functor
import Control.Arrow ((>>>))
import Control.Monad (forM_)
import Data.Int (Int64)
import Data.List (group, reverse)

readIntB8 :: B8.ByteString -> Maybe Int
readIntB8 = B8.readInt >>> fmap fst

primeFactors :: Int64 -> [Int64]
primeFactors n = primeFactors' n [2 .. sqrtN]
    where
        sqrtN = floor . sqrt $ realToFrac n

        primeFactors' :: Int64 -> [Int64] -> [Int64]
        primeFactors' n is@(i : otherIs)
            | n `mod` i == 0 = i : primeFactors' (n `div` i) is
            | otherwise      = primeFactors' n otherIs
        primeFactors' n _
            | n == 1         = []
            | otherwise      = [n]

solve :: Int64 -> [Int64]
solve n = reverse result
    where
        result = foldr1 (zipWith (*)) resizedGroups
        resizedGroups = map (\ps -> take k (ps ++ repeat 1)) primeGroups
        k = maximum . map length $ primeGroups
        primeGroups = group $ primeFactors n

mainCase :: IO ()
mainCase = do
    Just n <- B8.getLine <&> readIntB8 <&> fmap fromIntegral
    let answer = solve n
    printf "%d\n" $ length answer
    forM_ answer $ printf "%lld "
    printf "\n"

main :: IO ()
main = do
    Just t <- B8.getLine <&> readIntB8
    forM_ [1 .. t] $ const mainCase

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GADTs, DeriveFunctor, OverloadedStrings #-}

import qualified Data.ByteString.Char8 as B8
import Text.Printf
import Data.Functor
import Control.Arrow ((>>>))
import Control.Monad (forM_)
import Data.Int (Int64)
import Data.List (group, reverse)

readIntB8 :: B8.ByteString -> Maybe Int
readIntB8 = B8.readInt >>> fmap fst

primeFactors :: Int64 -> [Int64]
primeFactors n = primeFactors' n [2 .. sqrtN]
    where
        sqrtN = floor . sqrt $ realToFrac n

        primeFactors' :: Int64 -> [Int64] -> [Int64]
        primeFactors' n is@(i : otherIs)
            | n `mod` i == 0 = i : primeFactors' (n `div` i) is
            | otherwise      = primeFactors' n otherIs
        primeFactors' n _
            | n == 1         = []
            | otherwise      = [n]

solve :: Int64 -> [Int64]
solve n = reverse result
    where
        result = foldr1 (zipWith (*)) resizedGroups
        resizedGroups = map (\ps -> take k (ps ++ repeat 1)) primeGroups
        k = maximum . map length $ primeGroups
        primeGroups = group $ primeFactors n

mainCase :: IO ()
mainCase = do
    Just n <- B8.getLine <&> readIntB8 <&> fmap fromIntegral
    let answer = solve n
    forM_ answer $ printf "%d "
    printf "\n"

main :: IO ()
main = do
    Just t <- B8.getLine <&> readIntB8
    forM_ [1 .. t] $ const mainCase

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GADTs, DeriveFunctor, OverloadedStrings #-}

import qualified Data.ByteString.Char8 as B8
import Text.Printf
import Data.Functor
import Control.Arrow ((>>>))
import Control.Monad (forM_)
import Data.Int (Int64)
import Data.List (group, reverse)

readIntB8 :: B8.ByteString -> Maybe Int
readIntB8 = B8.readInt >>> fmap fst

primeFactors :: Int64 -> [Int64]
primeFactors n = primeFactors' n [2 .. sqrtN]
    where
        sqrtN = floor . sqrt $ realToFrac n

        primeFactors' :: Int64 -> [Int64] -> [Int64]
        primeFactors' n is@(i : otherIs)
            | n `mod` i == 0 = i : primeFactors' (n `div` i) is
            | otherwise      = primeFactors' n otherIs
        primeFactors' n _
            | n == 1         = []
            | otherwise      = [n]

solve :: Int64 -> [Int64]
solve n = reverse result
    where
        result = foldr1 (zipWith (*)) resizedGroups
        resizedGroups = map (\ps -> take k (ps ++ repeat 1)) primeGroups
        k = maximum . map length $ primeGroups
        primeGroups = group $ primeFactors n

mainCase :: IO ()
mainCase = do
    Just n <- B8.getLine <&> readIntB8 <&> fmap fromIntegral
    let answer = solve n
    printf "%d\n" $ length answer
    forM_ answer $ printf "%d "
    printf "\n"

main :: IO ()
main = do
    Just t <- B8.getLine <&> readIntB8
    forM_ [1 .. t] $ const mainCase

{-# LANGUAGE Safe #-}

import safe Control.Arrow ((>>>))
--import safe Data.List (sort)
import Data.List
import Data.Maybe (listToMaybe)
import Data.Function (on)

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

solve :: Integer -> [[Integer]]
solve n  = [[toInteger $ length fs], fs] where
  fs = (:) (n `div` (x^(y-1))) $ take (y-1) $ repeat x
  (x,y) = maximumBy (compare `on` snd) $ primeFac n

primeFac :: Integer -> [(Integer,Int)]
primeFac n = map (\xs -> (head xs, length xs)) $ group $ primeFac' n 2 where
  primeFac' n l = unfoldr  primePow (n,l)
  primePow (m,l) = listToMaybe [(p, (m `div` p, p)) | p<- jumpatsqrt l m, m `mod` p == 0]
  jumpatsqrt a b = takeWhile ((<=b).(^2)) [a..b] ++ (if b>1 then [b] else [])


You are given an array $$$a$$$ of $$$n$$$ integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_j - a_i = j - i$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case output the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_j - a_i = j - i$$$.

import Control.Monad
import Data.List (group, sort)
import Data.Int (Int64)

main = do
    t <- read <$> getLine
    replicateM_ t $ do
        n <- read <$> getLine
        a <- map read . words <$> getLine :: IO [Int]
        let nC2 x = fromIntegral x * (fromIntegral x - 1) `quot` 2 :: Int64
        print $ sum $ map (nC2 . length) $ group $ sort $ zipWith (-) a [1 .. n]

import Control.Arrow
import Data.List(sort, group)

chunksOf :: Int -> [a] -> [[a]]
chunksOf k [] = []
chunksOf k xs = let (g, xs') = splitAt k xs in g : chunksOf k xs'

type LL = Integer

main = interact $
         lines >>> drop 1 >>> chunksOf 2
         >>> map ((!! 1) >>> words >>> map read >>> solve >>> show)
         >>> unlines

solve :: [Int] -> LL
solve = sum . map (choose2 . length) . group . sort . zipWith (-) [0..]

choose2 :: Int -> LL
choose2 n = let n' = toInteger n in (n' * (n' - 1)) `div` 2

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

main = do
    t <- read <$> getLine
    replicateM_ t $ do
        n <- read <$> getLine
        a <- map read . words <$> getLine :: IO [Int]
        let nC2 x = x * (x - 1) `quot` 2
        print $ sum $ map (nC2 . length) $ group $ sort $ zipWith (-) a [1 .. n]

import Control.Arrow
import Data.List(sort, group)

chunksOf :: Int -> [a] -> [[a]]
chunksOf k [] = []
chunksOf k xs = let (g, xs') = splitAt k xs in g : chunksOf k xs'

main = interact $
         lines >>> drop 1 >>> chunksOf 2
         >>> map ((!! 1) >>> words >>> map read >>> solve >>> show)
         >>> unlines

solve :: [Int] -> Int
solve = sum . map (choose2 . length) . group . sort . zipWith (-) [0..]

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


Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation! Every day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes how many times he had a breakfast, a dinner and a supper (thus, the card contains three integers). Vasiliy could sometimes have missed some meal, for example, he could have had a breakfast and a supper, but a dinner, or, probably, at some days he haven't been at the dining room at all.Vasiliy doesn't remember what was the time of the day when he arrived to sanatorium (before breakfast, before dinner, before supper or after supper), and the time when he left it (before breakfast, before dinner, before supper or after supper). So he considers any of these options. After Vasiliy arrived to the sanatorium, he was there all the time until he left. Please note, that it's possible that Vasiliy left the sanatorium on the same day he arrived.According to the notes in the card, help Vasiliy determine the minimum number of meals in the dining room that he could have missed. We shouldn't count as missed meals on the arrival day before Vasiliy's arrival and meals on the departure day after he left.

The only line contains three integers b, d and s (0 ≤ b, d, s ≤ 1018,  b + d + s ≥ 1) — the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium.

Print single integer — the minimum possible number of meals which Vasiliy could have missed during his vacation.

NoteIn the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day. In the second sample, Vasiliy could have arrived before breakfast, have had it, and immediately have left the sanatorium, not missing any meal.In the third sample, Vasiliy could have been in the sanatorium for one day, not missing any meal.

import Data.Functor
import Data.Int

solve :: Int64 -> Int64 -> Int64 -> Int64
solve 0 0 0 = 0
solve x 0 0 = 2 * (x - 1)
solve 0 x 0 = 2 * (x - 1)
solve 0 0 x = 2 * (x - 1)
solve x y 0
    | x == y = x - 1
    | x >  y = 2 * (x - 1) - y
    | x <  y = 2 * (y - 1) - x
solve x 0 y
    | x == y = x - 1
    | x >  y = 2 * (x - 1) - y
    | x <  y = 2 * (y - 1) - x
solve 0 x y
    | x == y = x - 1
    | x >  y = 2 * (x - 1) - y
    | x <  y = 2 * (y - 1) - x
solve x y z = solve (x - w) (y - w) (z - w)
    where w = min x (min y z)

main :: IO()
main = do
    [b, d, s] <- map read . words <$> getLine
    print $ solve b d s

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

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

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

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

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

  minus :: Integer -> Integer -> Integer
  minus m x = m - x

  -- 11 comes before breakfast, leave before breakfast
  solve11 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve11 b s d =
    let m = maximum [b, s, d]
        in Just $ map (minus m) [b, s, d]

  -- comes before breakfast, leave before supper
  -- 1 0 0
  solve12 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve12 b s d
    | b > s && b > d =
      let m = b
          in Just [(m - b), (m - s - 1), (m - d - 1)]
    | otherwise = Nothing

  -- comes before breakfast, leave before dinner
  -- 1 1 0
  solve13 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve13 b s d
    | max b s > d =
      let m = max b s
          in Just [(m - b), (m - s), (m - d - 1)]
    | otherwise = Nothing

  -- comes before supper, leave before breakfast
  -- 0 1 1
  solve21 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve21 b s d
    | max s d > b =
      let m = max s d
          in Just [(m - b - 1), (m - s), (m - d)]
    | otherwise = Nothing

  -- comes before supper, leave before supper
  -- breakfast == super == dinner
  solve22 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve22 = solve11

  -- comes before supper, leave before dinner
  -- 0 1 0
  solve23 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve23 b s d
    | s > d && s > b =
      let m = s
          in Just [(m - b - 1), (m - s), (m - d - 1)]
    | otherwise = Nothing

  -- comes before dinner, leave before breakfast
  -- 0 0 1
  solve31 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve31 b s d
    | d > s && d > b =
      let m = d
          in Just [(m - b - 1), (m - s - 1), (m - d)]
    | otherwise = Nothing

  -- comes before dinner, leave before supper
  -- 1 0 1
  solve32 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve32 b s d
    | max b d > s =
      let m = maximum [b, s, d]
          in Just [(m - b), (m - s - 1), (m - d)]
    | otherwise = Nothing

  solve33 :: Integer -> Integer -> Integer -> Maybe [Integer]
  solve33 = solve11

  getValue :: Maybe a -> a
  getValue (Just x) = x
  getValue Nothing = undefined

  main :: IO()
  main = do
    [b, s, d] <- getLine >>= return . (map (read :: String -> Integer)) . words
    let r11 = solve11 b s d
        r12 = solve12 b s d
        r13 = solve13 b s d
        r21 = solve21 b s d
        r22 = solve22 b s d
        r23 = solve23 b s d
        r31 = solve31 b s d
        r32 = solve32 b s d
        r33 = solve33 b s d
        r = filter (/=Nothing) [r11, r12, r13, r21, r22, r23, r31, r32, r33]
        rr = map (sum . getValue) r
    print $ minimum $ rr

import Data.List( sort)
main = do
    [b,d,s] <- return.map (read::String->Integer).words =<< getLine
    let max = maximum [b,d,s]
	list = map (\e->e-max) [b,d,s]
	[i,j,k] = sort [b,d,s]
    print $ case sort list of
     [0,0,0]	-> 0
     [n,0,0]	-> -1-n
     [n,m,0]	-> 2*k-2-i-j

import Data.List( sort)
main = do
    [b,d,s] <- return.map (read::String->Integer).words =<< getLine
    let max = maximum [b,d,s]
	list = map (\e->e-max) [b,d,s]
    print $ case sort list of
     [0,0,0]	-> 0
     [n,0,0]	-> -1-n
     [n,m,0]	-> maximum [n,m] - minimum [n,m]


Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.Today he wrote string $$$s$$$ of length $$$n$$$ consisting only of uppercase or lowercase Latin letters. He asks you to check if $$$s$$$ is the correct spelling of his name.

The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ $$$(1 \leq n \leq 10)$$$ — the length of string $$$s$$$. The second line of each test case contains a string $$$s$$$ consisting of only uppercase or lowercase Latin characters.

For each test case, output "YES" (without quotes) if $$$s$$$ satisfies the condition, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

10

5

Timur

5

miurT

5

Trumi

5

mriTu

5

timur

4

Timr

6

Timuur

10

codeforces

10

TimurTimur

5

TIMUR

module Main
       ( main )
       where

import Control.Monad (replicateM)

sort :: (Ord a) => [a] -> [a]
sort []     = []
sort (x:xs) =
  let smallerSorted = sort [a | a <- xs, a <= x]
      biggerSorted = sort [a | a <- xs, a > x]
  in  smallerSorted ++ [x] ++ biggerSorted

timurSorted :: String
timurSorted = sort "Timur"

isTimurPermutation :: String -> Bool
isTimurPermutation s = (sort s) == timurSorted

main :: IO ()
main = do
  tests <- readLn
  replicateM  tests $ do
    -- ignore length of next line
    _ <- getLine
    s <- getLine
    case (isTimurPermutation s) of
      True  -> putStrLn "YES"
      False -> putStrLn "NO"
  return ()

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

import Data.Maybe
import Control.Monad

import Data.List (group, sort)

--solve :: Int -> C.ByteString -> String
solve n s
    | sort s == sort "Timur" = "YES"
    | otherwise = "NO"

read = do
    s1 <- getLine
    s2 <- getLine

    putStrLn $ solve (Prelude.read s1 :: Int) s2

main = do
    t <- getLine

    replicateM_ (Prelude.read t :: Int) Main.read

{-# LANGUAGE CPP, ScopedTypeVariables, TypeApplications #-}
import qualified Data.ByteString.Lazy.Char8 as BS
import qualified Data.ByteString.Char8 as SBS
import Control.Monad.ST
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Data.ByteString.Builder
import qualified Data.ByteString.Builder.Prim as Prim
-- import qualified Data.ByteString.Lazy.Char8 as B
import System.IO
import Control.Monad.Trans.State
import Control.Monad.Trans
import Data.Maybe
import Data.Bits
import Prelude hiding (reverse)
import Data.List
import Debug.Trace
import Data.Char

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

type SBS = State BS.ByteString
type S = StateT BS.ByteString

str = string7

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

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

getInt :: Monad m => S m Int
getInt = state $ fromJust . BS.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
#ifdef LOCAL
  handle <- openFile "input" ReadMode
  inp <- BS.hGetContents handle
#else
  inp <- BS.getContents
#endif
  let outp = evalState tloop inp
  BS.putStr $ toLazyByteString outp

tloop :: SBS Builder
tloop = do
  t <- getInt
  -- let t = 1
  fmap mconcat $ replicateM t $ solve

solve :: SBS Builder
solve = do
  n <- getInt
  s <- getNext
  let
    base = SBS.sort . BS.toStrict $ s
    res = base == ((SBS.pack . sort) "Timur")
  pure . str $ if res then "YES\n" else "NO\n"

{-# LANGUAGE CPP, ScopedTypeVariables, TypeApplications #-}
import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Char8 as C
import Control.Monad.ST
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Data.ByteString.Builder
import qualified Data.ByteString.Builder.Prim as Prim
-- import qualified Data.ByteString.Lazy.Char8 as B
import System.IO
import Control.Monad.Trans.State
import Control.Monad.Trans
import Data.Maybe
import Data.Bits
import Prelude hiding (reverse)
import Data.List
import Debug.Trace
import Data.Char

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

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

str = string7

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
#ifdef LOCAL
  handle <- openFile "input" ReadMode
  inp <- P.hGetContents handle
#else
  inp <- P.getContents
#endif
  let outp = evalState tloop inp
  P.putStr $ toLazyByteString outp

tloop :: SP Builder
tloop = do
  t <- getInt
  -- let t = 1
  fmap mconcat $ replicateM t $ solve

solve :: SP Builder
solve = do
  n <- getInt
  s <- getNext
  let
    base = C.sort . P.toStrict $ s
    res = base == ((C.pack . sort) "Timur")
  pure . str $ if res then "YES\n" else "NO\n"

{-# LANGUAGE CPP, ScopedTypeVariables, TypeApplications #-}
import qualified Data.ByteString.Lazy.Char8 as P
import Control.Monad.ST
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Data.ByteString.Builder
import qualified Data.ByteString.Builder.Prim as Prim
-- import qualified Data.ByteString.Lazy.Char8 as B
import System.IO
import Control.Monad.Trans.State
import Control.Monad.Trans
import Data.Maybe
import Data.Bits
import Prelude hiding (reverse)
import Data.List
import Debug.Trace
import Data.Char

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

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

str = string7

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
#ifdef LOCAL
  handle <- openFile "input" ReadMode
  inp <- P.hGetContents handle
#else
  inp <- P.getContents
#endif
  let outp = evalState tloop inp
  P.putStr $ toLazyByteString outp

tloop :: SP Builder
tloop = do
  t <- getInt
  -- let t = 1
  fmap mconcat $ replicateM t $ solve

solve :: SP Builder
solve = do
  n <- getInt
  s <- getNext
  let
    base = sort $ P.unpack s
    res = base == (sort "Timur")
  pure . str $ if res then "YES\n" else "NO\n"

{-# LANGUAGE LambdaCase #-}

import Data.List (sort)
import Data.IntMap (filterWithKey)

main :: IO ()
main = interact $ format . solve . parse

parse :: String -> [String]
parse input = case lines input of
    [] -> undefined
    (w:ws) -> oddPositions 0 $ take (2 * read w) ws

oddPositions :: Int -> [a] -> [a]
oddPositions _ [] = []
oddPositions n (x:xs)
    | odd n = x : oddPositions (n + 1) xs
    | otherwise = oddPositions (n + 1) xs

format :: [Bool] -> String
format = unlines . map (\case
        True -> "YES"
        False -> "NO"
    )

solve :: [String] -> [Bool]
solve = map ((== "Timru") . sort)

import Data.List (sort)
import Control.Monad (replicateM_)
main = read <$> getLine >>= flip replicateM_ scase
scase = getLine >> getLine >>= putStrLn . (ite "Yes" "No") . isValid
ite x y b = if b then x else y
isValid s@(a1:a2:a3:a4:a5:[]) = sort s == "Timru"
isValid _ = False

{-#LANGUAGE ScopedTypeVariables#-}
import Data.Set (Set, fromList, size)

main = do
    input <- getContents
    let parsed = keepEverySecond (drop 1 (lines input)) []
    let results = reverse (mapTimur parsed [])
    mapM_ putStrLn results

keepEverySecond :: [a] -> [a] -> [a]
keepEverySecond [] res             = reverse res
keepEverySecond (fst:snd:rest) res = keepEverySecond rest ([snd] ++ res)

mapTimur :: [String] -> [String] -> [String]
mapTimur [] res     = res
mapTimur (x:xs) res = mapTimur xs ([(isTimur x (fromList "Timur"))] ++ res)

isTimur :: String -> Set Char -> String
isTimur maybeTimur definitelyTimur
    | (length maybeTimur) /= (size definitelyTimur) = "NO"
    | (fromList maybeTimur) == definitelyTimur      = "YES"
    | otherwise                                     = "NO"

import Debug.Trace

isCharOfName :: Char -> Bool
isCharOfName c = case c of
                     'T' -> True
                     'i' -> True
                     'm' -> True
                     'u' -> True
                     'r' -> True
                     _   -> False

allPass :: (Char -> Bool) -> Bool
allPass f = not (f 'T') && not (f 'i') && not (f 'm')
            && not (f 'u') && not (f 'r')

checkStr :: String -> (Char -> Bool) -> Bool
checkStr [] f = if allPass f
                then True
                else False
checkStr (x:xs) f = if f x
                    then checkStr xs (\c -> if c == x
                                     then False
                                     else f c)
                    else False

decide :: Int -> IO ()
decide 0 = return ()
decide n = do
    getLine
    s <- getLine
    if checkStr s isCharOfName
    then putStrLn "YES"
    else putStrLn "NO"
    decide (n - 1)

main :: IO ()
main = do
    t <- getLine >>= (return . (read :: String -> Int))
    decide t

import Control.Monad (forM_)
import Data.List
--import Debug.Trace (trace)

readInts = getLine >>= pure . map read . words

main = do
    [n'] <- readInts
    forM_ [1..n'] (\_ -> do
        [n] <- readInts
        s <- getLine
        putStrLn $ if n == 5 && "Timur" `elem` (permutations s) then "YES" else "NO"
        )

module Main
       ( main )
       where

import Control.Monad (replicateM)

sort :: (Ord a) => [a] -> [a]
sort []     = []
sort (x:xs) =
  let smallerSorted = sort [a | a <- xs, a <= x]
      biggerSorted = sort [a | a <- xs, a > x]
  in  smallerSorted ++ [x] ++ biggerSorted

timurSorted :: String
timurSorted = sort "Timur"

isTimurPermutation :: String -> Bool
isTimurPermutation s = (sort s) == timurSorted

main :: IO ()
main = do
  tests <- readLn
  replicateM  tests $ do
    s <- getLine
    case (isTimurPermutation s) of
      True  -> putStrLn "YES"
      False -> putStrLn "NO"
  return ()

module Main
       ( main )
       where

import Control.Monad (replicateM)

timur :: String
timur = "Timur"

isTimurPermutation :: String -> Bool
isTimurPermutation s = lengthEqual && allElem
  where lengthEqual = length s == length timur
        allElem     = all (\x -> x `elem` timur) s

main :: IO ()
main = do
  tests <- readLn
  replicateM  tests $ do
    s <- getLine
    case (isTimurPermutation s) of
      True  -> putStrLn "YES"
      False -> putStrLn "NO"
  return ()

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

import Data.Maybe
import Control.Monad

import Data.List (group, sort)

--solve :: Int -> C.ByteString -> String
solve n s
    | C.sort s == C.sort (C.pack "Timur") = "YES"
    | otherwise = "NO"

read = do
    s1 <- C.getLine
    s2 <- C.getLine

    putStrLn $ solve (fst $ fromJust $ C.readInt s1) s2

main = do
    t <- C.getLine

    replicateM_ (fst $ fromJust $ C.readInt t) Main.read

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

import Data.Maybe
import Control.Monad

import Data.List (group, sort)

--solve :: Int -> C.ByteString -> String
solve n s
    | B.sort s == B.sort (C.pack "Timur") = "YES"
    | otherwise = "NO"

read = do
    s1 <- C.getLine
    s2 <- C.getLine

    putStrLn $ solve (fst $ fromJust $ C.readInt s1) s2

main = do
    t <- C.getLine

    replicateM_ (fst $ fromJust $ C.readInt t) Main.read

{-# LANGUAGE LambdaCase #-}

import Data.List (sort)

main :: IO ()
main = interact $ format . solve . parse

parse :: String -> [String]
parse input = case lines input of
    [] -> undefined
    (w:ws) -> take (read w) ws

format :: [Bool] -> String
format = unlines . map (\case
        True -> "YES"
        False -> "NO"
    )

solve :: [String] -> [Bool]
solve = map ((== "imrTu") . sort)


In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance as large as possible.There are n (n is always even) possible starting positions for characters marked along the Ox axis. The positions are given by their distinct coordinates x1, x2, ..., xn, two characters cannot end up at the same position.Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be n - 2). After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which the warrior and the archer will start fighting if both Vova and Lesha play optimally.

The first line on the input contains a single integer n (2 ≤ n ≤ 200 000, n is even) — the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 ≤ xi ≤ 109), giving the coordinates of the corresponding positions.

Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.

NoteIn the first sample one of the optimum behavior of the players looks like that:  Vova bans the position at coordinate 15;  Lesha bans the position at coordinate 3;  Vova bans the position at coordinate 31;  Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, and the distance between them is equal to 7.In the second sample there are only two possible positions, so there will be no bans.

{-import qualified Data.Vector.Unboxed as VU-}
{-import Data.Vector.Unboxed ((!))-}
{-import Data.Vector.Algorithms.Intro as I-}
import Data.List
import Control.Applicative

{-solve :: VU.Vector Int -> Int-}
{-solve points =-}
    {-let sorted = VU.modify I.sort points-}
        {-n = VU.length sorted-}
        {-diff i = sorted ! (i + n `div` 2) - sorted ! i-}
    {-in VU.foldl' (\x i -> min x (diff i)) (10^9) $ VU.generate (n `div` 2) id-}

solve2 :: [Int] -> Int
solve2 points =
    let sorted  = sort points
        n       = length sorted
        st      = take (n `div` 2) sorted
        sn      = drop (n `div` 2) sorted
    in minimum $ zipWith (-) sn st

main :: IO ()
main = do
    nums <- drop 1 . map (read::String->Int) . words <$> getContents
    {-print $ solve (VU.fromList nums)-}
    print $ solve2 nums


A palindrome is a string that reads the same backward as forward. For example, the strings "z", "aaa", "aba", and "abccba" are palindromes, but "codeforces" and "ab" are not. You hate palindromes because they give you déjà vu.There is a string $$$s$$$. You must insert exactly one character 'a' somewhere in $$$s$$$. If it is possible to create a string that is not a palindrome, you should find one example. Otherwise, you should report that it is impossible.For example, suppose $$$s=$$$ "cbabc". By inserting an 'a', you can create "acbabc", "cababc", "cbaabc", "cbabac", or "cbabca". However "cbaabc" is a palindrome, so you must output one of the other options.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The only line of each test case contains a string $$$s$$$ consisting of lowercase English letters. The total length of all strings does not exceed $$$3\cdot 10^5$$$.

For each test case, if there is no solution, output "NO". Otherwise, output "YES" followed by your constructed string of length $$$|s|+1$$$ on the next line. If there are multiple solutions, you may print any. You can print each letter of "YES" and "NO" in any case (upper or lower).

YES
cbabac
YES
aab
YES
zaza
YES
baa
NO
YES
nutforajarofatuna

NoteThe first test case is described in the statement.In the second test case, we can make either "aab" or "aba". But "aba" is a palindrome, so "aab" is the only correct answer.In the third test case, "zaza" and "zzaa" are correct answers, but not "azza".In the fourth test case, "baa" is the only correct answer.In the fifth test case, we can only make "aa", which is a palindrome. So the answer is "NO".In the sixth test case, "anutforajaroftuna" is a palindrome, but inserting 'a' elsewhere is valid.

import qualified Data.List as L
import qualified Data.Map as M
import qualified Data.Set as S

import qualified Data.Array as A

import qualified Control.Monad.State.Strict as State
import Control.Monad.ST

import Data.Bits(xor, (.&.), (.|.))

import Data.Array((!))
import Control.Monad(replicateM)
import Data.Maybe

type Set = S.Set Integer

isP :: String -> Bool
isP str = foldr (&&) True $ fmap (\i->strA ! i == strA ! (n-1-i)) [0..div n 2] where
	strA = A.listArray (0, n-1) str
	n = length str

solve :: String -> Maybe String
solve str
	| not $ isP $ str ++ "a" = Just $ str ++ "a"
	| not $ isP $ 'a':str = Just $ 'a':str
	| otherwise = Nothing

showR :: Maybe String -> IO ()
showR Nothing = putStrLn "NO"
showR (Just s) = do
	putStrLn $ "YES"
	putStrLn $ s

main = do
	t <- getLine >>= return . read :: IO Integer
	replicateM (fromInteger t) $ do
		--[n, k] <- getLine >>= return . (fmap read) . words :: IO [Integer]
		str <- getLine
		showR $ solve str

import Control.Arrow ((>>>))
import Data.List (find)
import Data.Maybe (fromMaybe)

main :: IO ()
main =
  interact $
  words >>> drop 1 >>> map (solve >>> maybe "NO" ("YES\n" ++)) >>> unlines

solve :: String -> Maybe String
solve s = find (not . isPalin) ['a' : s, s ++ "a"]

isPalin :: String -> Bool
isPalin s = s == reverse s