The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are,  The chosen c prisoners has to form a contiguous segment of prisoners.  Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer. Find the number of ways you can choose the c prisoners.

The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.

Print a single integer — the number of ways you can choose the c prisoners.

main = do
    s1<-getLine
    s2<-getLine
    putStr $ res((map read.words)s1)((map read.words)s2)

res [n,t,c] x = solve n t c x 0
solve _ _ _ [] acc = show acc
solve n t c x acc
    |length(takeWhile (<=t) x)>=c   = solve n t c (dropWhile(<=t)x) (acc+1-c+length(takeWhile (<=t) x))
    |dropWhile(<=t)x==[]        = show acc
    |otherwise          = solve n t c (tail(dropWhile(<=t)x)) acc

import Data.List
import Control.Applicative

main :: IO ()
main = do
	inp <- (map read . words) <$> getLine
	xs <- (map read . words) <$> getLine
	print $ solve inp 0 xs

solve :: [Int] -> Int -> [Int] -> Int
solve _  _          []  = 0
solve [n, t, c] cnt (x : xs)
	| newCount >= c  = 1 + solve [n, t, c] newCount xs
	| otherwise =  solve [n, t, c] newCount xs
	where
		newCount = if x <= t then cnt + 1 else 0

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint =  C.getLine >>= \ns -> putStrLn.solve.(\as->[tail $ map (fst.fromJust.C.readInt) $ C.words ns]++[as]) =<< (C.getLine>>= \js -> return $ map (fst.fromJust.C.readInt) $ C.words js)
solve [[x1,x2],xs] = show $ slv2 slv1
  where
  slv1 = foldr f [0] xs
  f a (b:bs) = if a>x1 then (0:b:bs) else (b+1):bs
  slv2 ys = foldr (\ a b -> if a<x2 then b else a-x2+1+b) 0 ys

split as [] l t c = if l >= c then (l-c+1:as) else as
split as (b:bs) l t c =
  if b > t
    then split (if l >= c then (l-c+1:as) else as) bs 0 t c
    else split as bs (l+1) t c

main = do
  let getInts = getLine >>= return . map (\x -> read x :: Int) . words
  [n,t,c] <- getInts
  is <- getInts
  print $ sum $ split [] is 0 t c

f t xs = let (l,ys) = span (<=t) xs in if ys == [] then if l==[] then [] else l:[]  else if l == [] then (f t $ tail ys) else l:(f t $ tail ys)

count c t = (\x -> if x<c then 0 else x-c+1) . length

codeforces t c = sum . (map $ count c t) . f t

main = do
	[_,t,c] <- getLine >>= return . map (read :: String -> Int) . words
	res <- getLine >>= return . codeforces t c . map (read :: String -> Int) . words
	print res

import Data.List (group)

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

solve :: [Int] -> Int
solve (_:t:c:xs) = sum $ filter (>0) $ map (subtract (c - 1) . length) $ filter head $ group $ map (<=t) xs
solve _ = undefined

import Data.List
main=interact$show.f.map read.words
f (_:t:c:xs)=sum$filter(>0)$map((+(1-c)).length)$filter head$group$map(<=t)xs

import Data.List
strToList = (fmap (read :: (String -> Int))).words
f [] _ _ = 0
f xs t c = let (a, b) = break (>t) (dropWhile (>t) xs) in max 0 (length a - c + 1) + f b t c
main = do
    [n, t, c] <- fmap strToList getLine
    s <- fmap strToList getLine
    putStrLn $ show (f s t c)

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, ViewPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs = case B.readInt bs of Just (n,_) -> n
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

main :: IO ()
main = do
  [n,t,c] <- map read.words <$> getLine
  xs <- map readInt.B.words <$> B.getLine
  print $ solve t c xs

solve :: Int -> Int -> [Int] -> Int
solve t c xs = go 0 xs
   where
     go !res [] = res
     go !res xs = go (res + max 0 (len - c + 1)) zs
      where
        (ys, zs) = dropWhile (t<) <$> span (<=t) xs
        !len = length ys

-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
-- hiding clauses are to allow Data.Foldable's generalizations
import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl, foldl1, foldr, foldr1, and, or, any, all, sum, product, concat, concatMap, maximum, minimum, elem, notElem)
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad hiding (mapM, mapM_, forM, forM_, sequence, sequence_, msum)
import Control.Monad.ST

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)
import Data.Bits
import Data.Char
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import Data.List hiding (foldl, foldl', foldl1, foldl1', foldr, foldr1, concat, concatMap, and, or, any, all, sum, product, maximum, minimum, elem, notElem, find)
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Traversable
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
import System.IO
-- }}}
-- silly utilities {{{
-- stolen from lens:
a & f = f a
a <&> f = fmap f a
infixl 1 &, <&>

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)
-- }}}
-- input and output {{{
bsGetLine :: IO ByteString
bsGetLine = fst . BS.spanEnd isSpace <$> BS.getLine

inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]

ssUnwords :: [ShowS] -> ShowS
ssUnwords [] = id
ssUnwords (x:xs) = x . (' ':) . ssUnwords xs

ssUnlines :: [ShowS] -> ShowS
ssUnlines [] = id
ssUnlines (x:xs) = x . ('\n':) . ssUnlines xs

showMany :: (Show a) => [a] -> String
showMany xs = ssUnwords (map shows xs) ""
showMatrix :: (Show a) => [[a]] -> String
showMatrix xs = ssUnlines (map (ssUnwords . map shows) xs) ""

printMany :: (Show a) => [a] -> IO ()
printMany xs = putStrLn (showMany xs)
printMatrix :: (Show a) => [[a]] -> IO ()
printMatrix xs = putStr (showMatrix xs)
-- }}}

main :: IO ()
main = do
	[_,t,c] <- inputInts
	print =<< sum . filter (>0) . map (subtract (c - 1) . length) . filter head . group . map (<= t) <$> inputInts

main = do line <- getLine
          let [n, t, c] = (map read $ words line) :: [Int]
          line <- getLine
          let xs = (map read $ words line) :: [Int]
          putStrLn $ show $ countCrime xs t c

countCrime :: [Int] -> Int -> Int -> Int
countCrime xs t c = count badGuys 0 0
    where badGuys = map (\ x -> x > t) xs
          count [] acc cur = acc + (max (cur - c + 1) 0)
          count (True : xs) acc cur  = count xs newAcc 0
              where newAcc = acc + (max (cur - c + 1) 0)
          count (False : xs) acc cur = count xs acc (cur + 1)

split as [] l t c = if l > 0 then (l-c+1:as) else as
split as (b:bs) l t c =
  if b > t
    then split (if l >= c then (l-c+1:as) else as) bs 0 t c
    else split as bs (l+1) t c

main = do
  let getInts = getLine >>= return . map (\x -> read x :: Int) . words
  [n,t,c] <- getInts
  is <- getInts
  print $ sum $ split [] is 0 t c

f t xs = let (l,ys) = span (<=t) xs in if ys == [] then if l==[] then [] else l:[]  else if l == [] then (f t $ tail ys) else l:(f t $ tail ys)

count c t = (\x -> if x<c then 0 else x-c+1) . length

codeforces t c = sum . (map $ count c t) . f t

main = do
	[_,t,c] <- getLine >>= return . map (read :: String -> Int) . words
	getLine >>= return . codeforces t c . map (read :: String -> Int) . words


Michael and Joe are playing a game. The game is played on a grid with $$$n$$$ rows and $$$m$$$ columns, filled with distinct integers. We denote the square on the $$$i$$$-th ($$$1\le i\le n$$$) row and $$$j$$$-th ($$$1\le j\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$.Michael starts by saying two numbers $$$h$$$ ($$$1\le h \le n$$$) and $$$w$$$ ($$$1\le w \le m$$$). Then Joe picks any $$$h\times w$$$ subrectangle of the board (without Michael seeing).Formally, an $$$h\times w$$$ subrectangle starts at some square $$$(a,b)$$$ where $$$1 \le a \le n-h+1$$$ and $$$1 \le b \le m-w+1$$$. It contains all squares $$$(i,j)$$$ for $$$a \le i \le a+h-1$$$ and $$$b \le j \le b+w-1$$$.    Possible move by Joe if Michael says $$$3\times 2$$$ (with maximum of $$$15$$$). Finally, Michael has to guess the maximum number in the subrectangle. He wins if he gets it right.Because Michael doesn't like big numbers, he wants the area of the chosen subrectangle (that is, $$$h \cdot w$$$), to be as small as possible, while still ensuring that he wins, not depending on Joe's choice. Help Michael out by finding this minimum possible area. It can be shown that Michael can always choose $$$h, w$$$ for which he can ensure that he wins.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 20$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 40$$$)  — the size of the grid. Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer on the $$$i$$$-th line is $$$a_{ij}$$$ ($$$-10^9 \le a_{ij} \le 10^9$$$)  — the element in the cell $$$(i, j)$$$. It is guaranteed that all the numbers are distinct (that is, if $$$a_{i_1j_1} = a_{i_2j_2}$$$, then $$$i_1 = i_2, j_1 = j_2$$$).

For each test case print a single positive integer  — the minimum possible area the subrectangle can have while still ensuring that Michael can guarantee the victory.

NoteIn the first test case, the grid is $$$1\times 1$$$, so the only possible choice for $$$h, w$$$ is $$$h = 1, w = 1$$$, giving an area of $$$h\cdot w = 1$$$.The grid from the second test case is drawn in the statement. It can be shown that with $$$h = 3, w = 3$$$ Michael can guarantee the victory and that any choice with $$$h\cdot w \le 8$$$ doesn't.

{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad

main :: IO ()
main = do
  tc <- readLn
  replicateM_ tc $ do
    [m,n] <- map read . words <$> getLine
    arr :: [[Int]] <- replicateM m $ map read . words <$> getLine
    let amax = maximum $ concat arr
    let (imax,jmax) = head $ filter (\(i,j) -> arr !! i !! j == amax) [(i,j) | i <- [0..m-1], j <- [0..n-1]]
    let h = m - min imax (m-1-imax)
    let w = n - min jmax (n-1-jmax)
    print $ h*w


Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.

The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).  Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road. It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.

Print a single integer — the minimum number of separated cities after the reform.

NoteIn the first sample the following road orientation is allowed: , , .The second sample: , , , , .The third sample: , , , , .

{- ok -}
import Control.Applicative ((<$>))
import Data.Array (Array, (!))
import qualified Data.Array as Array
import Data.Set (Set)
import qualified Data.Set as Set
import Data.ByteString (getLine, getContents)
import Data.ByteString.Char8 (readInt, lines, words, unpack)
import Data.Maybe (fromJust)
import Data.Tuple (swap)

import Prelude hiding (getLine, getContents, lines, words)

readInt' = fst . fromJust . readInt

type Graph = Array Int [Int]

main = do
	[n, m] <- map readInt' . words <$> getLine
	edges <- map ((\[a, b] -> (a, b)) . map readInt' . words) . lines <$> getContents

	let
		search set
     			| Set.null set = []
      			| otherwise = cycle:(search set')
       			where
		  		(cycle, set') = dfs set (Set.findMin set) 0

				dfs set u w = dfs' (u `Set.delete` set) $ filter (/= w) $ g ! u
    					where
		 				dfs' set [] = (False, set)
		 				dfs' set (v:vs)
							| v `Set.notMember` set = (True, set)
						        | otherwise = (cycle1 || cycle2, set'')
		    					where
							   (cycle1, set') = dfs set v u
							   (cycle2, set'') = dfs' set' vs

                                -- above is prettier ?
				-- dfs set u w = foldl dfs' (False, vs') . filter (/= w) $ g ! u
				--	where
	     			--		vs' = u `Set.delete` vs

				--		dfs' (f, vs) v
				--			| v `Set.notMember` vs = (True, vs)
				--		        | otherwise = (f || f', vs')
       				--			where
				--			      (f', vs') = dfs vs v u

	 	comps = search $ Set.fromList [1..n]
		g = Array.accumArray (flip (:)) [] (1, n) $ edges ++ map swap edges

 	print . length . filter not $ comps

import Control.Applicative ((<$>))
import Data.Array (Array, (!))
import qualified Data.Array as Array
import Data.Set (Set)
import qualified Data.Set as Set
import Data.ByteString (getLine, getContents)
import Data.ByteString.Char8 (readInt, lines, words, unpack)
import Data.Maybe (fromJust)
import Data.Tuple (swap)

import Prelude hiding (getLine, getContents, lines, words)

readInt' = fst . fromJust . readInt

type Graph = Array Int [Int]

main = do
	[n, m] <- map readInt' . words <$> getLine
	edges <- map ((\[a, b] -> (a, b)) . map readInt' . words) . lines <$> getContents

	let
		search vs
     			| Set.null vs = []
      			| otherwise = f : (search vs')
       			where
		  		(f, vs') = dfs vs (Set.findMin vs) (-1)
      					where
		  				u = Set.findMin vs

				dfs vs u w
					| u `Set.notMember` vs = (True, vs')
			   	        | otherwise = foldl next (False, vs') $ filter (/= w) $ g ! u
		    			where
		 				vs' = u `Set.delete` vs

       						next (f, vs) v = (f || f', vs')
		     					where
		    						(f', vs') = dfs vs v u

	 	comps = search $ Set.fromList [1..n]
		g = Array.accumArray (flip (:)) [] (1, n) $ edges ++ map swap edges

 	print . length . filter not $ comps

{- ok -}
import Control.Applicative ((<$>))
import Data.Array (Array, (!))
import qualified Data.Array as Array
import Data.Set (Set)
import qualified Data.Set as Set
import Data.ByteString (getLine, getContents)
import Data.ByteString.Char8 (readInt, lines, words, unpack)
import Data.Maybe (fromJust)
import Data.Tuple (swap)

import Prelude hiding (getLine, getContents, lines, words)

readInt' = fst . fromJust . readInt

type Graph = Array Int [Int]

main = do
	[n, m] <- map readInt' . words <$> getLine
	edges <- map ((\[a, b] -> (a, b)) . map readInt' . words) . lines <$> getContents

	let
		search vs
     			| Set.null vs = []
      			| otherwise = f : (search vs')
       			where
		  		(f, vs') = dfs vs (Set.findMin vs) 0

				dfs vs u w = foldl dfs' (False, vs') . filter (/= w) $ g ! u
					where
	     					vs' = u `Set.delete` vs

						dfs' (f, vs) v
							| v `Set.notMember` vs = (True, vs)
						        | otherwise = (f || f', vs')
       							where
							      (f', vs') = dfs vs v u

	 	comps = search $ Set.fromList [1..n]
		g = Array.accumArray (flip (:)) [] (1, n) $ edges ++ map swap edges

 	print . length . filter not $ comps

{- ok -}
import Control.Applicative ((<$>))
import Data.Array (Array, (!))
import qualified Data.Array as Array
import Data.Set (Set)
import qualified Data.Set as Set
import Data.ByteString (getLine, getContents)
import Data.ByteString.Char8 (readInt, lines, words, unpack)
import Data.Maybe (fromJust)
import Data.Tuple (swap)

import Prelude hiding (getLine, getContents, lines, words)

readInt' = fst . fromJust . readInt

type Graph = Array Int [Int]

main = do
	[n, m] <- map readInt' . words <$> getLine
	edges <- map ((\[a, b] -> (a, b)) . map readInt' . words) . lines <$> getContents

	let
		search set
     			| Set.null set = []
      			| otherwise = cycle:(search set')
       			where
		  		(cycle, set') = dfs set (Set.findMin set) 0

				dfs set u w = dfs' (u `Set.delete` set) $ filter (/= w) $ g ! u
    					where
		 				dfs' set [] = (False, set)
		 				dfs' set (v:vs)
							| v `Set.notMember` set = (True, set)
						        | otherwise = (cycle1 || cycle2, set'')
		    					where
							   (cycle1, set') = dfs set v u
							   (cycle2, set'') = dfs' set' vs

				-- dfs set u w = foldl dfs' (False, vs') . filter (/= w) $ g ! u
				--	where
	     			--		vs' = u `Set.delete` vs

				--		dfs' (f, vs) v
				--			| v `Set.notMember` vs = (True, vs)
				--		        | otherwise = (f || f', vs')
       				--			where
				--			      (f', vs') = dfs vs v u

	 	comps = search $ Set.fromList [1..n]
		g = Array.accumArray (flip (:)) [] (1, n) $ edges ++ map swap edges

 	print . length . filter not $ comps

{- ok -}
import Control.Applicative ((<$>))
import Data.Array (Array, (!))
import qualified Data.Array as Array
import Data.Set (Set)
import qualified Data.Set as Set
import Data.ByteString (getLine, getContents)
import Data.ByteString.Char8 (readInt, lines, words, unpack)
import Data.Maybe (fromJust)
import Data.Tuple (swap)

import Prelude hiding (getLine, getContents, lines, words)

readInt' = fst . fromJust . readInt

type Graph = Array Int [Int]

main = do
	[n, m] <- map readInt' . words <$> getLine
	edges <- map ((\[a, b] -> (a, b)) . map readInt' . words) . lines <$> getContents

	let
		search vs
     			| Set.null vs = []
      			| otherwise = f : (search vs')
       			where
		  		(f, vs') = dfs vs (Set.findMin vs) 0

				dfs vs u w = foldl dfs' (False, vs') . filter (/= w) $ g ! u
					where
	     					vs' = u `Set.delete` vs

						dfs' (f, vs) v
							| u `Set.notMember` vs = (True, vs)
						        | otherwise = (f || f', vs')
       							where
							      (f', vs') = dfs vs v u

	 	comps = search $ Set.fromList [1..n]
		g = Array.accumArray (flip (:)) [] (1, n) $ edges ++ map swap edges

 	print . length . filter not $ comps


$$$n$$$ towns are arranged in a circle sequentially. The towns are numbered from $$$1$$$ to $$$n$$$ in clockwise order. In the $$$i$$$-th town, there lives a singer with a repertoire of $$$a_i$$$ minutes for each $$$i \in [1, n]$$$.Each singer visited all $$$n$$$ towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each town. In addition, in each town, the $$$i$$$-th singer got inspired and came up with a song that lasts $$$a_i$$$ minutes. The song was added to his repertoire so that he could perform it in the rest of the cities.Hence, for the $$$i$$$-th singer, the concert in the $$$i$$$-th town will last $$$a_i$$$ minutes, in the $$$(i + 1)$$$-th town the concert will last $$$2 \cdot a_i$$$ minutes, ..., in the $$$((i + k) \bmod n + 1)$$$-th town the duration of the concert will be $$$(k + 2) \cdot a_i$$$, ..., in the town $$$((i + n - 2) \bmod n + 1)$$$ — $$$n \cdot a_i$$$ minutes.You are given an array of $$$b$$$ integer numbers, where $$$b_i$$$ is the total duration of concerts in the $$$i$$$-th town. Reconstruct any correct sequence of positive integers $$$a$$$ or say that it is impossible.

The first line contains one integer $$$t$$$ $$$(1 \le t \le 10^3$$$) — the number of test cases. Then the test cases follow. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 4 \cdot 10^4$$$) — the number of cities. The second line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^{9}$$$) — the total duration of concerts in $$$i$$$-th city. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print the answer as follows: If there is no suitable sequence $$$a$$$, print NO. Otherwise, on the first line print YES, on the next line print the sequence $$$a_1, a_2, \dots, a_n$$$ of $$$n$$$ integers, where $$$a_i$$$ ($$$1 \le a_i \le 10^{9}$$$) is the initial duration of repertoire of the $$$i$$$-th singer. If there are multiple answers, print any of them.

NoteLet's consider the $$$1$$$-st test case of the example:  the $$$1$$$-st singer in the $$$1$$$-st city will give a concert for $$$3$$$ minutes, in the $$$2$$$-nd — for $$$6$$$ minutes, in the $$$3$$$-rd — for $$$9$$$ minutes;  the $$$2$$$-nd singer in the $$$1$$$-st city will give a concert for $$$3$$$ minutes, in the $$$2$$$-nd — for $$$1$$$ minute, in the $$$3$$$-rd - for $$$2$$$ minutes;  the $$$3$$$-rd singer in the $$$1$$$-st city will give a concert for $$$6$$$ minutes, in the $$$2$$$-nd — for $$$9$$$ minutes, in the $$$3$$$-rd — for $$$3$$$ minutes.

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

solve :: IO ()
solve = do
    ~[n] <- readInts
    bs <- readInts
    let cs = (head bs - last bs) : zipWith (-) (tail bs) bs
        ds = map (`div` n) $ zipWith (-) cs (tail cs)
        es = scanl1 (+) ds
        s1 = sum $ zipWith (*) [n, n-1 ..] es
        p = (n * (n + 1)) `div` 2
        (a0, rm) = (head bs - s1) `divMod` p
        as = a0 : map (+a0) es
    putStrLn $ if rm == 0 && all (>0) as
        then "YES\n" ++ unwords (map show as)
        else "NO"

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

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


Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1n + 2n + 3n + 4n) mod 5for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).

The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.

Print the value of the expression without leading zeros.

NoteOperation x mod y means taking remainder after division x by y.Note to the first sample:

compute :: Int -> Int
compute n = (1 +  2 ^ n + 3 ^ n + 4 ^ n) `mod` 5

lastN :: Int -> [a] -> [a]
lastN n xs = foldl (const . drop 1) xs (drop n xs)

main :: IO ()
main = do
  line <- getLine
  let n = read $ lastN 3 line
  print $ compute (n `mod` 4)

import Data.List
import Data.Function
import Data.Char

main = interact $ show . getAns . read

getAns :: Integer -> Int
getAns x = let m2 = fromInteger $ x `mod` 2; m4 = fromInteger $ x `mod` 4
			in (1 + ([1, 2, 4, 3] !! m4) + ([1, 3, 4, 2] !! m4) + ([1, 4] !! m2)) `mod` 5

{-
1 -> [1]
2 -> [2, 4, 3, 1]
3 -> [3, 4, 2, 1]
4 -> [4, 1]-}

import Data.List
import Data.Function
import Data.Char

main = interact $ show . getAns . read

getAns :: Integer -> Int
getAns x = let m = fromInteger $ x `mod` 4
			in (1 + 2 ^ m + 3 ^ m + 4 ^ m) `mod` 5

module Main where

import Data.Bits

main = do
    n <- getLine
    let l = length n
    print  $  (\x -> if x `mod`4 == 0 then 4 else 0) $ (read :: String -> Int) $ drop (if l > 1 then l-2 else 0) n

solve :: String -> Int
solve s
  | read ends `mod` 4 == 0  =  4
  | otherwise  =  0
  where
    ends  =  drop (length s - 2) s

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

main = do
    input <- getLine
    let len = length input
    let short = (read :: String -> Int) $ drop (len - 2) input
    putStrLn $ show $ f short

f :: Int -> Int
f x =
    let x1 = cycle [1,1,1,1]
        x2 = cycle [6,2,4,8]
        x3 = cycle [1,3,9,7]
        x4 = cycle [6,4,6,4]
        m = mod4 x
    in mod5 (x1 !! m + x2 !! m + x3 !! m + x4 !! m)

mod4 x = mod x 4
mod5 x = mod x 5

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

solve :: String -> Int
solve [] = 0
solve (_:s@(_:_:_)) = solve s
solve s = if (read s :: Int) `mod` 4 == 0 then 4 else 0

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

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

solve :: B.ByteString -> Int
solve s = if fst (fromJust $ B.readInt (B.drop (B.length s - 3) s)) `mod` 4 == 0 then 4 else 0

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

solve :: Integer -> Integer
solve n | n `mod` 4 == 0 = 4 | otherwise = 0

main = putStrLn . solve =<< getLine
solve s | n `mod` 4 == 0 = "4" | otherwise = "0" where
  n = read $ reverse $ take 2 $ reverse s

module Main where

main = do
	line <- getLine
	let n = read line `mod` 4
	let ans = (1^n + 2^n + 3^n + 4^n) `mod` 5
	putStrLn $ show ans

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

main= do
	s<-  C.tail. C.take 3 . C.reverse .C.filter (/=' ') <$> C.getLine
	let s1 = read (reverse (C.unpack s)) ::Int
	print $ if mod s1 4 ==0 then 4 else 0

main = do
    n <- fmap (read . reverse . take 2 . reverse) getLine :: IO Int
    print $ f n where
        f n
            | mod n 2 == 1 = 0
            | otherwise = let m = mod n 4 in mod (1 + 2^m + 3^m + 4^m) 5

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

-- Meat
solve :: BS.ByteString -> Int
solve s = if divisibleBy4 s then 4 else 0

divisibleBy4 :: BS.ByteString -> Bool
divisibleBy4 s =  toInt str `mod` 4 == 0
    where len = BS.length s
          str = if len < 2 then s else BS.drop (len - 2) s
          toInt = fst .fromJust . BS.readInt

-- Shit
main :: IO ()
main = do
        [[s]]  <- readShit
        printShit [solve s]

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

yN :: Bool -> String
yN True = "YES"
yN False = "NO"

iif :: x -> x -> Bool-> x
iif a b c = if c then a else b

instance  Shit Int where
    printShit = print
    readShit = fmap read getLine

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

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

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

instance Shit [[Double]] where
    printShit = printShit . (fmap.fmap) show
    readShit = fmap (fmap.fmap $ read) readShit

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

instance Shit String where
    printShit = putStrLn
    printShitV = putStrLn
    readShit = getLine

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

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

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

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

solve n = if n `mod` 4 == 0 then 4 else 0
main = getLine >>= print . solve . read

import Data.Char

compute :: Int -> Int
compute n = (1 +  2 ^ n + 3 ^ n + 4 ^ n) `mod` 5

main :: IO ()
main = do
  line <- getLine
  let n = digitToInt $ last line
  print $ compute (n `mod` 4)

main = do
    input <- getLine
    let len = length input
    let short = (read :: String -> Int) $ drop (len - 2) input
    putStrLn $ show $ f short

f :: Int -> Int
f 0 = 4
f x =
    let x1 = cycle [1,1,1,1]
        x2 = cycle [2,4,8,6]
        x3 = cycle [3,9,7,1]
        x4 = cycle [4,6,4,6]
        m = mod4 x
    in mod5 (x1 !! m + x2 !! m + x3 !! m + x4 !! m)

mod4 x = mod x 4
mod5 x = mod x 5

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

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

solve :: B.ByteString -> Int
solve s = if fst (fromJust $ B.readInt (B.drop (B.length s - 2) s)) `mod` 4 == 0 then 4 else 0

main = interact $ show . solve
solve s | n `mod` 4 == 0 = "4" | otherwise = "0" where
  n = read $ reverse $ take 2 $ reverse s

main = interact $ show . solve
solve s = (1^n + 2^n + 3^n + 4^n) `mod` 5 :: Integer where
  n = read $ reverse $ take 2 $ reverse s

main = interact solve
solve s | n `mod` 4 == 0 = "4" | otherwise = "0" where
  n = read $ reverse $ take 2 $ reverse s

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

main= do
	s<-  C.take 2 . C.reverse <$> C.getLine
	let s1 = read (reverse (C.unpack s)) ::Int
	print $ if mod s1 4 ==0 then 4 else 0

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

-- Meat
solve :: BS.ByteString -> Int
solve s = if divisibleBy4 s then 4 else 0

divisibleBy4 :: BS.ByteString -> Bool
divisibleBy4 s =  toInt str `mod` 4 == 0
    where len = BS.length s
          str = if len < 2 then s else BS.drop (len - 2) s
          toInt = fst .fromJust . BS.readInt

-- Shit
main :: IO ()
main = do
        [s]  <- readShit
        printShit [solve s]

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

yN :: Bool -> String
yN True = "YES"
yN False = "NO"

iif :: x -> x -> Bool-> x
iif a b c = if c then a else b

instance  Shit Int where
    printShit = print
    readShit = fmap read getLine

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

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

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

instance Shit [[Double]] where
    printShit = printShit . (fmap.fmap) show
    readShit = fmap (fmap.fmap $ read) readShit

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

instance Shit String where
    printShit = putStrLn
    printShitV = putStrLn
    readShit = getLine

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

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

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

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

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

-- Meat
solve :: BS.ByteString -> Int
solve s = if divisibleBy4 s then 4 else 0

divisibleBy4 :: BS.ByteString -> Bool
divisibleBy4 s = (suffInt  `mod` 4) == 0
    where len = BS.length s
          suffix = BS.drop (len - 2) s :: BS.ByteString
          suffInt = fst . fromJust . BS.readInt $ suffix :: Int
-- Shit
main :: IO ()
main = do
        [s]  <- readShit
        printShit [solve s]

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

yN :: Bool -> String
yN True = "YES"
yN False = "NO"

iif :: x -> x -> Bool-> x
iif a b c = if c then a else b

instance  Shit Int where
    printShit = print
    readShit = fmap read getLine

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

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

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

instance Shit [[Double]] where
    printShit = printShit . (fmap.fmap) show
    readShit = fmap (fmap.fmap $ read) readShit

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

instance Shit String where
    printShit = putStrLn
    printShitV = putStrLn
    readShit = getLine

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

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

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

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


Polycarp has $$$26$$$ tasks. Each task is designated by a capital letter of the Latin alphabet.The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task.Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice.For example, if Polycarp solved tasks in the following order: "DDBBCCCBBEZ", then the teacher will see that on the third day Polycarp began to solve the task 'B', then on the fifth day he got distracted and began to solve the task 'C', on the eighth day Polycarp returned to the task 'B'. Other examples of when the teacher is suspicious: "BAB", "AABBCCDDEEBZZ" and "AAAAZAAAAA".If Polycarp solved the tasks as follows: "FFGZZZY", then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn't have any suspicious: "BA", "AFFFCC" and "YYYYY".Help Polycarp find out if his teacher might be suspicious.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of days during which Polycarp solved tasks. The second line contains a string of length $$$n$$$, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks.

For each test case output:    "YES", if the teacher cannot be suspicious;  "NO", otherwise.  You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

5
3
ABA
11
DDBBCCCBBEZ
7
FFGZZZY
1
Z
2
AB

module Main where

import Data.List ( group, nub )

isUnsospicious :: String -> String
isUnsospicious tasks
  | taskList == nub taskList = "YES"
  | otherwise                = "NO"
  where
    taskList = map head $ group tasks

solve :: IO String
solve = do
  stringLength <- getLine
  tasks        <- getLine
  return $ isUnsospicious tasks

solution :: Int -> IO [String]
solution 0    = return []
solution test = do
  answer <- solve
  rest   <- solution (test - 1)
  return $ answer : rest

main :: IO ()
main = do
  tests   <- getLine
  results <- solution (read tests)
  mapM_ putStrLn results

import Control.Monad
import Data.List

printResult = do
    n <- readLn :: IO Int
    s <- getLine
    let a = map head $ group s
        r = any (\x->length x > 1) $ group $ sort a
    putStrLn $ if r then "NO" else "YES"

main = do
    t <- readLn :: IO Int
    replicateM_ t printResult

import Data.List
import System.IO
import Control.Monad

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

countConsequence c [] meet = 0
countConsequence c (x:xs) meet
    | c /= x  && meet == False = countConsequence c xs meet
    | c /= x && meet == True = 0
    | otherwise = 1  + (countConsequence c xs True)

forString [] ys = putStrLn"YES"
forString (x:xs) ys = if sum[1|y<-ys,y==x] /= (countConsequence x ys False)
    then putStrLn "NO"
    else forString xs ys

printS = do
    n<-readLn::IO Int
    s<-getLine
    let ys = ['A'..'Z']
    forString ys s

main = do
    t <- readLn::IO Int
    replicateM_ t printS

import Control.Monad
import Data.Containers.ListUtils (nubInt)
import Data.List (group)

main = do
    t <- read <$> getLine
    replicateM_ t $ do
        _ <- getLine
        a <- map fromEnum <$> getLine
        let b = map head $ group a
        putStrLn $
            if b == nubInt b
                then "YES"
                else "NO"

import Control.Arrow
import Data.List
import Data.Bool

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf n xs = let (s, xs') = splitAt n xs in s : chunksOf n xs'

main = interact $
         lines >>> drop 1 >>> chunksOf 2
         >>> map ((!! 1) >>> solve >>> bool "NO" "YES")
         >>> unlines

solve = all ((== 1) . length) . group . sort . map head . group

module Main where

import Control.Monad
import qualified Data.Set as Set

solveString :: String -> String
solveString str = if helper Set.empty str '1' then "Yes" else "No" where
  helper :: Set.Set Char -> String -> Char -> Bool
  helper _ [] _ = True
  helper set (ch : chs) lastChar | Set.member ch set && ch /= lastChar = False
                                 | otherwise = helper (Set.insert ch set) chs ch

solve :: Integer -> IO ()
solve t
  | t == 0 = putStr ""
  | otherwise = do
      n <- getLine
      string <- getLine
      putStrLn $ solveString string
      solve (t - 1)

main :: IO ()
main = do
  t <- getLine
  solve (read t)

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

solve l = let
    chunks = L.group l
    chunksFirst = map head chunks
    chunksFirstUniq = S.fromList chunksFirst
    in
    if length chunksFirst == length chunksFirstUniq then "YES" else "NO"

solveAll [] = []
solveAll (l1:l2:lrest) = (solve l2):(solveAll lrest)

main = do
    stdin <- getContents
    let _lines = lines stdin
    let l1:lrest = _lines
    let n = (read :: String -> Int) l1
    let answers = solveAll lrest
    sequence [putStrLn a | a <- answers]

import Data.List
import Data.Function

solve [] = [""]
solve (x:xs) = [answer $ head xs] ++ (solve $ tail xs)
  where
    answer x = if isNice x
               then "YES"
               else "NO"

    isNice x = all (== True) $
          map (\x -> all (== True) $ map snd x) $
          map isGood $ map (\x -> map snd x) $
          groupBy ((==) `on` fst) $
          sort $
          zip x [0..]
    isGood (x:xs) = scanl (\x y -> if (fst x) + 1 == y
                                   then (y, (snd x) && True)
                                   else (y, False)) (x, True) xs

main = interact $ unlines . helper . lines
  where
    helper (x:xs) = solve xs

module Main where

import Data.List ( group, nub )

isSuspicious :: String -> String
isSuspicious tasks
  | taskList == nub taskList = "YES"
  | otherwise                = "NO"
  where
    taskList = group tasks

solve :: IO String
solve = do
  stringLength <- getLine
  tasks        <- getLine
  return $ isSuspicious tasks

solution :: Int -> IO [String]
solution 0    = return []
solution test = do
  answer <- solve
  rest   <- solution (test - 1)
  return $ answer : rest

main :: IO ()
main = do
  tests   <- getLine
  results <- solution (read tests)
  mapM_ putStrLn results


Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer $$$n$$$, he encrypts it in the following way: he picks three integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$l \leq a,b,c \leq r$$$, and then he computes the encrypted value $$$m = n \cdot a + b - c$$$.Unfortunately, an adversary intercepted the values $$$l$$$, $$$r$$$ and $$$m$$$. Is it possible to recover the original values of $$$a$$$, $$$b$$$ and $$$c$$$ from this information? More formally, you are asked to find any values of $$$a$$$, $$$b$$$ and $$$c$$$ such that  $$$a$$$, $$$b$$$ and $$$c$$$ are integers,  $$$l \leq a, b, c \leq r$$$,  there exists a strictly positive integer $$$n$$$, such that $$$n \cdot a + b - c = m$$$.

The first line contains the only integer $$$t$$$ ($$$1 \leq t \leq 20$$$) — the number of test cases. The following $$$t$$$ lines describe one test case each. Each test case consists of three integers $$$l$$$, $$$r$$$ and $$$m$$$ ($$$1 \leq l \leq r \leq 500\,000$$$, $$$1 \leq m \leq 10^{10}$$$). The numbers are such that the answer to the problem exists.

For each test case output three integers $$$a$$$, $$$b$$$ and $$$c$$$ such that, $$$l \leq a, b, c \leq r$$$ and there exists a strictly positive integer $$$n$$$ such that $$$n \cdot a + b - c = m$$$. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions.

NoteIn the first example $$$n = 3$$$ is possible, then $$$n \cdot 4 + 6 - 5 = 13 = m$$$. Other possible solutions include: $$$a = 4$$$, $$$b = 5$$$, $$$c = 4$$$ (when $$$n = 3$$$); $$$a = 5$$$, $$$b = 4$$$, $$$c = 6$$$ (when $$$n = 3$$$); $$$a = 6$$$, $$$b = 6$$$, $$$c = 5$$$ (when $$$n = 2$$$); $$$a = 6$$$, $$$b = 5$$$, $$$c = 4$$$ (when $$$n = 2$$$).In the second example the only possible case is $$$n = 1$$$: in this case $$$n \cdot 2 + 2 - 3 = 1 = m$$$. Note that, $$$n = 0$$$ is not possible, since in that case $$$n$$$ is not a strictly positive integer.

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

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

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

readB8Int64 :: B8.ByteString -> Int64
readB8Int64 = fromInteger . readB8Integer

readB8Integer :: B8.ByteString -> Integer
readB8Integer = fst . fromJust . B8.readInteger

type Solution = (Int64, Int64, Int64)

solve :: Int64 -> Int64 -> Int64 -> Solution
solve l r m = answer
    where
        !leftAllowed = l - r
        !rightAllowed = r - l

        checkNAllowed :: Int64 -> Int64 -> Bool
        checkNAllowed a n = (n > 0) && (inRange (leftAllowed, rightAllowed) $ m - n * a)

        findN :: Int64 -> Maybe Int64
        findN a = find (checkNAllowed a) [leftN - 1, leftN, leftN + 1, rightN - 1, rightN, rightN + 1]
            where
                leftN = max 1 $ (m - rightAllowed) `div` a
                rightN = max 1 $ (m - leftAllowed) `div` a

        a :: Int64
        !a = fromJust . find (isJust . findN) $ [l .. r]

        findB :: Int64 -> Int64 -> Int64
        findB n a = fromJust . find (\b -> inRange (l, r) (- (m - n * a - b))) $ [l .. r]

        findC :: Int64 -> Int64 -> Int64 -> Int64
        findC n a b = - (m - n * a - b)

        answer :: Solution
        !answer = (a, b, c)
            where
                !n = fromJust $ findN a
                !b = findB n a
                !c = findC n a b

main :: IO ()
main = do
    testCount <- readB8Int <$> B8.getLine
    replicateM_ testCount $ do
        [l, r, m] <- (map readB8Int64) <$> B8.words <$> B8.getLine
        let (a, b, c) = solve l r m
        printf "%lld %lld %lld\n" a b c


Polycarp is a big lover of killing time in social networks. A page with a chatlist in his favourite network is made so that when a message is sent to some friend, his friend's chat rises to the very top of the page. The relative order of the other chats doesn't change. If there was no chat with this friend before, then a new chat is simply inserted to the top of the list.Assuming that the chat list is initially empty, given the sequence of Polycaprus' messages make a list of chats after all of his messages are processed. Assume that no friend wrote any message to Polycarpus.

The first line contains integer n (1 ≤ n ≤ 200 000) — the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10.

Print all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom.

8
alina
maria
ekaterina
darya
darya
ekaterina
maria
alina

NoteIn the first test case Polycarpus first writes to friend by name "alex", and the list looks as follows:   alex Then Polycarpus writes to friend by name "ivan" and the list looks as follows:  ivan  alex Polycarpus writes the third message to friend by name "roman" and the list looks as follows:  roman  ivan  alex Polycarpus writes the fourth message to friend by name "ivan", to who he has already sent a message, so the list of chats changes as follows:  ivan  roman  alex

import qualified Data.ByteString.Char8 as C
import qualified Data.Set as S
main = C.interact $ C.unlines . reverse . fst . foldr f ([],S.empty) . tail . C.lines . C.filter (/='\r')
f n (l,s) | S.member n s = (l,s)
          | True = (n:l,S.insert n s)

import Data.Set

main = do
  getLine
  interact $ unlines.solve empty.reverse.lines

solve _ [] = []
solve s (x:xs)
  |not(x`member`s) = x:solve(x`insert`s) xs
  |otherwise = solve s xs


There are $$$n$$$ logs, the $$$i$$$-th log has a length of $$$a_i$$$ meters. Since chopping logs is tiring work, errorgorn and maomao90 have decided to play a game.errorgorn and maomao90 will take turns chopping the logs with errorgorn chopping first. On his turn, the player will pick a log and chop it into $$$2$$$ pieces. If the length of the chosen log is $$$x$$$, and the lengths of the resulting pieces are $$$y$$$ and $$$z$$$, then $$$y$$$ and $$$z$$$ have to be positive integers, and $$$x=y+z$$$ must hold. For example, you can chop a log of length $$$3$$$ into logs of lengths $$$2$$$ and $$$1$$$, but not into logs of lengths $$$3$$$ and $$$0$$$, $$$2$$$ and $$$2$$$, or $$$1.5$$$ and $$$1.5$$$.The player who is unable to make a chop will be the loser. Assuming that both errorgorn and maomao90 play optimally, who will be the winner?

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 50$$$)  — the number of logs. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 50$$$)  — the lengths of the logs. Note that there is no bound on the sum of $$$n$$$ over all test cases.

For each test case, print "errorgorn" if errorgorn wins or "maomao90" if maomao90 wins. (Output without quotes).

NoteIn the first test case, errorgorn will be the winner. An optimal move is to chop the log of length $$$4$$$ into $$$2$$$ logs of length $$$2$$$. After this there will only be $$$4$$$ logs of length $$$2$$$ and $$$1$$$ log of length $$$1$$$.After this, the only move any player can do is to chop any log of length $$$2$$$ into $$$2$$$ logs of length $$$1$$$. After $$$4$$$ moves, it will be maomao90's turn and he will not be able to make a move. Therefore errorgorn will be the winner.In the second test case, errorgorn will not be able to make a move on his first turn and will immediately lose, making maomao90 the winner.

{-# LANGUAGE TypeApplications, BlockArguments #-}

module Main where

import           Data.List
import           Data.Functor
import           Control.Monad

main :: IO ()
main =
  getLine
    <&> read @Int
    >>= flip replicateM (getLine >> getLine <&> map (read @Int) . words)
    >>= mapM_
          (putStrLn . \a -> if odd . sum . map (flip (-) 1) . filter (/= 1) $ a
            then "errorgorn"
            else "maomao90"
          )

{-# LANGUAGE TypeApplications, BlockArguments #-}

module Main where

import           Data.List
import           Data.Functor
import           Control.Monad

data Turn = ErrorGorn | MaoMao90 deriving Show

type Winner = Turn

play :: Int -> [Int] -> Winner
play count logs =
  let chosenOne = maximum logs
  in  if chosenOne == 1
        then if odd count then MaoMao90 else ErrorGorn
        else
          let fstPiece = chosenOne `div` 2
              sndPiece = chosenOne - fstPiece
          in  play (count + 1) (fstPiece : sndPiece : delete chosenOne logs)

main :: IO ()
main =
  getLine
    <&> read @Int
    >>= flip replicateM (getLine >> getLine <&> map (read @Int) . words)
    >>= mapM_ (print . (1 `play`))

{-# LANGUAGE TypeApplications, OverloadedStrings, BlockArguments #-}

module Main where

import           Control.Monad
import           Data.Functor

main :: IO ()
main =
  getLine
    <&> read @Int
    >>= flip replicateM (getLine >> getLine <&> map (read @Int) . words)
    >>= mapM_ \a ->
          let s = sum a
          in  putStrLn if s == 1
                then "maomao90"
                else if odd s then "errorgorn" else "maomao90"


Nikolay lives in a two-storied house. There are $$$n$$$ rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between $$$1$$$ and $$$n$$$). If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers $$$i$$$ and $$$i+1$$$ on each floor are neighbouring, for all $$$1 \leq i \leq n - 1$$$. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room $$$x$$$ on the first floor and the room $$$x$$$ on the second floor, then Nikolay can use it to move from one room to another.     The picture illustrates a house with $$$n = 4$$$. There is a staircase between the room $$$2$$$ on the first floor and the room $$$2$$$ on the second floor, and another staircase between the room $$$4$$$ on the first floor and the room $$$4$$$ on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string "0101" in the input.  Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). Calculate the maximum number of rooms Nikolay can visit during his tour, if:  he can start in any room on any floor of his choice,  and he won't visit the same room twice.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines. The first line contains one integer $$$n$$$ $$$(1 \le n \le 1\,000)$$$ — the number of rooms on each floor. The second line contains one string consisting of $$$n$$$ characters, each character is either a '0' or a '1'. If the $$$i$$$-th character is a '1', then there is a staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. If the $$$i$$$-th character is a '0', then there is no staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied.

For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.

NoteIn the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits $$$6$$$ rooms.There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).In the third test case it is possible to visit all rooms: first floor, first room $$$\rightarrow$$$ second floor, first room $$$\rightarrow$$$ second floor, second room $$$\rightarrow$$$ first floor, second room $$$\rightarrow$$$ first floor, third room $$$\rightarrow$$$ second floor, third room $$$\rightarrow$$$ second floor, fourth room $$$\rightarrow$$$ first floor, fourth room $$$\rightarrow$$$ first floor, fifth room $$$\rightarrow$$$ second floor, fifth room.In the fourth test case it is also possible to visit all rooms: second floor, third room $$$\rightarrow$$$ second floor, second room $$$\rightarrow$$$ second floor, first room $$$\rightarrow$$$ first floor, first room $$$\rightarrow$$$ first floor, second room $$$\rightarrow$$$ first floor, third room.

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.ST

import Data.Char
import Data.List
import Data.Maybe

import qualified Data.ByteString.Char8 as B

import Data.Array.ST.Safe
import Data.STRef

-- import Debug.Trace
import Text.Printf

readInt = readLn :: IO Int
readInteger = readLn :: IO Integer
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readIntegers = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine

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

modifyArray a i f = writeArray a i =<< f <$> readArray a i

main = readInt >>= flip replicateM solve >>= mapM_ print

solve = do
	n <- readInt
	s <- getLine
	let
		indices = map snd $ filter ( ( == '1' ) . fst ) $ zip s [ 1 .. ]
		indices' = map ( \i -> max i $ n + 1 - i ) indices
	return $ maximum $ ( n : ) $ map ( 2 * ) indices'


Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not.In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one.   You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:   Each bracket is either not colored any color, or is colored red, or is colored blue.  For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored.  No two neighboring colored brackets have the same color. Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109 + 7).

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7).

NoteLet's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.     The two ways of coloring shown below are incorrect.

import Data.Int
import Data.Map as M
import qualified Data.ByteString.Char8 as BS
import Control.Monad

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

evalState :: State s a -> s -> a
evalState act = fst . runState act

get :: State s s
get = State $ \s -> (s, s)

put :: s -> State s ()
put s = State $ \_ -> ((), s)

modify :: (s -> s) -> State s ()
modify act = do
    s <- get
    put (act s)

instance Monad (State s) where
    return x = State $ \s -> (x, s)
    a1 >>= a2 = State $ \s0 ->
        let (a, s1) = runState a1     s0
            (b, s2) = runState (a2 a) s1
        in (b, s2)

tof :: String -> (Int -> Char)
tof str = (str !!)

nextb fstr n level | level == 0 = n-1
nextb fstr n level | fstr n == '(' = nextb fstr (n+1) (level+1)
nextb fstr n level | fstr n == ')' = nextb fstr (n+1) (level-1)

sum' = foldr1 (\x y -> (x + y) `mod` 1000000007)

times :: Int -> Int -> Int
times a b = let
    a' = fromIntegral a :: Int64
    b' = fromIntegral b :: Int64
    in fromIntegral $ ((a' * b') `mod` 1000000007)

type S = Map (Int, Int, Int, Int) Int
type Mem = State S

solve :: (Int -> Char) -> Int -> Int
solve fstr n =
    let
        f s t a b         = do
            m <- get
            case M.lookup (s, t, a, b) m of
               Just x -> return x
               Nothing -> do
                   ans <- f' s t a b
                   modify $ insert (s, t, a, b) ans
                   return ans

        f' s t a b = let m = nextb fstr (s+1) 1 in
           if m == t
             then
               if (a == 0) == (b == 0)
                 then return 0
                 else if s == t-1 then return 1
                 else liftM sum' $ sequence [ f (s+1) (t-1) a' b'
                                            | a' <- [0..2], a' /= a || a' == 0,
                                              b' <- [0..2], b' /= b || b' == 0 ]
             else
               liftM sum' $ sequence [ liftM2 times (f s m a a') (f (m+1) t b' b)
                                     | a' <- [0..2],
                                       b' <- [0..2], b' /= a' || b' == 0 ]

    in evalState (liftM sum' $ sequence $ [ f 0 (n-1) a b
                                          | a <- [0..2]
                                          , b <- [0..2] ]) M.empty

main = do
    [l] <- liftM BS.words BS.getLine

    let fstr = BS.head . flip BS.drop l
    let n    = BS.length l

    print $ solve fstr n


Alice and Bob play a game. Alice has $$$n$$$ cards, the $$$i$$$-th of them has the integer $$$a_i$$$ written on it. Bob has $$$m$$$ cards, the $$$j$$$-th of them has the integer $$$b_j$$$ written on it.On the first turn of the game, the first player chooses one of his/her cards and puts it on the table (plays it). On the second turn, the second player chooses one of his/her cards such that the integer on it is greater than the integer on the card played on the first turn, and plays it. On the third turn, the first player chooses one of his/her cards such that the integer on it is greater than the integer on the card played on the second turn, and plays it, and so on — the players take turns, and each player has to choose one of his/her cards with greater integer than the card played by the other player on the last turn.If some player cannot make a turn, he/she loses.For example, if Alice has $$$4$$$ cards with numbers $$$[10, 5, 3, 8]$$$, and Bob has $$$3$$$ cards with numbers $$$[6, 11, 6]$$$, the game may go as follows:  Alice can choose any of her cards. She chooses the card with integer $$$5$$$ and plays it.  Bob can choose any of his cards with number greater than $$$5$$$. He chooses a card with integer $$$6$$$ and plays it.  Alice can choose any of her cards with number greater than $$$6$$$. She chooses the card with integer $$$10$$$ and plays it.  Bob can choose any of his cards with number greater than $$$10$$$. He chooses a card with integer $$$11$$$ and plays it.  Alice can choose any of her cards with number greater than $$$11$$$, but she has no such cards, so she loses. Both Alice and Bob play optimally (if a player is able to win the game no matter how the other player plays, the former player will definitely win the game).You have to answer two questions:  who wins if Alice is the first player?  who wins if Bob is the first player?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of four lines. The first line of a test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of cards Alice has. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 50$$$) — the numbers written on the cards that Alice has. The third line contains one integer $$$m$$$ ($$$1 \le m \le 50$$$) — the number of Bob's cards. The fourth line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le 50$$$) — the numbers on Bob's cards.

For each test case, print two lines. The first line should be Alice if Alice wins when she is the first player; otherwise, the first line should be Bob. The second line should contain the name of the winner if Bob is the first player, in the same format.

NoteLet's consider the first test case of the example.Alice has one card with integer $$$6$$$, Bob has two cards with numbers $$$[6, 8]$$$.If Alice is the first player, she has to play the card with number $$$6$$$. Bob then has to play the card with number $$$8$$$. Alice has no cards left, so she loses.If Bob is the first player, then no matter which of his cards he chooses on the first turn, Alice won't be able to play her card on the second turn, so she will lose.

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

main :: IO ()
main = do
	n <- getLine
	let number = read n :: Int
	solve number number

solve number 0 = return ()
solve number current = do
	n <- getLine
	alice <- getLine
	let aliceCards = (map read $ words alice) :: [Int]
	m <- getLine
	bob <- getLine
	let bobCards = (map read $ words bob) :: [Int]
	if (maximum aliceCards) > (maximum bobCards) then do
		putStrLn "Alice"
		putStrLn "Alice"
	else if (maximum aliceCards == maximum bobCards) then do
		putStrLn "Alice"
		putStrLn "Bob"
		else do
			putStrLn "Bob"
			putStrLn "Bob"
	solve number (current - 1)

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Word (Word32)
import System.Environment (getArgs)
import GHC.Conc (par, pseq)
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust)
import Control.Monad.State (State, evalState, gets, MonadState (get, put), replicateM)
import Control.Applicative (Applicative(liftA2))
import Control.Arrow ((>>>))

data TC = TC { a::[Int], b::[Int] }

aliceBob x = if x then "Alice" else "Bob"

solve TC{..} = (aliceBob $ ma >= mb, aliceBob $ ma > mb)
  where
    ma = maximum a
    mb = maximum b

main = C.interact $ runScanner scan >>> map solve >>> map (\(x, y) -> [x, y]) >>> concat >>> map C.pack >>> C.unlines

scan =
  numberOf $ TC <$> numberOf int <*> numberOf int

type Scanner = State [C.ByteString]

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

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

peek :: Scanner C.ByteString
peek = gets head

str :: Scanner C.ByteString
str = get >>= \case { s:ss -> put ss >> return s }

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

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

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

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

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

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

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

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

(><) = times

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

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

import Control.Monad (replicateM_)

main :: IO ()
main = do
    t <- readLn :: IO Int
    replicateM_ t $ do
        _ <- getLine
        l1 <- getLine
        let a = (map read . words) l1 :: [Int]
        _ <- getLine
        l2 <- getLine
        let b = (map read . words) l2 :: [Int]
        let ma = maximum a
            mb = maximum b
        putStrLn $
            if ma == mb
                then "Alice\nBob"
                else if ma > mb then "Alice\nAlice" else "Bob\nBob"


Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it?The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.

The input consists of a single string $$$s$$$ ($$$2 \leq |s| \leq 10^6$$$). The string $$$s$$$ consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal.

Output a palindrome $$$t$$$ that is a subsequence of $$$s$$$ and $$$|t| \geq \lfloor \frac{|s|}{2} \rfloor$$$. If there are multiple solutions, you may print any of them. You don't have to maximise the length of $$$t$$$. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity).

NoteIn the first example, other valid answers include "cacac", "caac", "aca" and "ccc".

{-# OPTIONS_GHC -O2 #-}

import Data.Sequence (Seq, Seq((:<|)), Seq((:|>)), Seq(Empty), fromList)

solve :: Seq Char -> Seq Char
solve s = solveR id s Empty
  where
    solveR r Empty = r
    solveR r ((a :<| b :<| s') :|> b' :|> a') =
      let (x, y) = head [(x, y) | x <- [a, b], y <- [b', a'], x == y]
       in solveR (r . (x :<|) . (:|> y)) s'
    solveR r (a :<| _) = r . (a :<|)

main = do
  s <- getLine
  putStrLn . foldr (:) [] . solve . fromList $ s