In this problem you have to solve a simple classification task: given an image, determine whether it depicts a Fourier doodle. You are given a set of 50 images with ids 1 through 50. You are also given a text file labels.txt containing the labels for images with ids 1 through 20, which comprise the learning data set. You have to output the classification results for images with ids 21 through 50 in the same format.

Download the images and the training labels Each line of the file labels.txt contains a single integer 0 or 1. Line $$$i$$$ (1-based) contains the label of the image {i}.png. Label 1 means that the image depicts a Fourier doodle, label 0 - that it does not.

Output 30 lines, one line per image 21 through 50. Line $$$i$$$ (1-based) should contain the classification result for the image {i + 20}.png.

m i = if ((min i 25) + i) `mod` (2 + (i `mod` 3)) > 0 then 1 else 0
main = putStr . unlines . map (show . m) $ [21..50]


A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward").You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?

The first line of input contains a string commands — the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F". The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list.

Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list.

NoteIn the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.

module Main where

import Data.Array

main = interact (solve . lines)

solve :: [String] -> String
--solve [prog, n'] = unlines [show [dyn ! (k, l) | l <- [0..len]] | k <- [0..n]] where
solve [prog, n'] = show result where
    result = max (abs $ fst bestTuple) (abs $ snd bestTuple)
    bestTuple = foldl1 best [dyn ! (k, 0) | k <- [0, 2..n]]
    dyn = array cells [(x, distance x) | x <- range cells]
    cells = ((0, 0), (n, len))
    n = read n'
    len = length prog

    distance (k, l)
        | l == len   = if k == n then (0, 0) else (10000, -10000)
        | k == n     = effect (prog !! l) (dyn ! (k, l + 1))
        | otherwise  = best
            (effect (prog !! l) (dyn ! (k, l + 1)))
            (effect (rev $ prog !! l) (dyn ! (k + 1, l + 1)))

effect 'T' (a, b) = (-b, -a)
effect 'F' (a, b) = (a + 1, b + 1)

rev 'F' = 'T'
rev 'T' = 'F'

best :: (Int, Int) -> (Int, Int) -> (Int, Int)
best (a, b) (c, d) = (min a c, max b d)

-- distance :: (Int, Int) -> (Int, Int)

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

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

parseInput = do
    moves <- BS.unpack <$> readString
    k <- readInt
    return (moves, k)
  where
    readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace
    readInteger = state $ fromJust . BS.readInteger . BS.dropWhile isSpace
    readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace
    readLine = state $ BS.span (not . isEoln) . BS.dropWhile isEoln
    isEoln ch = ch == '\r' || ch == '\n'

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

addInterval (a, b) (c, d) = (min a c, max b d)
nonInterval = (inf, -inf)
inf = 10^9

solve (moves', k) = max (-ansMin) ansMax
  where
    n = length moves'
    moves = listArray (0, n - 1) moves' :: UArray Int Char

    (ansMin, ansMax) = turtle (0, k, True)

    turtle :: (Int, Int, Bool) -> (Int, Int)
    turtle (pos, rem, dir) | rem < 0   = nonInterval
                           | otherwise = cache ! (pos, rem, dir)
      where
        bnds = ((0, 0, False), (n, k, True))
        cache = listArray bnds $ map go $ range bnds :: Array (Int, Int, Bool) (Int, Int)

        go (pos, rem, dir) | pos == n = if even rem then (0, 0) else nonInterval
        go (pos, rem, dir) = movesF `addInterval` movesT
          where
            pdir = (moves ! pos) == 'F'

            movesF | dir       = (minv + 1, maxv + 1)
                   | otherwise = (minv - 1, maxv - 1)
              where
                (minv, maxv) = turtle (pos + 1, rem - (if pdir then 0 else 1), dir)

            movesT = turtle (pos + 1, rem - (if pdir then 1 else 0), not dir)

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

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

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

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

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

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

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

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

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

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

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

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

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

state = State


Masha has three sticks of length $$$a$$$, $$$b$$$ and $$$c$$$ centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks.What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices.

The only line contains tree integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq a, b, c \leq 100$$$) — the lengths of sticks Masha possesses.

Print a single integer — the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks.

NoteIn the first example, Masha can make a triangle from the sticks without increasing the length of any of them.In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length $$$2$$$ centimeter stick by one and after that form a triangle with sides $$$3$$$, $$$3$$$ and $$$5$$$ centimeters.In the third example, Masha can take $$$33$$$ minutes to increase one of the $$$10$$$ centimeters sticks by $$$33$$$ centimeters, and after that take $$$48$$$ minutes to increase another $$$10$$$ centimeters stick by $$$48$$$ centimeters. This way she can form a triangle with lengths $$$43$$$, $$$58$$$ and $$$100$$$ centimeters in $$$81$$$ minutes. One can show that it is impossible to get a valid triangle faster.

import Control.Applicative
import Data.List

main = do
  [a,b,c] <- map read . words <$> getLine :: IO [Int]

  let t = [(i,j,k) | i<-[1..200], j<-[1..200], k<-[1..200], i+j>k, i+k>j, j+k>i]

  print $ minimum [(abs $ i-a) + (abs $ j-b) + (abs $ k-c) | (i,j,k) <- t]

--Codeforces 10640

isTriangle :: Int -> Int -> Int -> Bool
isTriangle a b c = (a + b) > c && abs(a - b) < c

makeTriangle :: Int -> Int -> Int -> Int
makeTriangle a b c
    | isTriangle a b c = 0
    | otherwise = 2 * maximum [a, b, c] + 1 - sum [a, b, c]

main :: IO ()
main = do
    input <- getLine
    let [a, b, c] = map (read :: String -> Int) (words input)
    print (makeTriangle a b c)

import Data.List

main =do
      e<-getLine
      let (a:b:c:[])=sort $ map read (words e)::[Int]
      print $ if c-b<a then 0 else (c-b-a+1)

import Data.List
main = interact $ show . solve . sort . map read . words
solve [a,b,c] = max 0 (c-b-a+1)

import Data.List

main = getLine >>= print . solve . sort . map read . words

solve (a:b:c:_) = max 0 (c - b - a + 1)

main :: IO ()
main = do
    ln <- getLine
    let (a:b:c:_) = scanall ln in putStrLn (show (ans a b c))

ans :: Integral i => i -> i -> i -> i
ans a b c
    | a + b <= c = p c a b
    | a + c <= b = p b a c
    | b + c <= a = p a b c
    | otherwise = 0
    where p x y z = 1 + (x - (y + z))

scanall :: Read a => String -> [a]
scanall s = [read x | x <- split ' ' s]

split :: Char -> String -> [String]
split c "" = []
split c s = a : split c b where (a, b) = splitfirst c s

splitfirst :: Char -> String -> (String, String)
splitfirst c "" = ("", "")
splitfirst c (x:xs)
    | c == x = ("", xs)
    | otherwise = (x : a, b)
    where (a, b) = splitfirst c xs

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

process n | x>0 = 0
          | otherwise = 1-x
	where x =( sum n)- 2*maximum n

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

--ghc 7.10

makeTriangle :: Int -> Int -> Int -> Int
makeTriangle a b c
    | d < 0 = 0
    | otherwise = d+1
    where
        maxSide = max a (max b c)
        d = 2 * maxSide - (a + b + c)

main = do
    abcStr <- getLine
    let [a,b,c] = map read . words $ abcStr
    print $ makeTriangle a b c


The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds.The elimination rounds are divided into main and additional. Each of the main elimination rounds consists of c problems, the winners of the round are the first n people in the rating list. Each of the additional elimination rounds consists of d problems. The winner of the additional round is one person. Besides, k winners of the past finals are invited to the finals without elimination.As a result of all elimination rounds at least n·m people should go to the finals. You need to organize elimination rounds in such a way, that at least n·m people go to the finals, and the total amount of used problems in all rounds is as small as possible.

The first line contains two integers c and d (1 ≤ c, d ≤ 100) — the number of problems in the main and additional rounds, correspondingly. The second line contains two integers n and m (1 ≤ n, m ≤ 100). Finally, the third line contains an integer k (1 ≤ k ≤ 100) — the number of the pre-chosen winners.

In the first line, print a single integer — the minimum number of problems the jury needs to prepare.

main = interact $ show . max 0 . f . map read . words
f [c, d, n, m, k] = minimum [let (x, y) = divMod (n * m - k) n in x * c + y * d, d * (n * m - k), div (n * m - k + n - 1) n * c]

import Data.List

solve c d n m k =
  case nums of
    [] -> 0
    _  -> minimum nums
  where f (a,b) = a*c+b*d
        end = m + ((n-1-k) `div` n)
        nums = map f [ (a,b) | a <- [0..end], let b = max 0 (n*m-k-n*a) ]

main = do
  (c:d:_) <- fmap (map read . words) getLine
  (n:m:_) <- fmap (map read . words) getLine
  k <- fmap read getLine
  print $ solve c d n m k

import Control.Applicative

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

main = do
  [c, d] <- readInts
  [n, m] <- readInts
  k <- readLn
  let
    f x y = c * x + d * y
    c1 = f ((n*m-k+n-1) `div` n) 0
    c2 = f ((n*m-k) `div` n) (((-k) `mod` n + n) `mod` n)
    c3 = f 0 (n*m-k)
   in putStrLn . show $ max (minimum [c1, c2, c3]) 0

import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Array

readInt :: IO Integer
readInt = read <$> getLine
readIntList :: IO [Integer]
readIntList = fmap read . words <$> getLine
readIntArray :: Integer -> IO (Array Integer Integer)
readIntArray n = listArray (0, n-1) <$> readIntList
readSortedIntArray :: Integer -> IO (Array Integer Integer)
readSortedIntArray n = listArray (0, n-1) . sort <$> readIntList

main :: IO ()
main = do
  [c, d] <- readIntList
  [n, m] <- readIntList
  [k] <- readIntList
  putStrLn . show $ minimum $ flip fmap [0..1000] $ \i -> i * c + (max 0 $ n * m - i * n - k) * d

main = interact $ show . f . map read . words
f [c, d, n, m, k] = minimum [let (x, y) = divMod (n * m - k) n in x * c + y * d, d * (n * m - k), div (n * m - k + n - 1) n * c]

import Control.Applicative

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

main = do
  [c, d] <- readInts
  [n, m] <- readInts
  k <- readLn
  let
    f x y = c * x + d * y
    c1 = f ((n*m-k+n-1) `div` n) 0
    c2 = f ((n*m-k) `div` n) (((-k) `mod` n + n) `mod` n)
    c3 = f 0 (n*m-k)
   in putStrLn . show $ minimum [c1, c2, c3]

import Control.Applicative

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

main = do
  [c, d] <- readInts
  [n, m] <- readInts
  k <- readLn
  let
    binarySearch lb ub
      | ub-lb > 1 = if can mid then binarySearch lb mid else binarySearch mid ub
      | otherwise = ub
      where mid = (lb+ub) `div` 2
    value z = if n*d > c then n*d*z+k*c*d else c*z+k*c*d
    can z = value z >= n * m * c * d
   in putStrLn . show $ binarySearch (-1) 1000000


Valera has got n domino pieces in a row. Each piece consists of two halves — the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even.To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true.

The first line contains integer n (1 ≤ n ≤ 100), denoting the number of dominoes Valera has. Next n lines contain two space-separated integers xi, yi (1 ≤ xi, yi ≤ 6). Number xi is initially written on the upper half of the i-th domino, yi is initially written on the lower half.

Print a single number — the minimum required number of seconds. If Valera can't do the task in any time, print  - 1.

NoteIn the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything.In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd.In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.

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

main = do
	n <- readLn
	ps <- replicateM n $ do
		[ a, b ] <- map read . words <$> getLine
		return ( a, b )
	print $ solve ps

solve ps
	| sum1 `mod` 2 == 0 && sum2 `mod` 2 == 0 = 0
	| sum1 `mod` 2 == 1 && sum2 `mod` 2 == 1 =
		if any ( uncurry (&&) . ( even *** odd ) ) ps || any ( uncurry (&&) . ( odd *** even ) ) ps
			then 1
			else -1
	| otherwise = -1
		where
			sum1 = sum . map fst $ ps
			sum2 = sum . map snd $ ps

import Data.Functor
import Control.Monad

main = do
     n <- read <$> getLine :: IO Int
     ls <- replicateM n ((map read) <$> (words <$> getLine)) :: IO [[Int]]
     putStrLn (show (solve n ls))
     return ()

solve n ds
      | even (sum (map fst ds')) && even (sum (map snd ds')) = 0
      | odd (sum (map fst ds')) && odd (sum (map snd ds')) && any (\(x,y) -> odd (x+y)) ds' = 1
      | otherwise = -1
      where
            ds' = map (\(x:y:_) -> (x,y)) ds

{-# 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 (&&)

xor True True = False
xor a b = a || b

main = do
    n <- readLn
    l <- replicateM n ((\[x,y] -> (odd x,odd y)) <$> readsLine)
    let (lu,ll) = unzip l
    case (foldl1 xor lu,foldl1 xor ll) of
        (False,False)  -> print 0
        (True,False) -> print (-1)
        (False,True) -> print (-1)
        (True,True) -> case find (uncurry xor) l of
            Just _ -> print 1
            Nothing -> print (-1)

import Control.Monad (liftM, replicateM)
import Data.Char (ord)
import Prelude hiding (reads)

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

readTuple :: Num a => IO (a, a)
readTuple  =  do
    [a, b]  <-  reads
    return (a, b)

solve :: [(Int, Int)] -> Int
solve [(a, b)]
  | even a && even b  =  0
  | otherwise  =  -1
solve ds
  | even up && even down  =  0
  | odd up && odd down && findOddSum  =  1
  | otherwise  =  -1
  where
    up  =  sum $ map fst ds
    down  =  sum $ map snd ds
    findOddSum  =  any (odd . uncurry (+)) ds

main :: IO ()
main  =  do
    n  <-  readLn
    ds  <-  replicateM n readTuple
    print $ solve ds

import Data.List (group, transpose)

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

solve :: [[Int]] -> Int
solve xys = case (map (even . sum) (transpose xys), length (filter ((>1) . length . group . map even) xys ) > 1) of
                 ([ True, True ], _) -> 0
                 ([ False, False ], True) -> 1
                 _ -> -1

import Data.List (group, transpose)

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

solve :: [[Int]] -> Int
solve xys | all even (map sum (transpose xys)) = 0
          | length (group (map (even . sum) (transpose xys))) == 1 && length (filter ((>1) . length . group . map even) xys ) > 1 = 1
          | otherwise = -1

main=interact$show.f.parse.map read.tail.words
parse (x:y:xys) = (x,y):parse xys
parse _ = []

f xys
  | p xys = 0
  | any (p.swap xys) [1..100] = 1
  | otherwise = -1

swap xys i
  | (xys0,(x,y):xys1) <- splitAt (i-1) xys = xys0 ++ (y,x) : xys1
  | otherwise = xys

p xys = even (sum xs) && even (sum ys)
   where
     (xs,ys) = unzip xys

solve :: (Integer, Integer, Integer) -> Integer
solve (both, top, bottom)
    | ((both `mod` 2) == 0) = case (top `mod` 2, bottom `mod` 2) of
        (0, 0) -> 0
        (1, 1) -> 1
        (0, 1) -> -1
        (1, 0) -> -1
    | otherwise = case (top `mod` 2, bottom `mod` 2) of
        (1, 1) -> 0
        (0, 1) -> -1
        (1, 0) -> -1
        (0, 0) -> if (top + bottom) > 0 then 1 else -1

calc :: [String] -> (Integer, Integer, Integer)
calc [] = (0, 0, 0)
calc (x:xs) = case (top `mod` 2, bottom `mod` 2) of
        (0, 0) -> (pboth, ptop, pbottom)
        (0, 1) -> (pboth, ptop, pbottom+1)
        (1, 0) -> (pboth, ptop+1, pbottom)
        (1, 1) -> (pboth+1, ptop, pbottom)
    where
        (pboth, ptop, pbottom) = calc xs
        [top, bottom] = map read $ words x

get :: Integer -> IO [String]
get 0 = return []
get n = do
    s <- getLine
    ls <- get (n-1)
    return $ s : ls

main = do
    n <- getLine
    a <- get (read n)
    print $ solve (calc a)

import Control.Applicative
import Data.List

main= do
	s<-  getLine
	[a1,a2]<- transpose. map (map read). map words.lines  <$> getContents::IO [[Int]]
	let s1 = sum a1
	let s2 = sum a2
	print $ if even s1 && even s2 then 0
                              else if odd (s1+s2) then -1
                                                  else if length (take 1 (filter  odd (zipWith (+) a1 a2))) ==1 then 1
														else -1

module Main where
---------------------Import--------------------------- {{{
import Data.List
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Char
import System.IO
import Data.Ratio                                   -- x = 5%6
import Data.Bits                                    -- (.&.), (.|.), shiftL...
import Text.Printf                                  -- printf "%0.6f" (1.0)
import qualified Data.ByteString.Char8 as BS-- }}}

---------------------Input---------------------------- {{{
getInteger = (\(Just (x,_)) -> x). BS.readInteger
getInt = (\(Just (x,_)) -> x). BS.readInt

getIntArray = readIntArray
getIntegerArray = readIntegerArray

readIntArray input =
  case x of
    Just (a, xs) -> a : readIntArray xs
    Nothing -> []
  where
   x = BS.readInt. BS.dropWhile isSpace $ input

readIntegerArray input =
  case x of
    Nothing -> []
    Just (y, ys) -> y : readIntegerArray ys
  where
    x = BS.readInteger. BS.dropWhile isSpace $ input
------------------------------------------------------ }}}

parse [] = []
parse (x:y:xxs) = (x, y): parse xxs

main :: IO ()
main =  BS.getContents >>= print. solve. parse. tail. readIntArray

--solve :: [(Int, Int)] -> Bool
solve arr =
  let
    oddPair = not. null. filter (\(x, y) -> odd x && odd y) $ arr
    (xs, ys) = foldl' (\(acx, acy) (x, y) -> (acx+x, acy+y)) (0, 0) arr
    p1 = not. null. filter (\(x, y) -> even (xs - x + y) && even (ys - y+x) ) $ arr
  in
    if even xs && even ys
      then 0
      else if p1
        then 1
        else (-1)

module Main where

main = do
  n <- getLine
  x <- fmap (map ((\[a,b] ->(a,b)) . take 2 . map read . words) . lines) getContents
  print $ rush x

rush :: [(Int,Int)] -> Int
rush xs
  | odd (ls + rs) = -1
  | even rs = 0
  | otherwise = if exists xs then 1 else -1
  where (l, r) = unzip xs
        ls = sum l
        rs = sum r
        exists [] = False
        exists ((x,y):r)
          | (even x && odd y) || (odd x && even y) = True
          | otherwise = exists r

import Data.Functor
import Control.Monad

main = do
     n <- read <$> getLine :: IO Int
     ls <- replicateM n ((map read) <$> (words <$> getLine)) :: IO [[Int]]
     putStrLn (show (solve n ls))
     return ()

solve n ds
      | c1 = 0
      | c2 = 1
      | otherwise = -1
      where c1 = (even (sum (map fst ds'))) && (even (sum (map snd ds')))
            c2 = any (\(x,y) -> odd (x+y)) ds' && 1<n
            ds' = map (\(x:y:_) -> (x,y)) ds

import Data.Functor
import Control.Monad

main = do
     n <- read <$> getLine :: IO Int
     ls <- replicateM n ((map read) <$> (words <$> getLine)) :: IO [[Int]]
     putStrLn (show (solve ls))
     return ()

solve ds
      | c1 = 0
      | c2 = 1
      | otherwise = -1
      where c1 = (even (sum (map fst ds'))) && (even (sum (map snd ds')))
            c2 = any (\(x,y) -> odd (x+y)) ds'
            ds' = map (\(x:y:_) -> (x,y)) ds

import Data.List (group, transpose)

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

solve :: [[Int]] -> Int
solve xys | all even (map sum (transpose xys)) = 0
          | length (filter ((>1) . length . group . map even) xys ) > 1 = 1
          | otherwise = -1

main=interact$show.f.parse.map read.tail.words
parse (x:y:xys) = (x,y):parse xys
parse _ = []
f xys
  | even (sum xs) && even (sum ys) = 0
  | (_:_:_)<-filter (\(x,y)->odd(x+y)) xys = 1
  | otherwise = -1
   where
     (xs,ys) = unzip xys


Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k.

The only line of input contains integers n and k (1 ≤ k ≤ n ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Print the number that will stand at the position number k after Volodya's manipulations.

NoteIn the first sample Volodya's sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5.

import Control.Applicative

main = do
  [n,k] <- map read . words <$> getLine :: IO [Integer]
  let d = n `div` 2 + fromIntegral (fromEnum (rem n 2 == 1))
  print $ if k > d then 2*(k-d) else 2*(k-1)+1

{-# LANGUAGE PatternGuards #-}

import Control.Concurrent
import Control.Monad
import System.IO
import Text.Printf
import System.Environment

main = subMain

solve n k = if (k > midn) then 2*(k - midn) else 2*k-1 where
		midn = if (nm2 == 0) then n2 else n2 + 1
		nm2 = mod n 2
		n2 = div n 2

subMain = do
  numsStr <- getLine
  let [n, k] = map (read :: String -> Integer) $ words numsStr
  print $ solve n k

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

import Control.Monad
import Data.Array
import Data.Char
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
-- import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
getList :: Read a => IO [a]
getList = fmap (map (read . B.unpack) . B.words) B.getLine
getIntList :: IO [Int]
getIntList = fmap (unfoldr (B.readInt . B.dropWhile isSpace)) B.getLine

solve :: Int64 -> Int64 -> Int64
solve n k = if k <= m then 2 * k - 1 else 2 * (k - m)
            where m = (n + 1) `div` 2

main :: IO ()
main = do
        [n,k] <- getList
        print $ solve n k

import Control.Applicative
import Data.List
import Data.List.Split

calc n k | even n && k<=n2 = 2*k-1
         | even n = (k-n2)*2
         | odd n && k<=(n2+1) = 2*k-1
         | otherwise = (k-n2-1)*2
  where n2 = div n 2

main=do
      [n,k]<-   map read <$>words <$> getLine ::IO [Integer]
      print $ calc n k

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

solve :: [Integer] -> Integer
solve [n, k] | k <= (n + 1) `div` 2 = 2 * k - 1
             | otherwise = 2 * (k - (n + 1) `div` 2)

main = interact $ show . solve . map read . words
solve [n,k] = r + 1 + (1 - n `mod` 2) * q
  where (q,r) = (2 * (k-1)) `divMod` n

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

glwr = fmap (map read . words) getLine

main = do
  hSetBuffering stdout NoBuffering
  solve

solve :: IO ()
solve = do
  n:k:_ <- glwr

  print $ evenOrOdd n k

evenOrOdd :: Integer -> Integer -> Integer
evenOrOdd n k
  | k <= odds = k*2 - 1
  | 0 < 1 = (k - odds) * 2
  where
    odds = div n 2 + sum [ 1 | odd n ]
    evens = n - odds

nthNumber n k
  | oddsCount >= k = 2 * k - 1
  | otherwise = (k - oddsCount) * 2
  where
    oddsCount = (n + 1) `div` 2

main = do
  input <- getLine
  let [n, k] = map (read :: String -> Integer) (words input) in print (nthNumber n k)

import Control.Applicative
main = map read . words <$> getLine >>= print . solve
solve :: [Integer] -> Integer
solve [n, k] | even n = solveEven n k
             | odd n = solveOdd n k
solveEven :: Integer -> Integer -> Integer
solveEven n k | k <= n `div` 2 = k * 2 - 1
              | k > n `div` 2 = (k - n `div` 2) * 2
solveOdd :: Integer -> Integer -> Integer
solveOdd n k | k <= n `div` 2 + 1 = k * 2 - 1
             | k > n `div` 2 + 1 = (k - (n`div` 2 + 1)) * 2

-- Vicfred
-- https://codeforces.com/problemset/problem/318/A

import Data.Int

solve :: [Int64] -> Int64
solve [n,k] = if k <= m then 2*k-1 else 2*(k-m)
              where m = (n+1) `div` 2

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

solve [a,b] | b <= div (a+1) 2 = 2 * b - 1
            | 0 < 1            = 2 * (b - div (a + 1) 2)
main = interact $ show . solve . map read . words

main=interact$show.f.map read.words
f :: [Integer] -> Integer
f[n,k]|k<=div(n+1)2=2*k-1|0<1=2*(k-div(n+1)2)

main = interact (show . answer . readInput)

readInput :: String -> [Integer]
readInput line = (map read) . words $ line

answer :: [Integer] -> Integer
answer [n, k] = if (k <= (n + 1) `div` 2) then k * 2 - 1 else (k - (n + 1) `div` 2) * 2

import Control.Applicative
import Data.List

main= do
	[n,k]<- map read. words <$> getLine ::IO [Integer]
	print $ if even n then if k*2 <=n then k*2-1 else (2*(k- (div n 2)) )else if k*2-1<=n then k*2-1 else (2*(k-(div (n+1) 2)))

main = do
  m <- getLine
  let [m1, m2] = (map read . words) m
  print $ makn m1 m2
makn :: Integer -> Integer -> Integer
--makn x y = head $ drop (y-1) (filter odd [1..x] ++ filter even [1..x])
makn x y
 | 2*y-1 <= x = 2*y-1
 | otherwise = 2*(y-(quot (x+1) 2))

solve [n, k] =
  (+ div (2*(k - 1)) n).(+1).(*2).mod (k - 1) $ div (n + 1) 2
main = getLine >>= print. solve. map read. words

main :: IO ()
main = do
    [n, k] <- map (read :: String -> Integer) . words <$> getLine
    print $ if 2 * k <= (n + 1)
        then 2 * (k - 1) + 1
        else 2 * (k - ((n + 1) `div` 2))

main :: IO()
main = interact mainFunction

mainFunction :: String -> String
mainFunction input = let [n, k] = map parseInteger . words $ input
                     in show $ solve n k

parseInteger :: String -> Integer
parseInteger = read

solve :: Integer -> Integer -> Integer
solve n k = let oddCount = (n + 1) `div` 2
            in if k <= oddCount
               then 2 * k - 1
               else 2 * (k - oddCount)

main :: IO ()
main = do
 line <- getLine
 putStrLn $ show $ solve line

lineToInts :: String -> [Integer]
lineToInts s = map (read :: String -> Integer) (words s)

solve :: String -> Integer
solve line =
 let nums = lineToInts line
     amount = head nums
     index = (last nums) - 1 in
 -- ([1,3..amount] ++ [2,4..amount]) !! (fromIntegral index)
 solve' amount index

solve' :: Integer -> Integer -> Integer
solve' amount index
 | index < odds = index * 2 + 1
 | otherwise = (index - odds + 1) * 2
 where odds = ceiling $ (fromIntegral amount) / 2

main = getLine >>= return . map read . words
               >>= putStrLn . show . solve

solve (a:b:_)
  | bound < b = (b - bound) * 2
  | otherwise = b * 2 - 1
    where dm    = a `divMod` 2
          bound = fst dm + snd dm

main = do
    input <- getLine
    let inp = (split ' ' input)
    let n = read (inp !! 0) :: Integer
    let k = read (inp !! 1) :: Integer
    let v = (n+1) `div` 2
    putStrLn (show (if k <= v then (k * 2 - 1) else ((k - v) * 2)))

split' :: (Eq a) => a -> [a] -> [a] -> [[a]]
split' _ [] l = [l]
split' v (x:xs) l
   | x /= v = split' v xs (l ++ [x])
   | otherwise = [l] ++ split' v xs []

split :: (Eq a) => a -> [a] -> [[a]]
split v xs = split' v xs []

process :: Integral a => a -> a -> a
process n k
    | k <= m = 2*k-1
    | otherwise = 2*(k-m)
    where m = n - n `div` 2

readInt :: String -> Integer
readInt = read

main = do
    [n, k] <- fmap (map readInt.words) getLine
    print $ process n k

import Control.Monad
import qualified Data.Map.Lazy as Map
import qualified Data.ByteString.Lazy as BS
import Data.List

main :: IO ()
main = do
  n:m:_ <- fmap (map read . words) getLine
  print $ rush n m

rush :: Integer -> Integer -> Integer
rush n m
  | n `mod` 2 == 0 && m * 2 <= n = 2 * m - 1
  | n `mod` 2 == 0 = (m - n `div` 2) * 2
  | n `mod` 2 == 1 && m * 2 - 1 <= n = 2 * m - 1
  | otherwise = (m - n `div` 2 - 1) * 2

-- Even and Odds
main :: IO ()
main = do
  [n, k] <- fmap (map read . words) getLine
  print $ sol n k

sol :: Integer -> Integer -> Integer
sol n k
  | k <= ceil = pred (k * 2)
  | otherwise = (* 2) (k - ceil)
  where
    ceil = div (succ n) 2

main = interact solve
solve str = let [n, k] = map (read::String -> Integer) . words $ str
            in findk n k

findk n k
    | k <= oddCounts = show $ 2*k - 1
    | otherwise = show $ (k-oddCounts) * 2
    where oddCounts = (n+1) `div` 2

import Control.Monad
import Control.Applicative

genAns n k
  | k <= ceiling (fromIntegral n / 2) = k * 2 - 1
  | otherwise = (k - (ceiling (fromIntegral n / 2) )) * 2

main = do
  [n,k] <- map read . words <$> getLine :: IO [Integer]
  print $ genAns n k

module Main (main)
    where

main :: IO ()
main = print . find . take 2 . map read . words =<< getLine
    where find [n,k]
            | k > lastOdd = 2*(k - lastOdd)
            | otherwise   = 2*k - 1
                where lastOdd = (n + n `mod` 2) `div` 2

main = do
	s<-getLine
	let [n,k]=map read $words s
	putStrLn $ show $ solve n k

solve n k | even n && k> div n 2 = 2*(k-div n 2 )
          | even n && k<= div n 2 = 1+2*(k-1)
          | odd n && k>div (n+1) 2=2*(k- div (n+1) 2)
          | odd n && k<=div (n+1) 2=1+2*(k-1)

findnum f s =
  if (odd f)
  then if (s > ((div f 2)+1))
       then (2*(s-((div f 2)+1)))
       else (2*s - 1)
  else if (s > (div f 2))
       then (2*(s-(div f 2)))
       else (2*s - 1)

main = do
  aa <- getLine
  let ff = (read (head (words aa)))::Integer
      ss = (read (last (words aa)))::Integer

  let ans = findnum ff ss
  putStrLn (show ans)

-- Snippet: readItems
readItems = do
    line <- getLine
    return $ map read $ words line

main = do
    [n, k] <- readItems
    let numOdd = div (n + 1) 2
    putStrLn $ show $ if k <= numOdd then k + k - 1 else (k - numOdd) * 2

main=interact$ (++"\n").show.(\[n,k] ->
  let numOdds=(n+1)`div`2 in if k<= numOdds then 2*k-1 else 2*(k- numOdds)
  ).map read.words

main :: IO ()
main = interact $ show . solve . map read . words

solve :: [Integer] -> Integer
solve [n, k]
  | k <= m = 2 * k - 1
  | otherwise = 2 * (k - m)
  where
  m = n `div` 2 + n `mod` 2

main = do
  [n, k] <- fmap (map read . words) getLine

  print $ if k <= (n+1) `div` 2 then 2*(k-1)+1 else 2*(k - (n+1)`div`2)

{-# LANGUAGE BlockArguments #-}

import Control.Arrow
import Data.List

main = do { nk <- getLine
          ; let [n,k] = words >>> map read $ nk
          ; putStrLn $ show (solve n k)
          }

solve :: Integer -> Integer -> Integer
solve n k = let m = (div n 2) + (mod n 2)
            in  if   k <= m
                then 2*k - 1
                else 2*(k-m)

main = do
    str <- getLine
    let xs = map read $ words str :: [Integer]
    let n = xs !! 0
    let k = xs !! 1
    let m = if (mod n 2 == 0) then (div n 2) else (div n 2 + 1)
    let ans = if (k > m) then ((k - m) * 2) else (k * 2 - 1)
    print ans

import Data.List
import Data.Maybe
main=   readsolveprint
readsolveprint::IO()
readsolveprint =  interact $ show.solve.map read.words.head . lines
solve :: [Integer]->Integer
solve [x,y] = let z= ceiling (fromIntegral x/ 2) in if y <=z then 2*y-1 else 2*(y - z)

import Control.Monad

main = do
   [n,k] <- liftM (map (\x -> read x::Integer) . words) getLine
   let midn = n`div`2 + (if odd n then 1 else 0)
       result = if k > midn then 2*(k-midn) else 2*(k-1)+1
   print result

import Control.Monad (liftM, replicateM)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

solve :: [Integer] -> Integer
solve [n, k]
  | k <= n `div` 2 + n `mod` 2  =  2 * k - 1
  | otherwise  =  2 * (k - n `div` 2 - n `mod` 2)

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

import Control.Applicative

main = do
    [a,b] <- map read.words <$> getLine :: IO [Integer]
    putStrLn.show $ if odd a then oddA a b else evenA a b

oddA a b = if b <= div (a+1) 2 then 2*b-1 else (b-div (a+1) 2)*2
evenA a b = if b <= div a 2 then 2*b-1 else (b-div a 2)*2

import Control.Applicative

main = do
    [n,i] <- map read.words <$> getLine :: IO [Integer]
    putStrLn.show $ index n i

index n i
    | i <= div (n+1) 2 = 2*i-1
    | otherwise = (i- div (n+1) 2) * 2

import Control.Applicative

main = do
    [a,b] <- map read.words <$> getLine :: IO [Int]
    putStrLn.show $ if odd a then oddA a b else evenA a b

oddA a b = if b <= div (a+1) 2 then [1,3..a] !! (b-1) else (b-div (a+1) 2)*2
evenA a b = if b <= div a 2 then 2*b-1 else (b-div a 2)*2

import Control.Applicative

main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  let d = n `div` 2 + fromEnum (rem n 2 == 1)
  print $ if k > d then 2*(k-d) else 2*(k-1)+1

{-# LANGUAGE PatternGuards #-}

import Control.Concurrent
import Control.Monad
import System.IO
import Text.Printf
import System.Environment

main = subMain

solve n k = if (k > midn) then 2*(k - midn) else 2*k-1 where
		midn = div n 2 + 1

subMain = do
  numsStr <- getLine
  let [n, k] = map (read :: String -> Integer) $ words numsStr
  print $ solve n k

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

{-# LANGUAGE PatternGuards #-}

import Control.Concurrent
import Control.Monad
import System.IO
import Text.Printf
import System.Environment

main = subMain

solve n k = if (k >= midn) then 2*(k - midn) else 2*k-1 where
		midn = if (nm2 == 0) then n2 else n2 + 1
		nm2 = mod n 2
		n2 = div n 2

subMain = do
  numsStr <- getLine
  let [n, k] = map (read :: String -> Integer) $ words numsStr
  print $ solve n k

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

{-# LANGUAGE PatternGuards #-}

import Control.Concurrent
import Control.Monad
import System.IO
import Text.Printf
import System.Environment

main = subMain

solve n k = if (k > midn) then 2*(k - midn) else 2*k-1 where
		midn = div n 2 + 1

subMain = do
  numsStr <- getLine
  let [n, k] = map (read :: String -> Int) $ words numsStr
  print $ solve n k

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

import Control.Applicative
import Data.List
import Data.List.Split

calc n k | even n && k<=n2 = 2*k-1
         | even n = (k-n2)*2
         | odd n && k<=(n2+1) = 2*k-1
         | otherwise = (k-n2-1)*2
  where n2 = div n 2

main=do
      [n,k]<-   map read <$>words <$> getLine ::IO [Int]
      print $ calc n k

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

glwr = fmap (map read . words) getLine

main = do
  hSetBuffering stdout NoBuffering
  solve

solve :: IO ()
solve = do
  n:k:_ <- glwr

  print $ evenOrOdd n k

evenOrOdd :: Int -> Int -> Int
evenOrOdd n k
  | k <= odds = k*2 - 1
  | 0 < 1 = (k - odds) * 2
  where
    odds = div n 2 + sum [ 1 | odd n ]
    evens = n - odds

nthNumber n k
  | oddsCount >= k = 2 * k - 1
  | otherwise = (k - oddsCount) * 2
  where
    oddsCount = (n + 1) `div` 2

main = do
  input <- getLine
  let [n, k] = map (read :: String -> Int) (words input) in print (nthNumber n k)

-- Vicfred
-- https://codeforces.com/problemset/problem/318/A

solve :: [Int] -> Int
solve [n,k] = if k <= m then 2*k-1 else 2*(k-m)
              where m = (n+1) `div` 2

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

solve [a,b] | a `div` 2 > b = 2*b - 1
            | a `div` 2 < b = 2 * (b - (a `div` 2) - 1)
main = interact $ show . solve . map read . words

import Control.Applicative
import Data.List

main= do
	[n,k]<- map read. words <$> getLine ::IO [Int]
	print $ if even n then if k*2 <=n then k*2-1 else (2*(k- (div n 2)) )else if k*2-1<=n then k*2-1 else (2*(k-(div (n+1) 2)))

main = do
  m <- getLine
  let [m1, m2] = (map read . words) m
  print $ makn m1 m2
makn :: Integer -> Integer -> Integer
--makn x y = head $ drop (y-1) (filter odd [1..x] ++ filter even [1..x])
makn x y
 | 2*y <x = 2*y-1
 | otherwise = 2*(y-(quot (x+1) 2))

main = do
  m <- getLine
  let [m1, m2] = (map read . words) m
  print $ makn m1 m2
makn :: Integer -> Integer -> Integer
--makn x y = head $ drop (y-1) (filter odd [1..x] ++ filter even [1..x])
makn x y
 | 2*y <x = 2*y-1
 | otherwise = 2*(y- 1-(quot x 2))

main = do
  m <- getLine
  let [m1, m2] = (map read . words) m
  print $ makn m1 m2
makn :: Integer -> Integer -> Integer
--makn x y = head $ drop (y-1) (filter odd [1..x] ++ filter even [1..x])
makn x y
 | 2*y <x = 2*y-1
 | otherwise = 2*y

main :: IO ()
main = do
    [n, k] <- map (read :: String -> Integer) . words <$> getLine
    print $ if 2 * k < n
        then 2 * (k - 1) + 1
        else 2 * (k - ((n + 1) `div` 2))

main :: IO()
main = interact mainFunction

mainFunction :: String -> String
mainFunction input = let [n, k] = map parseInt . words $ input
                     in show $ solve n k

parseInt :: String -> Int
parseInt = read

solve :: Int -> Int -> Int
solve n k = let oddCount = (n + 1) `div` 2
            in if k <= oddCount
               then 2 * k - 1
               else 2 * (k - oddCount)

main = do
    input <- getLine
    let inp = (split ' ' input)
    let n = read (inp !! 0) :: Int
    let k = read (inp !! 1) :: Int
    let v = (n+1) `div` 2
    putStrLn (show (if k <= v then (k * 2 - 1) else ((k - v) * 2)))

split' :: (Eq a) => a -> [a] -> [a] -> [[a]]
split' _ [] l = [l]
split' v (x:xs) l
   | x /= v = split' v xs (l ++ [x])
   | otherwise = [l] ++ split' v xs []

split :: (Eq a) => a -> [a] -> [[a]]
split v xs = split' v xs []

-- Even and Odds

main :: IO ()
main = do
        [n, k] <- fmap (map read . words) getLine
        print $ sol n k

sol :: Int -> Int -> Int
sol n k
  | 2 * k <= n = last . take k $ [1,3..]
  | otherwise = last . take (k `div` 2) $ [2,4..]

-- Even and Odds
main :: IO ()
main = do
  [n, k] <- fmap (map read . words) getLine
  print $ sol n k

sol :: Int -> Int -> Int
sol n k
  | k <= ceil = pred (k * 2)
  | otherwise = (* 2) (k - ceil)
  where
    ceil = div (succ n) 2

main = interact solve
solve str = let [n, k] = map (read::String -> Int) . words $ str
            in findk n k

findk n k
    | k <= oddCounts = show $ 2*k - 1
    | otherwise = show $ (k-oddCounts) * 2
    where oddCounts = (n+1) `div` 2

findnum f s =
  if (odd f)
  then if (s > ((div f 2)+1))
       then (2*(s-((div f 2)+1)))
       else (2*s - 1)
  else if (s > (div f 2))
       then (2*(s-(div f 2)))
       else (2*s - 1)

main = do
  aa <- getLine
  let ff = (read (head (words aa)))::Int
      ss = (read (last (words aa)))::Int

  let ans = findnum ff ss
  putStrLn (show ans)

{-# LANGUAGE BlockArguments #-}

import Control.Arrow
import Data.List

main = do { nk <- getLine
          ; let [n,k] = words >>> map read $ nk
          ; putStrLn $ show (solve n k)
          }

solve :: Int -> Int -> Int
solve n k = let m = (div n 2) + (mod n 2)
            in  if   k <= m
                then 2*k - 1
                else 2*(k-m)

import Data.List
import Data.Maybe
main=   readsolveprint
readsolveprint::IO()
readsolveprint =  interact $ show.solve.map read.words.head . lines
solve :: [Int]->Int
solve [x,y] = let z= ceiling (fromIntegral x/ 2) in if y <=z then 2*y-1 else 2*(y - z)

import Control.Monad

main = do
   [n,k] <- liftM (map (\x -> read x::Int) . words) getLine
   let midn = n`div`2
   print $ if k > midn then 2*(k-midn-1) else 2*(k-1)+1

import Control.Monad

main = do
   [n,k] <- liftM (map (\x -> read x::Int) . words) getLine
   let midn = n`div`2
   print $ if k > midn then 2*(k-midn-(if odd n then 1 else 0)) else 2*(k-1)+1

import Control.Applicative

main = do
    [n,i] <- map read.words <$> getLine :: IO [Int]
    putStrLn.show $ index n i

index n i
    | i < div (n+1) 2 = 2*i-1
    | otherwise = (i- div (n+1) 2) * 2

import Control.Applicative

main = do
    [a,b] <- map read.words <$> getLine :: IO [Int]
    putStrLn.show $ if odd a then oddA a b else evenA a b

oddA a b = if b <= div (a+1) 2 then 2*b-1 else (b-div (a+1) 2)*2
evenA a b = if b <= div a 2 then 2*b-1 else (b-div a 2)*2

import Control.Applicative

main = do
    [n,i] <- map read.words <$> getLine :: IO [Int]
    putStrLn.show $ index n i

index n i
    | i <= div (n+1) 2 = 2*i-1
    | otherwise = (i- div (n+1) 2) * 2


Some country is populated by wizards. They want to organize a demonstration.There are n people living in the city, x of them are the wizards who will surely go to the demonstration. Other city people (n - x people) do not support the wizards and aren't going to go to the demonstration. We know that the city administration will react only to the demonstration involving at least y percent of the city people. Having considered the matter, the wizards decided to create clone puppets which can substitute the city people on the demonstration. So all in all, the demonstration will involve only the wizards and their puppets. The city administration cannot tell the difference between a puppet and a person, so, as they calculate the percentage, the administration will consider the city to be consisting of only n people and not containing any clone puppets. Help the wizards and find the minimum number of clones to create to that the demonstration had no less than y percent of the city people.

The first line contains three space-separated integers, n, x, y (1 ≤ n, x, y ≤ 104, x ≤ n) — the number of citizens in the city, the number of wizards and the percentage the administration needs, correspondingly. Please note that y can exceed 100 percent, that is, the administration wants to see on a demonstration more people that actually live in the city ( > n).

Print a single integer — the answer to the problem, the minimum number of clones to create, so that the demonstration involved no less than y percent of n (the real total city population).

NoteIn the first sample it is necessary that at least 14% of 10 people came to the demonstration. As the number of people should be integer, then at least two people should come. There is only one wizard living in the city and he is going to come. That isn't enough, so he needs to create one clone. In the second sample 10 people should come to the demonstration. The city has 10 wizards. They will all come to the demonstration, so nobody has to create any clones.

getWords :: IO [String]
getWords = getLine >>= return . words

reads :: Read a => IO [a]
reads = getWords >>= return . (map read)

solve :: Int -> Int -> Int -> Int
solve n x y  =  max 0 ((n*y - 1) `div` 100 + 1 - x)

main :: IO ()
main  =  do
    [n, x, y] <- Main.reads
    print $ solve n x y

m :: Int -> Int -> Int -> Int
m y n x = y*n - x*100

ans :: Int -> Int
ans m | m > 0 = (m + 99) `div` 100
      | otherwise = 0

main = do
  [n, x, y] <- fmap ((map read).words) getLine
  print $ ans $ m y n x

main=interact$show.f.map read.words
f[n,x,y]=max 0$ -(-n*y)`div`100-x

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

solve :: [Integer] -> Integer
solve [ n, x, y ] = max 0 $ (n * y + 99) `div` 100 - x
solve _ = undefined

main = print . solve . map read . words =<< getLine
solve [n,x,y] = max 0 $ (y*n + 99) `div` 100 - x

main=interact$show.s.map read.words
s[n,x,y]=max 0$(-100*x+y*n+99)`div`100

import Control.Applicative
import Data.List

main= do
	[n,a,b]<- map read. words <$> getLine::IO [Double]
	print $  max 0 $ ceiling $ b*n/100.0 - a

import Control.Applicative
import Data.List

main= do
	[n,a,b]<- map read. words <$> getLine::IO [Double]
	print $  ceiling $ b*n/100.0 - a


Moamen and Ezzat are playing a game. They create an array $$$a$$$ of $$$n$$$ non-negative integers where every element is less than $$$2^k$$$.Moamen wins if $$$a_1 \,\&\, a_2 \,\&\, a_3 \,\&\, \ldots \,\&\, a_n \ge a_1 \oplus a_2 \oplus a_3 \oplus \ldots \oplus a_n$$$.Here $$$\&$$$ denotes the bitwise AND operation, and $$$\oplus$$$ denotes the bitwise XOR operation.Please calculate the number of winning for Moamen arrays $$$a$$$.As the result may be very large, print the value modulo $$$1\,000\,000\,007$$$ ($$$10^9 + 7$$$).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5$$$)— the number of test cases.  Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le n\le 2\cdot 10^5$$$, $$$0 \le k \le 2\cdot 10^5$$$).

For each test case, print a single value — the number of different arrays that Moamen wins with. Print the result modulo $$$1\,000\,000\,007$$$ ($$$10^9 + 7$$$).

{-# LANGUAGE Strict #-}
{-# LANGUAGE LambdaCase #-}
import Control.Applicative (liftA2)
import Control.Monad (replicateM)
import Control.Monad.State (State, evalState, get, gets, put)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromJust)
import Data.Int
import Debug.Trace (trace)

main :: IO ()
main = C.interact $ runScanner (C.unlines <$> numberOf (C.pack <$> testCase))

testCase :: Scanner String
testCase = show <$> (solve <$> int <*> int)

solve :: Int -> Int -> Int64
solve n k
  | odd n = (h + 1) ^% k
  | even n = (h - 1) ^% k +% gt
  where
    h = 2 ^% (n - 1)
    ls = take k $ iterate (*% (h - 1)) 1
    rs = reverse . take k $ iterate (*% (2 *% h)) 1
    gt = foldl (+%) 0 (zipWith (*%) ls rs)

modv = 10^9 + 7 :: Int64

u +% v = (u + v) `mod` modv
u *% v = (u * v) `mod` modv
u ^% n
  | u == 0 = 0
  | n == 0 = 1
  | odd n = u *% (u ^% (n - 1))
  | even n = let u' = u ^% (n `div` 2) in u' *% u'

infixl 6 +%
infixl 7 *%
infixl 8 ^%

debug s = trace (show s) s
debugV n s = trace (">> " ++ show n ++ " = " ++ show s) s

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

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

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

peek :: Scanner C.ByteString
peek = gets head

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

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

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

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

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

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

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

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

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

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

(><) = times

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

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

{-# LANGUAGE Strict #-}
{-# LANGUAGE LambdaCase #-}
import Control.Applicative (liftA2)
import Control.Monad (replicateM)
import Control.Monad.State (State, evalState, get, gets, put)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromJust)
import Data.Int
import Debug.Trace (trace)

main :: IO ()
main = C.interact $ runScanner (C.unlines <$> numberOf (C.pack <$> testCase))

testCase :: Scanner String
testCase = show <$> (solve <$> int <*> int)

solve :: Int -> Int -> Int64
solve n k
  | odd n = (h + 1) ^% k
  | even n = (h - 1) ^% k + gt
  where
    h = 2 ^% (n - 1)
    ls = take k $ iterate (*% (h - 1)) 1
    rs = reverse . take k $ iterate (*% (2 *% h)) 1
    gt = foldl (+%) 0 (zipWith (*%) ls rs)

modv = 10^9 + 7 :: Int64

u +% v = (u + v) `mod` modv
u *% v = (u * v) `mod` modv
u ^% n
  | u == 0 = 0
  | n == 0 = 1
  | odd n = u *% (u ^% (n - 1))
  | even n = let u' = u ^% (n `div` 2) in u' *% u'

infixl 6 +%
infixl 7 *%
infixl 8 ^%

debug s = trace (show s) s
debugV n s = trace (">> " ++ show n ++ " = " ++ show s) s

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

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

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

peek :: Scanner C.ByteString
peek = gets head

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

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

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

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

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

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

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

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

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

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

(><) = times

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

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


You have unlimited number of coins with values $$$1, 2, \ldots, n$$$. You want to select some set of coins having the total value of $$$S$$$. It is allowed to have multiple coins with the same value in the set. What is the minimum number of coins required to get sum $$$S$$$?

The only line of the input contains two integers $$$n$$$ and $$$S$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le S \le 10^9$$$)

Print exactly one integer — the minimum number of coins required to obtain sum $$$S$$$.

NoteIn the first example, some of the possible ways to get sum $$$11$$$ with $$$3$$$ coins are:   $$$(3, 4, 4)$$$ $$$(2, 4, 5)$$$ $$$(1, 5, 5)$$$ $$$(3, 3, 5)$$$ It is impossible to get sum $$$11$$$ with less than $$$3$$$ coins.In the second example, some of the possible ways to get sum $$$16$$$ with $$$3$$$ coins are:   $$$(5, 5, 6)$$$ $$$(4, 6, 6)$$$ It is impossible to get sum $$$16$$$ with less than $$$3$$$ coins.

import Data.List
import qualified Data.Map.Strict as Map

toint s = (read s) :: Integer

solve::String -> String
solve ss =
	let n:s:_ = map toint $ words ss in
	show(div (s+n-1) n)++"\n"

main = do
  interact $ solve

import Data.List

main = do
       e<-getLine
       let (n:s:[])=map read (words e)::[Integer]
       print $ s `div` n + if s `mod` n==0 then 0 else 1

import Prelude
import Control.Monad

str2Ints str = [read x::Int | x <- words str]
getLineN n = replicateM n getLine

main = do
    s <- getLine;
    let l = str2Ints(s);
    (putStrLn.show)(l!!1 `quot` l!!0 + (if l!!1 `mod` l!!0/=0 then 1 else 0));

main = interact $ show . solve . map read . words
solve [n,s] = s `div` n + signum (s `mod` n)

main = do
    contents <- getContents
    process (parse contents)

parse :: String -> Maybe (Integer, Integer)
parse s = two_first(words s)

process :: Maybe (Integer, Integer) -> IO ()
process (Just (n, s)) = print (solve n s)
process Nothing = putStrLn "Invalid input"

two_first (a:b:x) = Just (read a, read b)
two_first x = Nothing

solve :: Integer -> Integer -> Integer
solve n s = (quot (s - 1) n) + 1

main = do
    contents <- getContents
    print (solve (parse contents))

parse :: String -> (Integer, Integer)
parse s = two_first(words s)

two_first (a:b:x) = (read a, read b)
two_first x = (10, 100000)

solve (n, s) = (quot (s - 1) n) + 1

import Control.Monad

solve :: Int -> Int -> Int
solve n s = let (q, r) = s `quotRem` n in
  q + (if r /= 0 then 1 else 0)

main = do
  [n, s] <- fmap read <$> words <$> getLine :: IO [Int]
  print $ solve n s

import System.IO

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

main :: IO()
main = do
  [n, s] <- parseInt

  let sol = ceiling $ fromIntegral s  / fromIntegral n
  print sol

import Control.Applicative
import Control.Monad

main= do
		[n,m]<- map read <$> words <$> getLine ::IO [Int]
		print $ (div m n) + if (mod m n)>0 then 1 else 0

import Data.List (nub)

process :: Int -> Int -> Int
process n s = (s - 1) `div` n + 1

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    [n,s] <- fmap (map readInt.words) getLine
    print $ process n s

import Prelude
import Control.Monad

str2Ints str = [read x::Int | x <- words str]
getLineN n = replicateM n getLine

main = do
    s <- getLine;
    let l = str2Ints(s);
    (putStrLn.show)(l!!1 `quot` l!!0 + (if l!!1 `mod` l!!0==1 then 1 else 0));

import Control.Applicative
import Control.Monad

main= do
		[n,m]<- map read <$> words <$> getLine ::IO [Int]
		print $ (div m n) + if (mod m n)==1 then 1 else 0


Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m. What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?

The single line contains two space separated integers n, m (0 < n ≤ 10000, 1 < m ≤ 10).

Print a single integer — the minimal number of moves being a multiple of m. If there is no way he can climb satisfying condition print  - 1 instead.

NoteFor the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}.For the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5.

getAnswer :: Integer -> Integer -> Integer
getAnswer n m = quantify n 1 m

quantify :: Integer -> Integer -> Integer -> Integer
quantify limit current divisor = if current > limit
                                 then -1
                                 else if satisfy limit current divisor
                                 then current
                                 else quantify limit (current + 1) divisor

satisfy :: Integer -> Integer -> Integer -> Bool
satisfy limit steps divisor = if steps `mod` divisor == 0 && steps >= allowed
                              then True
                              else False
    where allowed = (limit+1) `div` 2

main :: IO ()
main = do
    numbers <- getLine
    let [n, m] = map read (words numbers)
    print (getAnswer n m)

import Data.List
main = interact $show.solve.map read.words

solve::[Int]->Int
solve (n:m:[]) = getRes $ find (comp m) [leftBound n..n]
             where leftBound n | even n = div n 2
                               | otherwise = div n 2 +1

comp m n | mod n m == 0  = True
         | otherwise = False

getRes::Maybe Int->Int
getRes (Just n) = n
getRes Nothing = -1

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

solve :: [Int] -> Int
solve [0, m] = 0
solve [n, m] | n < m     = -1
             | otherwise = (div ((div (n + 1) 2) + (m - 1)) m) * m

default ( Integer, Rational)

fib  = 0:1:zipWith (+) fib fib'
fib' = tail fib
sol n m = ((n+1) `div` 2 - 1) `div` m * m + m
solve [n,m] = if sol n m <= n then sol n m else -1

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

main = do
  [n, m] <- fmap (map read . words) getLine

  let ix = [i | i <- [1..10000], i `mod` m == 0, i <= n && n <= 2*i]

  print $ if null ix then -1 else head ix

module Main where

import Control.Applicative
import Control.Monad
import Data.Char           (toLower)
import Data.List           (intersperse)
import System.IO

solve :: Integral t => t -> t -> t
solve n m =
    if min_result > max_steps
        then -1
        else min_result
  where
    min_result = min_steps `cdiv` m * m
    max_steps = n
    min_steps = n `cdiv` 2
    cdiv a b = a `div` b + if a `mod` b > 0 then 1 else 0

main :: IO ()
main = do
    hSetBuffering stdin $ BlockBuffering Nothing
    hSetBuffering stdout $ BlockBuffering Nothing
    [n, m] <- map read . words <$> getLine
    print $ solve n m

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
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 = solve =<< (map (fst.fromJust.C.readInt).C.words<$>C.getLine)
--solve::Int->IO()
solve [x,y] = let m =x `div` 2+ x `mod` 2; b= snd $ break (\a-> a `mod` y ==0) [m..x] in print $ if b== [] then (-1) else head b

import Data.Char

h :: Int -> Int -> Int -> Int
h n m x
 | n-2*m >= 2*m = h (n-2*m) m (x+m)
 | n > 2*m = x + 2*m
 | n >= m = x+m
 | otherwise = -1

--main = do
-- line <- getLine
-- print (h line [])

main = do
 line <- getLine
 let a = (read (takeWhile (/= ' ') line) :: Int)
 let b = (read (drop 1 (dropWhile (/= ' ') line)) :: Int)
 print (h a b 0)

import Control.Applicative
import Data.List

process n m | mod nos m ==0 = nos
            | nos < m && n<m = -1
            | nos < m =m
            | otherwise = nos +m -(mod nos m)
   where nos = div (n+1) 2

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

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

solve :: [Int] -> Int
solve [n, m] | n < m = -1
             | otherwise = m * ((n `div'` 2) `div'` m)
        where div' a b = - (-a) `div` b

main = interact $ show . solve . map read . words
solve [n,m]
  | r > n     = -1
  | otherwise = r
  where b = (n+1) `div` 2 + m - 1
        r = b - b `mod` m

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray, runSTArray)
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, range, 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;_->error$"readInt error : bs = "++show bs;}
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 #-}
for :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
for !i0 p next f=go i0 where go !i=when(p i)$f i>>go(next i)
{-# INLINE for #-}
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,m] <- map read.words <$> getLine :: IO [Int]
  case [k|k<-[m,m+m..n],let a=2*k-n, let b=n-k, a>=0, b>=0] of
    [] -> print (-1)
    xs -> print $ head xs

#!/usr/bin/env runghc
main = getContents >>= mapM_ print . solve . lines
solve = map (f . map read . words)
f [n,m]
  | n==0 = 0
  | n<m = -1
  | otherwise = (((n+1)`div`2)+(m-1))`div`m*m

import Control.Applicative

main=do
	[n,m]<-map read.words <$> getLine::IO [Int]
	let ans = ( div n 2 )+ ( if odd n then 1 else 0)
	print $ if (m>n) then (-1) else (((div ans m)+(if mod ans m >0 then 1 else 0))*m)

main = do
  [n, m] <- getLine >>= return. map read. words :: IO [Int]
  let idx = [i | i <- [div (n + 1) 2 .. n], mod i m == 0]
  print $ if (not.null $ idx) then head idx else -1

main = interact $ show . f . map (read :: String -> Int) . words where
    f [n, m] = _f (div n 2) n m
    _f (-1) _ _ = -1
    _f x n m
        | mod (n - x) m == 0 = n - x
        | otherwise = _f (x - 1) n m

import Data.List

cal ls = if length res == 0 then "-1" else show $ minimum res
  where  xs = map read $ words $ head $ lines ls
         n = head xs
         m = head $ tail xs
         dm = divMod n 2
         min = (uncurry (+)) dm
         res = [i | i <- [min .. n], i `mod` m == 0]

main = interact cal

main :: IO ()
main = interact solve

minMoves :: Int -> Int -> Int
minMoves n m
    | moves <= n && n <= 2 * moves = moves
    | otherwise = -1
    where moves = m * (n `divUp` (2 * m))

divUp :: (Integral a) => a -> a -> a
divUp a b =  (a + b - 1) `div` b

solve :: String -> String
solve s = show $ minMoves n m
    where (n:m:_) = fmap read . words $ s

climb :: Int -> Int -> Int -> Int
climb _ 0 _ = -1
climb moves stairs m
    | even stairs && ((steps + moves) `mod` m == 0) = moves + steps
    | odd stairs && ((steps + moves + 1) `mod` m == 0) = moves + steps + 1
    | otherwise = climb (moves + 1) (stairs - 1) m
        where steps = quot stairs 2

main = do
    [stairs, m] <- fmap (map read . words) getLine
    print $ climb 0 stairs m

getAnswer :: Integer -> Integer -> Integer
getAnswer n m = quantify n 1 m

quantify :: Integer -> Integer -> Integer -> Integer
quantify limit current divisor = if current > limit
                                 then -1
                                 else if satisfy limit current divisor
                                 then current
                                 else quantify limit (current + 1) divisor

satisfy :: Integer -> Integer -> Integer -> Bool
satisfy limit steps divisor = if steps `mod` divisor == 0 && steps >= allowed
                              then True
                              else False
    where allowed = limit `div` 2

main :: IO ()
main = do
    numbers <- getLine
    let [n, m] = map read (words numbers)
    print (getAnswer n m)

import Data.List
main = interact $show.solve.map read.words

solve::[Int]->Int
solve (n:m:[]) = getRes $ find (comp m) [div n 2..n]

comp m n | mod n m == 0  = True
         | otherwise = False

getRes::Maybe Int->Int
getRes (Just n) = n
getRes Nothing = -1

import Data.Char

h :: Int -> Int -> Int -> Int
h n m x
 | n-2*m >= 2*m = h (n-2*m) m (x+m)
 | n >= 2*m = x + 2*m
 | n >= m = x+m
 | otherwise = -1

--main = do
-- line <- getLine
-- print (h line [])

main = do
 line <- getLine
 let a = (read (takeWhile (/= ' ') line) :: Int)
 let b = (read (drop 1 (dropWhile (/= ' ') line)) :: Int)
 print (h a b 0)

import Data.Char

h :: Int -> Int -> Int -> Int
h n m x
 | n-2*m >= m = h (n-2*m) m (x+m)
 | n >= m = x+m
 | otherwise = -1

--main = do
-- line <- getLine
-- print (h line [])

main = do
 line <- getLine
 let a = (read (takeWhile (/= ' ') line) :: Int)
 let b = (read (drop 1 (dropWhile (/= ' ') line)) :: Int)
 print (h a b 0)

import Control.Applicative
import Data.List

process n m | mod nos m ==0 = nos
            | nos < m = -1
            | otherwise = nos +m -(mod nos m)
   where nos = div (n+1) 2

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

#!/usr/bin/env runghc
main = getContents >>= mapM_ print . solve . lines
solve = map (f . map read . words)
f [n,m]
  | n<m = -1
  | otherwise = ((n`div`2)+(m-1))`div`m*m

#!/usr/bin/env runghc
main = getContents >>= mapM_ print . solve . lines
solve = map (f . map read . words)
f [n,m]
  | n==0 = 0
  | n<m = -1
  | otherwise = ((n`div`2)+(m-1))`div`m*m

import Control.Applicative

main=do
	[n,m]<-map read.words <$> getLine::IO [Int]
	let ans = ( div n 2 )+ ( if odd n then 1 else 0)
	print $ if (m>n) then (-1) else ((div ans m)+(if mod ans m >0 then 1 else 0)*m)

import Control.Applicative

main=do
	[n,m]<-map read.words <$> getLine::IO [Int]
	let ans = ( div n 2 )+ ( if odd n then 1 else 0)
	print $ if (m>n) then (-1) else (((div ans m)+1)*m)

main :: IO ()
main = interact solve

minMoves :: Int -> Int -> Int
minMoves n m
    | moves <= n && n < 2 * moves = moves
    | otherwise = -1
    where moves = m * (n `divUp` (2 * m))

divUp :: (Integral a) => a -> a -> a
divUp a b =  (a + b - 1) `div` b

solve :: String -> String
solve s = show $ minMoves n m
    where (n:m:_) = fmap read . words $ s

climb :: Int -> Int -> Int -> Int
climb _ 0 _ = -1
climb moves stairs m
    | even stairs && ((steps + moves) `mod` m == 0) = moves + steps
    | odd stairs && ((steps + moves) + 1 `mod` m == 0) = moves + steps + 1
    | otherwise = climb (moves + 1) (stairs - 1) m
        where steps = quot stairs 2

main = do
    [stairs, m] <- fmap (map read . words) getLine
    print $ climb 0 stairs m


Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken.CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large one — 7 chunks. Ivan wants to eat exactly x chunks. Now he wonders whether he can buy exactly this amount of chicken.Formally, Ivan wants to know if he can choose two non-negative integers a and b in such a way that a small portions and b large ones contain exactly x chunks.Help Ivan to answer this question for several values of x!

The first line contains one integer n (1 ≤ n ≤ 100) — the number of testcases. The i-th of the following n lines contains one integer xi (1 ≤ xi ≤ 100) — the number of chicken chunks Ivan wants to eat.

Print n lines, in i-th line output YES if Ivan can buy exactly xi chunks. Otherwise, print NO.

NoteIn the first example Ivan can buy two small portions.In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much.

module Main where
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Monad.Trans.State.Lazy
import Data.Bool

main = B.interact exec
exec = B.pack . evalState app . B.words
app = (unlines . map (bool "NO" "YES" . solve)) `fmap` (flip replicateM poi =<< poi)
  where
  pop = state $ \(x:xs) -> (x, xs)
  poi = fmap int pop
  int = maybe undefined fst . B.readInt

solve x = or [3*a + 7*b == x| a <- [0..100], b <- [0..100]]

main :: IO ()
main =
    interact
    $ unlines
    . map
        ((["NO", "YES"] !!)
        . fromEnum
        . (`elem` [3 * a + 7 * b | a <- [0 .. 100], b <- [0 .. 100]])
        . read
        )
    . tail
    . words

main = getLine >> interact (unlines . solve . map read . lines)

solve [] = []
solve (n:ns)
    | filter (\t -> (n - 3*t) `mod` 7 == 0) [0 .. (n `div` 3)] == [] = "NO":(solve ns)
    | otherwise = "YES":(solve ns)

g=1000000007::Integer

f :: Int->String
f n
  |n<0="NO"
  |n `mod` 7==0="YES"
  |otherwise=f (n-3)

main = do
       a<-getLine
       b<-getContents
       let xs=map read (lines b)::[Int]
       mapM_ putStrLn $ map f xs

canBuy :: Int -> Bool
canBuy x | x < 0          = False
         | x == 0         = True
         | x `mod` 3 == 0 = True
         | x `mod` 7 == 0 = True
         | x < 7          = canBuy (x - 3)
         | otherwise      = canBuy (x - 7)

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

main :: IO ()
main = do
    cases <- fmap (read :: String -> Int) getLine
    results <- sequence $ replicate cases getLine
    mapM_ putStrLn (map (showResult . canBuy . (read :: String -> Int)) results)
    return ()

main = interact $ unlines . map (solve . read) . tail . words
solve n | null [ () | i <- [0..33], j <- [0..14], 3*i+7*j == n ] = "NO"
        | otherwise = "YES"

groupOn :: Int -> [a] -> [[a]]
groupOn _ [] = []
groupOn n xs = let (sts, nds) = splitAt n xs in sts : groupOn n nds
-------------------------------------------------------------------------------

main = interact $ unlines . parse . lines

parse (_:ops') = map sol ops
  where
    ops = map (fn . words) ops'
    fn :: [String] -> Int
    fn [b] = read b

sol a = res
  where
    res = sol' a
    sol' a
      | a < 0 = "NO"
      | a `mod` 3 == 0 = "YES"
      | otherwise = sol' (a-7)

canBuy :: Int -> Bool
canBuy x = x `mod` 3 == 0 || x `mod` 7 == 0

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

main :: IO ()
main = do
    cases <- fmap (read :: String -> Int) getLine
    results <- sequence $ replicate cases getLine
    mapM_ putStrLn (map (showResult . canBuy . (read :: String -> Int)) results)
    return ()

canBuy :: Int -> Bool
canBuy x | x < 0        = False
         | x == 0       = True
         | x < 7        = canBuy (x - 3)
         | otherwise    = canBuy (x - 7)

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

main :: IO ()
main = do
    cases <- fmap (read :: String -> Int) getLine
    results <- sequence $ replicate cases getLine
    mapM_ putStrLn (map (showResult . canBuy . (read :: String -> Int)) results)
    return ()

main = interact $ unlines . map (show . solve . read) . tail . words
solve n | null [ () | i <- [0..33], j <- [0..14], 3*i+7*j == n ] = "NO"
        | otherwise = "YES"


A little girl loves problems on bitwise operations very much. Here's one of them.You are given two integers l and r. Let's consider the values of  for all pairs of integers a and b (l ≤ a ≤ b ≤ r). Your task is to find the maximum value among all considered ones.Expression  means applying bitwise excluding or operation to integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as "^", in Pascal — as "xor".

The single line contains space-separated integers l and r (1 ≤ l ≤ r ≤ 1018). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

In a single line print a single integer — the maximum value of  for all pairs of integers a, b (l ≤ a ≤ b ≤ r).

import Control.Applicative
import Data.Bits

gao :: Int -> (Integer, Integer) -> (Integer, Integer) -> Integer
gao p r1 r2
  | r1 == (0, mm) = mm
  | r2 == (0, mm) = mm
  | otherwise     = maximum $ [u1 `xor`  u2 `xor` gao (p - 1) w1 w2 |
      (u1, v1) <- f r1, (u2, v2) <- f r2, let w1 = g v1, let w2 = g v2]
  where
    mm = bit (p + 1) - 1
    b = bit p
    m = b - 1
    f (x, y) = filter (uncurry (<=) . snd) [
      (0, (x, min y m)), (b, (max x b, y))]
    g (x, y) = (x .&. m, y .&. m)

main :: IO ()
main = do
  [l, r] <- map read <$> words <$> getLine
  print $ gao 100 (l, r) (l, r)

import Data.Bits
import Data.Int
main :: IO()
main = do
    [b,a] <- fmap ((map read) . words) getLine :: IO [Int64]
    let l = reverse $ zip (bits a []) (bits b [0,0..])
        l' = dropWhile (\(x1,x2) -> x1==x2) l
        ans = bit $ length l' :: Int64
    print (ans-1)

bits 0 t = t
bits n t = n `rem` 2 : bits (n `quot` 2) t

import Control.Monad
import Data.Bits

readi :: [Char] -> Integer
readi x = read x :: Integer

func1 l r = foldl (max) 0 $ (foldl (++) []) $ [map (xor x) [l..r] | x <- [l..r]]

maxdig = 70

func2 l r = (foldl (max) 0 $ filter (\x -> (m .&. x) /= 0) (map (2^) [0..maxdig]))*2 - 1
	where m = xor l r

main = do
	[l, r] <- ((map readi) . words) `liftM` getLine
	putStr $ show $ max (func2 l r) 0

import Data.Bits
main=interact$show.f.map read.words
f :: [Integer] -> Integer
f[l,r]=last.(0:).takeWhile(<2*(l`xor`r)).map pred$iterate(2*)1

import Control.Monad
import Data.Bits

readi :: [Char] -> Integer
readi x = read x :: Integer

func1 l r = foldl (max) 0 $ (foldl (++) []) $ [map (xor x) [l..r] | x <- [l..r]]

maxdig = 70

func2 l r = (foldl (max) 0 $ filter (\x -> (m .&. x) /= 0) (map (2^) [0..maxdig]))*2 - 1
	where m = xor l r

main = do
	[l, r] <- ((map readi) . words) `liftM` getLine
	putStr $ show $ func2 l r