Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Let next(x) be the minimum lucky number which is larger than or equals x. Petya is interested what is the value of the expression next(l) + next(l + 1) + ... + next(r - 1) + next(r). Help him solve this problem.

The single line contains two integers l and r (1 ≤ l ≤ r ≤ 109) — the left and right interval limits.

In the single line print the only number — the sum next(l) + next(l + 1) + ... + next(r - 1) + next(r). Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

NoteIn the first sample: next(2) + next(3) + next(4) + next(5) + next(6) + next(7) = 4 + 4 + 4 + 7 + 7 + 7 = 33In the second sample: next(7) = 7

import Data.List

luckyList = [4, 7] ++ concat [ [x*10+4, x*10+7] | x <- luckyList ]

solve :: [Integer] -> Integer
solve [l,r] = sum [ y * z | [x, y] <- luckyLinked, let z = max 0 $ min y r - max x (l-1) ]
   where luckyLinked = ([0,4]:) . map (take 2) . takeWhile ((<r) . head) $ tails luckyList

main = interact $(++"\n") .  show . solve . map read . words

-- vim: set expandtab:

{-
import HUnit

testInput :: Test
testInput  =  TestList "TestInput"
  [
    Test "#1" $ assertEq 33 $ solve 2 7,
    Test "#2" $ assertEq 7 $ solve 7 7
  ]

testNext :: Test
testNext  =  TestList "TestNext"
  [
    Test "#1" $ assertEq 4 $ next 0,
    Test "#2" $ assertEq 4 $ next 4,
    Test "#3" $ assertEq 7 $ next 5,
    Test "#4" $ assertEq 7 $ next 7,
    Test "#5" $ assertEq 44 $ next 8,
    Test "#6" $ assertEq 44 $ next 44,
    Test "#7" $ assertEq 47 $ next 45,
    Test "#8" $ assertEq 47 $ next 47,
    Test "#9" $ assertEq 74 $ next 48,
    Test "#10" $ assertEq 74 $ next 74,
    Test "#11" $ assertEq 77 $ next 75,
    Test "#12" $ assertEq 77 $ next 77,
    Test "#13" $ assertEq 444 $ next 78,
    Test "#14" $ assertEq 74744 $ next 74572,
    Test "#15" $ assertEq 77444 $ next 74872,
    Test "#16" $ assertEq 444444 $ next 79999,
    Test "#17" $ assertEq 4444 $ next 4099,
    Test "#18" $ assertEq 4744 $ next 4499,
    Test "#19" $ assertEq 44 $ next 15
  ]

testNextBig :: Test
testNextBig  =  Test "TestNextBig" $ assertEq 4444444444 $ next 777777778

testBig :: Test
testBig  =  Test "TestBig" $ assertEq 444444444 $ solve 380000000 380000000

testBigBig :: Test
testBigBig  =  Test "TestBigBig" $ assertEq 0 $ solve 1 (10^9)

testWA28 :: Test
testWA28  =  Test "Test WA #28" $ assertEq 606732 $ solve 369 852

testEqDifferentSolve :: Test
testEqDifferentSolve  =  Test "TestEqDifferentSolve" $
    assertEq [] $ [(i, j) | i <- [1..100], j <- [i..100], solve i j /= solve' i j]
  where
    solve' l r  =  sum [next i | i <- [l .. r]]

test :: IO ()
test  =  mapM_ run
  [
    testInput,
    testNext,
    testNextBig,
    testBig,
    testBigBig,
    testWA28,
    testEqDifferentSolve
  ]
-}
--------------------------------------------------------------------------------

next :: Integer -> Integer
next  =  read . next' [] . reverse . show
  where
    next' acc []  =  acc
    next' acc (c:s)
      | c <  '4'  =  next' ('4' : (map (const '4') acc)) s
      | c == '4'  =  next' ('4' : acc) s
      | c <  '7'  =  next' ('7' : (map (const '4') acc)) s
      | c == '7'  =  next' ('7' : acc) s
      | otherwise  =  nextAdd ('4' : (map (const '4') acc)) s
    nextAdd acc []  =  '4' : acc
    nextAdd acc (c:s)
      | c == '9'  =  nextAdd ('4' : acc) s
      | otherwise  =  next' acc (succ c : s)

solve :: Integer -> Integer -> Integer
solve l r  =  solve' l 0
  where
    solve' l acc
      | nextL >= r  =  acc + (r - l + 1) * nextL
      | otherwise   =  solve' (nextL + 1) (acc + (nextL - l + 1) * nextL)
      where
        nextL  =  next l

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

-- Codeforces 121A

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Foldable
import Data.Int
import Data.List
import Data.STRef

foreach :: Monad m => [a] -> b -> (b -> a -> m b) -> m b
foreach xs v f = foldM f v xs

-- range: [, ], left inclusive and right inclusive
qsort :: (MArray arr e m, Ord e) => arr Int e -> Int -> Int -> m ()
qsort = qsortImpl
  where
    qsortImpl arr l r = when (r > l) $ do
        let mid = l + (r - l) `div` 2
        nmid <- partition arr l r mid
        qsortImpl arr l (nmid - 1)
        qsortImpl arr (nmid + 1) r
    swap arr ia ib = do
        a <- readArray arr ia
        b <- readArray arr ib
        writeArray arr ia b
        writeArray arr ib a
    partition arr l r mid = do
        pivot <- readArray arr mid
        swap arr mid r
        slot <- foreach [l..r-1] l (\slot i -> do
            val <- readArray arr i
            if val < pivot
               then swap arr i slot >> return (slot+1)
               else return slot)
        swap arr slot r >> return slot

main :: IO ()
main = do

    let nlen = 2 + Data.List.sum (map (2 ^) [1..9])

    let arr = runSTUArray $ do
          arr <- newArray_ (0, nlen) :: ST s (STUArray s Int Int64)
          let go k top arr | k > 444444444 = return ()
                           | otherwise = do
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 4)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 4) top arr
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 7)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 7) top arr

          writeArray arr 0 0
          top <- newSTRef 1
          go 0 top arr
          z <- readSTRef top
          qsort arr 1 nlen
          return arr

    let sumAll x | x == 0 = 0
                 | otherwise = go 1
                 where go i | i > nlen = 0
                            | x >= a = a * (a - b) + go (i + 1)
                            | otherwise = a * (x - b)
                            where a = arr ! i
                                  b = arr ! (i - 1)

    [a, b] <- (map read . words) <$> getLine :: IO [Int64]
    print $ sumAll b - sumAll (a - 1)

{-# 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
    l <- readInteger
    r <- readInteger
    return (l, r)
  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

lucky digits = [sum subset * 3 + ones | subset <- subsequences masks]
  where
    ones = (10^digits-1) `div` 9 * 4
    masks = map (10^) [0..digits-1]

luckys = sort $ concat [lucky digits | digits <- [1..10]]

solve (l, r) = sum [ (min r b - max l (a + 1) + 1) * b
                   | (a, b) <- intervals
                   , l <= b && r > a
                   ]
  where
    intervals = zip (0:luckys) luckys

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

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

import Data.Char
import Debug.Trace

fours n = replicate n '4'

next n =
    if all (=='7') n then
        fours (length n + 1)
    else
        let rn = reverse n
            (a,(b:bs)) = span (=='7') rn
        in
          (reverse bs) ++ ('7':(fours $ length a))

minLucky n_ = let n = show n_
                  (nf,nb) = span (\c -> c=='4'||c=='7') n
              in
                if null nb then
                    n
                else
                    let a = (ord (head nb))-(ord '0') in
                    if a>7 then
                        (next nf)++(fours (length nb))
                    else if a>4 then
                             nf++('7':(fours (length nb - 1)))
                         else
                             nf++(fours (length nb))

solve l r = rec l 0
    where
      rec i sum | i>r = show sum
                | otherwise =
                    let minl = read (minLucky i)::Integer
                        fr_ = minl - i +1
                        fr = if (i+fr_)>r then (r-i+1) else fr_ in
                    rec (i+fr) (sum+minl*fr)

main = do lis <- getLine
          let (l:r:_) = words lis
          putStrLn $ solve (read l::Integer) (read r::Integer)

module Main (main) where

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

luckyNumberList upper = sort $ tail $ makeLucky 0
              where
                makeLucky now
                  | now <= upper = [fromIntegral now] ++ makeLucky (now*10+4) ++ makeLucky (now*10+7)
                  | otherwise = []

calc :: [Integer] -> Integer -> Integer
calc [x1,x2] n = (max 0 ((min x2 n)- x1)) * x2
calc (x1:x2:xs) n = (max 0 ((min x2 n)- x1)) * x2 + calc (x2:xs) n

--fromIntegral n
sub :: Integer -> Integer
sub 0 = 0
sub n = if length list == 0 then 0
        else
          fromIntegral $ calc list n
            where list = [0] ++ luckyNumberList (n*10)

solve :: [Integer] -> Integer
solve [l,r] = sub r - sub (l - 1)

--main = interact$show.sub.read.head.words
main = interact$show.solve.map read.take 2.words

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Array
import qualified Data.IntMap as I
import qualified Data.Map as M

lucky 1 = ["4","7"]
lucky n = [a:b|a<-"47",b<-lucky (n-1)]

luck::[Integer]
luck = map read $ concat [lucky i | i<-[1..]] ::[Integer]

process::Integer->Integer->Integer->[Integer]->Integer
process a b c (s:ss) | s<b = process (s+1) b (c+(s-a+1)*s) ss
	             |otherwise = c+ (b-a+1) *s
main=do
	[a,b]<-map read.words <$>getLine ::IO [Integer]
	print $ process a b 0 $ dropWhile (<a)  luck

import Data.List

strToInt :: String -> Integer
strToInt = read

-- get soreted list of lucky numbers.
luckyNumbers x = sort $ f 0 x
  where
    f b x
      | b*10+7 <= x = (b*10+4):(b*10+7):[] ++ f (b*10+4) x ++ f (b*10+7) x
      | b*10+4 <= x = (b*10+4):[]
      | otherwise = []

-- solve the problem
--- x is next(l)
solve lucky l r i = let x = lucky!!i in
  if x < r then (x-l+1)*x + solve lucky (x+1) r (i+1)
  else (r-l+1)*x

main = do
  input <- getLine
  let (l:r:_) = map strToInt $ words input
  let lucky = luckyNumbers (10*r)
  putStrLn $ case findIndex (>=l) lucky of
    Just(i) -> show $ solve lucky l r i
    Nothing -> ""

module Main where
import Data.Char

isLucky :: Integer -> Bool
isLucky x = let s = show x
            in not $ any (flip elem ['1', '2', '3', '5', '6', '8', '9', '0']) s

lst :: [Integer]
lst = let ar = ['0', '4', '7']
          f :: String -> Bool
          f = hasOnlyLeadingZeroes
       in [read x | a<-ar, b<-ar, c<-ar, d<-ar, e<-ar, ff<-ar,g<-ar, h<-ar, i<-ar, j<-ar,let x = a:b:c:d:e:ff:g:h:i:j:[], f x]

hasOnlyLeadingZeroes :: String-> Bool
hasOnlyLeadingZeroes s = let
                            (a, b) = foldl (\(b1, b0) a ->
                                case a of
                                  '0' -> if b1 && b0 then (b1, not b0) else (b1, b0)
                                  _ -> if b1 then (b1, b0) else (not b1, b0)
                                    ) (False, True) s
                          in a && b

-- isLucky' :: Integer -> Bool
-- isLucky' x = go (x `div` 10) (x `mod` 10)
--   where
--     go :: Integer -> Integer -> Bool
--     go 0 dig = True
--     go new dig = if dig == 7 || dig == 4
--                  then go (new `div` 10) (x `mod` 10)
--                  else False

main :: IO ()
main = do
  interact $ unlines . map solve . lines

solve :: String -> String
solve st = let
             (l:r:[]) = map read . words $ st :: [Integer]
             (snn, _) = func l r
           in
             if (l == r) then show $ nextl l
             else show snn

nextl :: Integer -> Integer
nextl l = let ll = head [x | x <- lst, x >=l]
          in ll

-- func :: Integer -> Integer -> (Integer, [Integer])
-- func l r = let ll = [x | x <- lst, x >= l && x <= r]
--                h = head ll
--                lsl = nextl r
--                (_, llln) = foldl (\(b, l) a -> (a, (replicate (fromInteger $ a - b) a) : l)) (h, []) (tail ll)
--                lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - last ll) lsl
--            in (sum $ lln,lln)

func :: Integer -> Integer -> (Integer, [Integer])
func l r = let ll = [x | x <- lst, x >= l && x <= r]
               h = head ll
               lsl = nextl r
               lsll = last ll
               (_, llln, sn) = foldl (\(b, l, s) a -> (a, ((replicate (fromInteger $ a - b) a) : l), (s + a * (a-b)))) (h,[],0) (tail ll)
               lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - lsll) lsl
               snn = sn + h * (h - l + 1) + lsl * (r - lsll)
           in
           if length ll == 0
           then ((r - l + 1) * nextl l, [])
           else (snn,lln)

-- Codeforces 122C

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Foldable
import Data.Int
import Data.List
import Data.STRef

foreach :: Monad m => [a] -> b -> (b -> a -> m b) -> m b
foreach xs v f = foldM f v xs

-- range: [, ], left inclusive and right inclusive
qsort :: (MArray arr e m, Ord e) => arr Int e -> Int -> Int -> m ()
qsort = qsortImpl
  where
    qsortImpl arr l r = when (r > l) $ do
        let mid = l + (r - l) `div` 2
        nmid <- partition arr l r mid
        qsortImpl arr l (nmid - 1)
        qsortImpl arr (nmid + 1) r
    swap arr ia ib = do
        a <- readArray arr ia
        b <- readArray arr ib
        writeArray arr ia b
        writeArray arr ib a
    partition arr l r mid = do
        pivot <- readArray arr mid
        swap arr mid r
        slot <- foreach [l..r-1] l (\slot i -> do
            val <- readArray arr i
            if val < pivot
               then swap arr i slot >> return (slot+1)
               else return slot)
        swap arr slot r >> return slot

main :: IO ()
main = do

    let nlen = 2 + Data.List.sum (map (2 ^) [1..9])

    let arr = runSTUArray $ do
          arr <- newArray_ (0, nlen) :: ST s (STUArray s Int Int64)
          let go k top arr | k > 444444444 = return ()
                           | otherwise = do
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 4)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 4) top arr
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 7)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 7) top arr

          writeArray arr 0 0
          top <- newSTRef 1
          go 0 top arr
          z <- readSTRef top
          qsort arr 1 nlen
          return arr

    let sumAll x | x == 0 = 0
                 | otherwise = go 1
                 where go i | i > nlen = 0
                            | x >= a = a * (a - b) + go (i + 1)
                            | otherwise = a * (x - b)
                            where a = arr ! i
                                  b = arr ! (i - 1)

    [a, b] <- (map read . words) <$> getLine :: IO [Int64]
    print $ sumAll b - sumAll (a - 1)

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

solve :: [Integer] -> Integer
solve [l, r] = sumLucky r - sumLucky (l - 1)
solve _ = undefined

lucky :: [Integer]
lucky = map read $ concat $ tail $ take 11 $ iterate (\xs -> map ('4':) xs ++ map ('7':) xs) [""]

sumLucky :: Integer -> Integer
sumLucky n = sum $ map snd $ scanl f (0, 0) lucky
  where f (x, _) z | z <= n = (z, (z - x) * z)
                   | x >= 0 = (-1, (n - x) * z)
                   | otherwise = (-1, 0)

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

solve :: [Integer] -> Integer
solve [l, r] = sumLucky r - sumLucky (l - 1)

lucky :: [Integer]
lucky = map read $ (id =<<) $ tail $ take 11 $ iterate (\xs -> map ('4':) xs ++ map ('7':) xs) [""]

sumLucky :: Integer -> Integer
sumLucky n = sum $ map snd $ scanl f (0, 0) lucky
  where f (x, _) z | z <= n = (z, (z - x) * z)
                   | x >= 0 = (-1, (n - x) * z)
                   | otherwise = (-1, 0)

import Control.Monad (replicateM)
luckys :: [Integer]
luckys = map read $ concatMap (flip replicateM ['4', '7']) [1..10]

solve' :: Integer -> Integer -> [Integer] -> Integer
solve' _ _ [] = 0
solve' x y (k:ks)
  | y < x = 0
  | x > k = solve' x y ks
  | otherwise = (min y k - x + 1) * k + solve' (k + 1) y ks
parse :: String -> Integer
parse s = case map read (words s) of [x, y] -> solve' x y luckys

main = putStrLn . show . parse =<< getLine

lucky = let ys@(y : _) = ["4","7"] : map (\x -> map ("4"++) x ++ map ("7"++) x )ys in map (\x -> map (\y -> (read y) :: Integer) x) ys

count x = length $ show x

solve l r = doIt l r enum 0
	where
		s = count l
		enum = concat $ (drop (s-1) lucky )
		doIt l r (x : xs) acc		|	x < l		=	doIt l r xs acc
						|	x > r		=	acc + (r - l + 1) * x
						|	otherwise	=	($!) (doIt (x+1) r xs) (acc + (x - l + 1) * x)

main :: IO ()

main = do
		ln <- getLine
		let (l : r : _) = map (\x -> (read x) :: Integer) $ words ln
		print $ solve l r
		return ()

{-
import HUnit

testInput :: Test
testInput  =  TestList "TestInput"
  [
    Test "#1" $ assertEq 33 $ solve 2 7,
    Test "#2" $ assertEq 7 $ solve 7 7
  ]

testNext :: Test
testNext  =  TestList "TestNext"
  [
    Test "#1" $ assertEq 4 $ next 0,
    Test "#2" $ assertEq 4 $ next 4,
    Test "#3" $ assertEq 7 $ next 5,
    Test "#4" $ assertEq 7 $ next 7,
    Test "#5" $ assertEq 44 $ next 8,
    Test "#6" $ assertEq 44 $ next 44,
    Test "#7" $ assertEq 47 $ next 45,
    Test "#8" $ assertEq 47 $ next 47,
    Test "#9" $ assertEq 74 $ next 48,
    Test "#10" $ assertEq 74 $ next 74,
    Test "#11" $ assertEq 77 $ next 75,
    Test "#12" $ assertEq 77 $ next 77,
    Test "#13" $ assertEq 444 $ next 78,
    Test "#14" $ assertEq 74744 $ next 74572,
    Test "#15" $ assertEq 77444 $ next 74872,
    Test "#16" $ assertEq 444444 $ next 79999,
    Test "#17" $ assertEq 4444 $ next 4099,
    Test "#18" $ assertEq 4744 $ next 4499
  ]

testNextBig :: Test
testNextBig  =  Test "TestNextBig" $ assertEq 4444444444 $ next 777777778

testBig :: Test
testBig  =  Test "TestBig" $ assertEq 444444444 $ solve 380000000 380000000

testBigBig :: Test
testBigBig  =  Test "TestBigBig" $ assertEq 0 $ solve 1 (10^9)

test :: IO ()
test  =  mapM_ run
  [
    testInput,
    testNext,
    testNextBig,
    testBig,
    testBigBig
  ]
-}
--------------------------------------------------------------------------------

next :: Integer -> Integer
next  =  read . next' [] . reverse . show
  where
    next' acc []  =  acc
    next' acc (c:s)
      | c <= '4'  =  next' ('4' : acc) s
      | c <  '7'  =  next' ('7' : (map (const '4') acc)) s
      | c == '7'  =  next' ('7' : acc) s
      | otherwise  =  nextAdd ('4' : (map (const '4') acc)) s
    nextAdd acc []  =  '4' : acc
    nextAdd acc (c:s)
      | c == '9'  =  nextAdd ('4' : acc) s
      | otherwise  =  next' acc (succ c : s)

solve :: Integer -> Integer -> Integer
solve l r  =  solve' l 0
  where
    solve' l acc
      | nextL >= r  =  acc + (r - l + 1) * nextL
      | otherwise   =  solve' (nextL + 1) (acc + (nextL - l + 1) * nextL)
      where
        nextL  =  next l

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

-- Codeforces 121A

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Foldable
import Data.Int
import Data.List
import Data.STRef

foreach :: Monad m => [a] -> b -> (b -> a -> m b) -> m b
foreach xs v f = foldM f v xs

-- range: [, ], left inclusive and right inclusive
qsort :: (MArray arr e m, Ord e) => arr Int e -> Int -> Int -> m ()
qsort = qsortImpl
  where
    qsortImpl arr l r = when (r > l) $ do
        let mid = l + (r - l) `div` 2
        nmid <- partition arr l r mid
        qsortImpl arr l (nmid - 1)
        qsortImpl arr (nmid + 1) r
    swap arr ia ib = do
        a <- readArray arr ia
        b <- readArray arr ib
        writeArray arr ia b
        writeArray arr ib a
    partition arr l r mid = do
        pivot <- readArray arr mid
        swap arr mid r
        slot <- foreach [l..r-1] l (\slot i -> do
            val <- readArray arr i
            if val < pivot
               then swap arr i slot >> return (slot+1)
               else return slot)
        swap arr slot r >> return slot

main :: IO ()
main = do

    let nlen = Data.List.sum $ map (2 ^) [1..9]

    let arr = runSTUArray $ do
          arr <- newArray_ (0, nlen) :: ST s (STUArray s Int Int64)
          let go k top arr | k > 100000000 = return ()
                           | otherwise = do
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 4)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 4) top arr
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 7)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 7) top arr

          writeArray arr 0 0
          top <- newSTRef 1
          go 0 top arr
          z <- readSTRef top
          qsort arr 1 nlen
          return arr

    let sumAll x | x == 0 = 0
                 | otherwise = go 1
                 where go i | i > nlen = 0
                            | x >= a = a * (a - b) + go (i + 1)
                            | otherwise = a * (x - b)
                            where a = arr ! i
                                  b = arr ! (i - 1)

    [a, b] <- (map read . words) <$> getLine :: IO [Int64]
    print $ sumAll b - sumAll (a - 1)

{-# 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
    n <- readInt
    s <- readString
    return (n, s)
  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 = BS.putStr =<< solve . evalState parseInput <$> BS.getContents

solve (n, s) = BS.unlines $ replicate n s

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

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

import Data.Char
import Debug.Trace

fours n = replicate n '4'

next n =
    if all (=='7') n then
        fours (length n + 1)
    else
        let rn = reverse n
            (a,(b:bs)) = span (=='7') rn
        in
          (reverse bs) ++ ('7':a)

minLucky n_ = let n = show n_
                  (nf,nb) = span (\c -> c=='4'||c=='7') n
              in
                if null nb then
                    n
                else
                    let a = (ord (head nb))-(ord '0') in
                    if a>7 then
                        (next nf)++(fours (length nb))
                    else if a>4 then
                             nf++('7':(fours (length nb - 1)))
                         else
                             nf++(fours (length nb))

solve l r = rec l 0
    where
      rec i sum | i>r = show sum
                | otherwise =
                    let minl = read (minLucky i)::Integer
                        fr_ = minl - i +1
                        fr = if (i+fr_)>r then (r-i+1) else fr_ in
                    rec (i+fr) (sum+minl*fr)

main = do lis <- getLine
          let (l:r:_) = words lis
          putStrLn $ solve (read l::Integer) (read r::Integer)

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Array
import qualified Data.IntMap as I
import qualified Data.Map as M

lucky 1 = ["4","7"]
lucky n = [a:b|a<-"47",b<-lucky (n-1)]

luck::[Int]
luck = map read $ concat [lucky i | i<-[1..]] ::[Int]

process a b c (s:ss) | s<b = process (s+1) b (c+(s-a+1)*s) ss
	             |otherwise = c+ (b-a+1) *s
main=do
	[a,b]<-map read.words <$>getLine ::IO [Int]
	print $ process a b 0 $ dropWhile (<a)  luck

module Main where
import Data.Char

isLucky :: Integer -> Bool
isLucky x = let s = show x
            in not $ any (flip elem ['1', '2', '3', '5', '6', '8', '9', '0']) s

lst :: [Integer]
lst = let ar = ['0', '4', '7']
          f :: String -> Bool
          f = hasOnlyLeadingZeroes
       in [read x | a<-ar, b<-ar, c<-ar, d<-ar, e<-ar, ff<-ar,g<-ar, h<-ar, i<-ar, j<-ar,let x = a:b:c:d:e:ff:g:h:i:j:[], f x]

hasOnlyLeadingZeroes :: String-> Bool
hasOnlyLeadingZeroes s = let
                            (a, b) = foldl (\(b1, b0) a ->
                                case a of
                                  '0' -> if b1 && b0 then (b1, not b0) else (b1, b0)
                                  _ -> if b1 then (b1, b0) else (not b1, b0)
                                    ) (False, True) s
                          in a && b

-- isLucky' :: Integer -> Bool
-- isLucky' x = go (x `div` 10) (x `mod` 10)
--   where
--     go :: Integer -> Integer -> Bool
--     go 0 dig = True
--     go new dig = if dig == 7 || dig == 4
--                  then go (new `div` 10) (x `mod` 10)
--                  else False

main :: IO ()
main = do
  interact $ unlines . map solve . lines

solve :: String -> String
solve st = let
             (l:r:[]) = map read . words $ st :: [Integer]
             (snn, _) = func l r
           in
             if (l == r) then show $ nextl l
             else show snn

nextl :: Integer -> Integer
nextl l = let ll = head [x | x <- lst, x >=l]
          in ll

-- func :: Integer -> Integer -> (Integer, [Integer])
-- func l r = let ll = [x | x <- lst, x >= l && x <= r]
--                h = head ll
--                lsl = nextl r
--                (_, llln) = foldl (\(b, l) a -> (a, (replicate (fromInteger $ a - b) a) : l)) (h, []) (tail ll)
--                lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - last ll) lsl
--            in (sum $ lln,lln)

func :: Integer -> Integer -> (Integer, [Integer])
func l r = let ll = [x | x <- lst, x >= l && x <= r]
               h = head ll
               lsl = nextl r
               lsll = last ll
               (_, llln, sn) = foldl (\(b, l, s) a -> (a, ((replicate (fromInteger $ a - b) a) : l), (s + a * (a-b)))) (h,[],0) (tail ll)
               lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - lsll) lsl
               snn = sn + h * (h - l + 1) + lsl * (r - lsll)
           in
           if length ll == 0
           then ((r - l) * nextl l, [])
           else (snn,lln)

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

solve :: [Integer] -> Integer
solve [l, r] = sumLucky r - sumLucky (l - 1)

lucky :: [Integer]
lucky = map read $ (id =<<) $ tail $ take 9 $ iterate (\xs -> map ('4':) xs ++ map ('7':) xs) [""]

sumLucky :: Integer -> Integer
sumLucky n = sum $ map snd $ scanl f (0, 0) lucky
  where f (x, _) z | z <= n = (z, (z - x) * z)
                   | x >= 0 = (-1, (n - x) * z)
                   | otherwise = (-1, 0)

import Control.Monad (replicateM)
luckys :: [Integer]
luckys = map read $ concatMap (flip replicateM ['4', '7']) [1..9]

solve' :: Integer -> Integer -> [Integer] -> Integer
solve' _ _ [] = 0
solve' x y (k:ks)
  | y < x = 0
  | x > k = solve' x y ks
  | otherwise = (min y k - x + 1) * k + solve' (k + 1) y ks
parse :: String -> Integer
parse s = case map read (words s) of [x, y] -> solve' x y luckys

main = putStrLn . show . parse =<< getLine

lucky = let ys@(y : _) = ["4","7"] : map (\x -> map ("4"++) x ++ map ("7"++) x )ys in map (\x -> map (\y -> (read y) :: Int) x) ys

count x = length $ show x

solve l r = doIt l r enum 0
	where
		s = count l
		enum = concat $ (drop (s-1) lucky )
		doIt l r (x : xs) acc		|	x < l		=	doIt l r xs acc
						|	x > r		=	acc + (r - l + 1) * x
						|	otherwise	=	($!) (doIt (x+1) r xs) (acc + (x - l + 1) * x)

main :: IO ()

main = do
		ln <- getLine
		let (l : r : _) = map (\x -> (read x) :: Int) $ words ln
		print $ solve l r
		return ()


Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it.But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you help me to find the maximum possible least common multiple of these three integers?

The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.

Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.

NoteThe least common multiple of some positive integers is the least positive integer which is multiple for each of them.The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended.For the last example, we can chose numbers 7, 6, 5 and the LCM of them is 7·6·5 = 210. It is the maximum value we can get.

main = do
  n <- fmap read getLine
  print $ maximum $ [foldl1 lcm [i, j, k] |
    m <- [max 1 (n - 10)],
    i <- [m .. n],
    j <- [m .. n],
    k <- [m .. n]]

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    let ans = max (max (div ((pr (pr n))*(pr n)*n) (gcd n (pr (pr n)))) (div ((pr (pr k))*(pr k)*k) (gcd k (pr (pr k))))) (div ((pr (pr k))*(pr n)*n) (gcd n (pr (pr k)))) where
        k = pr n
    print ans

main = do
    n <- readLn
    print $ solve n

solve n | n == 1 = 1
        | n == 2 = 2
        | odd n = n * (n-1) * (n-2)
        | otherwise = max ((n-1)*(n-2)*(n-3)) $ maximum $ do
            let a = n
            let b = n-1
            c <- [1..n-2]
            return $ a `lcm` b `lcm` c

import Data.List
import Monad
import Prelude hiding (Int)

type Int  =  Integer

isPrime :: Int -> Bool
isPrime n  =  all (\x -> mod n x /= 0) $ takeWhile (\x -> x * x <= n) primes

primes :: [Int]
primes  =  2 : [n | n <- [3,5..], isPrime n]

maxPrime :: Int -> Int
maxPrime n  =  last $ takeWhile (< n) primes

lcmS :: [Int] -> Int
lcmS  =  foldl' lcm 1

solve :: Int -> Int
solve n  =  maximum $
    [lcmS [a, b, c] | a <- [m..n], b <- [a..n], c <- [b..n]]
  where
    m  =  truncate ((fromIntegral (lcmS [n,n-1,n-2])) ** (1/3 :: Double)) - 1

solve2 :: Int -> Int
solve2 1  =  1
solve2 2  =  2
solve2 4  =  12
solve2 n
  | odd n   =  lcmS [n, n-1, n-2]
  | n `mod` 3 == 0  =  lcmS [n-1, n-2, n-3]
  | otherwise  =  lcmS [n, n-1, n-3]

main :: IO ()
main  =  do
    n  <-  readLn
    print $ solve2 n

main = readLn >>= print . maxlcm3
       where maxlcm3 n
       		  | n < 3 		   = n
			  | mod n 6 == 0   = lcm3l [n-1,n-2, n-3]
			  | even n         = lcm3l [n,  n-1, n-3]
			  | otherwise      = lcm3l [n,  n-1, n-2]
			  where lcm3l [a,b,c] = a `lcm` b `lcm` c

-- lcm :: [Int] -> Int
-- lcm xs = lcm' [(d, divisor_degree d x) | d <- divisors x, x <- xs]

-- divisor_degree :: Int -> Int -> Int
-- divisor_degree d n
--   | mod n d == 0 = 0
--   | otherwise = 1 + (divisor_degree d (div n d))

-- divisors :: Int -> [Int]
-- divisors n = [d| d <- [1..(floor $ sqrt n)], (mod n d) == 0]

-- prime_divisors :: Int -> [Int]
-- prime_divisors n = [p|p <- divisors n, divisors p == [1,p]]

-- prime_divisors :: Int -> [(Int, Int)]
-- num2primedivisordegs n = [(p, divisor_degree d n) | d <- prime_divisors n]

-- divdegpairs2lcm :: [(Int, Int)] -> [(Int, Int)] -> Int
-- divdegpairs2lcm [] zs = zs
-- divdegpairs2lcm zs [] = zs
-- divdegpairs2lcm x:xs y:ys = if

-- lcm' :: [(Int, Int)] ->
-- lcm' ((div,deg):xs) = [ (div, max (deg)) | x <- xs]
-- maxdegree (a, dega) (a, degb) = (a, max dega degb)

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

import Data.Word

main = do
    x <- getLine
    putStrLn $ show $ f (read x :: Word64)

f :: (Integral a) => a -> a
f x
    | x<3 = x
    | (mod x 2 == 0) && (mod x 3 == 0) = (x-1)*(x-2)*(x-3)
    | mod x 2 == 0 = x*(x-1)*(x-3)
    | otherwise = x*(x-1)*(x-2)

f:: Integer -> Integer
f n
	|(n `mod` 2 == 0) && (n `mod` 3 == 0) = (n-1)*(n-2)*(n-3)
	|n `mod` 2 == 0 = n*(n-1)*(n-3)
	|otherwise = n*(n-1)*(n-2)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ (f n)

prime :: (Integral a, Ord a) => a -> a -> a -> Bool
prime 1 cur comp = False
prime n cur comp
	|n `mod` 2 == 0 = False
	|cur >= comp = True
	|otherwise = if n `mod` cur == 0 then False else prime n (cur+2) comp

get :: Int -> Float -> [Integer]
get 3 cur = []
get x cur = if prime (truncate $ cur) 3 (truncate . sqrt $ cur) == True then (truncate cur):(get (x+1) (cur-1)) else get x (cur-1)

mx :: (Ord a) => [a] -> a
mx xs = foldl (\acc x -> max acc x) (head xs) xs

find :: [Integer] -> Integer
find xs = mx $ [x*(kk y z) `div` (gcd x (kk y z)) | x <- [last $ xs..(head $ xs)], y <- [x+1 .. (head xs)], z <- [y+1 .. head xs]]
					where kk y z = y*z `div` (gcd y z)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ find $ ((truncate n):(get 0 n))

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

solve :: Integer -> Integer
solve n = maximum [ a `lcm` b `lcm` c | a <- [n,n-1..p],b <- [a,a-1..p],c <- [b,b-1..p] ]
  where p = last (take 3 $ take 3 (filter isPrime [n,n-1..1]) ++ [1])

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

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || (n `mod` p /= 0 && r)) True primes

main=readLn>>=print.solve
solve :: Integer -> Integer
solve x = maximum [i`lcm`j`lcm`k|i<-[z..x],j<-[z..x],k<-[z..x]]
   where
     y = head $ filter ((<2).gcd x) [x,x-1..1]
     z = head $ filter ((<2).gcd (x*y)) [y,y-1..1]

solve l1
    | n == 1 = 1
    | otherwise = maximum [m*(m-1)*head [k | k <- reverse [1..m], gcd k (m*(m-1)) == 1] | m <- [(max 1 (n-7))..n]]
    where n = read l1 :: Integer
main = do
    l1 <- getLine
    putStrLn (show (solve l1))

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    print ((pr (pr n))*(pr n)*n)

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    let ans = max (div ((pr (pr n))*(pr n)*n) (gcd n (pr (pr n)))) (div ((pr (pr k))*(pr k)*k) (gcd k (pr (pr k)))) where
        k = pr n
    print ans

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    print (div ((pr (pr n))*(pr n)*n) (gcd n (pr (pr n))))

main = readLn >>= print . maxlcm3
       where maxlcm3 n
			  | mod n 6 == 0   = lcm3l [n-1,n-2, n-3]
			  | even n         = lcm3l [n,  n-1, n-3]
			  | otherwise      = lcm3l [n,  n-1, n-2]
			  where lcm3l [a,b,c] = a `lcm` b `lcm` c

-- lcm :: [Int] -> Int
-- lcm xs = lcm' [(d, divisor_degree d x) | d <- divisors x, x <- xs]

-- divisor_degree :: Int -> Int -> Int
-- divisor_degree d n
--   | mod n d == 0 = 0
--   | otherwise = 1 + (divisor_degree d (div n d))

-- divisors :: Int -> [Int]
-- divisors n = [d| d <- [1..(floor $ sqrt n)], (mod n d) == 0]

-- prime_divisors :: Int -> [Int]
-- prime_divisors n = [p|p <- divisors n, divisors p == [1,p]]

-- prime_divisors :: Int -> [(Int, Int)]
-- num2primedivisordegs n = [(p, divisor_degree d n) | d <- prime_divisors n]

-- divdegpairs2lcm :: [(Int, Int)] -> [(Int, Int)] -> Int
-- divdegpairs2lcm [] zs = zs
-- divdegpairs2lcm zs [] = zs
-- divdegpairs2lcm x:xs y:ys = if

-- lcm' :: [(Int, Int)] ->
-- lcm' ((div,deg):xs) = [ (div, max (deg)) | x <- xs]
-- maxdegree (a, dega) (a, degb) = (a, max dega degb)

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

main = do
    x <- getLine
    putStrLn $ show $ f (read x :: Integer)

f :: (Integral a) => a -> a
f x
    | x<3 = x
    | mod x 2 == 0 = x*(x-1)*(x-3)
    | otherwise = x*(x-1)*(x-2)

f:: Integer -> Integer
f n
	|n `mod` 2 == 1 = n*(n-1)*(n-2)
	|n `mod` 3 /= 0 = n*(n-1)*(n-3)
	|otherwise = n*(n-1)*(n-2) `div ` 2

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ (f n)

prime :: (Integral a, Ord a) => a -> a -> a -> Bool
prime 1 cur comp = False
prime n cur comp
	|cur >= comp = True
	|n `mod` 2 == 0 = False
	|otherwise = if n `mod` cur == 0 then False else prime n (cur+2) comp

get :: Int -> Float -> [Integer]
get 3 cur = []
get x cur = if prime (truncate $ cur) 3 (truncate . sqrt $ cur) == True then (truncate cur):(get (x+1) (cur-1)) else get x (cur-1)

mx :: (Ord a) => [a] -> a
mx xs = foldl (\acc x -> max acc x) (head xs) xs

find :: [Integer] -> Integer
find xs = mx $ [x*(kk y z) `div` (gcd x (kk y z)) | x <- [last $ xs..(head $ xs)], y <- [x+1 .. (head xs)], z <- [y+1 .. head xs]]
					where kk y z = y*z `div` (gcd y z)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ find $ ((truncate n):(get 0 n))

f:: Integer -> Integer
f n
	|n `mod` 2 == 1 = n*(n-1)*(n-2)
	|otherwise = n*(n-1)*(n-3)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ (f n)

main=readLn>>=print.solve
solve :: Integer -> Integer
solve x = x*y*z
   where
     y = head $ filter ((<2).gcd x) [x,x-1..1]
     z = head $ filter ((<2).gcd (x*y)) [y,y-1..1]

solve l1
    | n == 1 = 1
    | otherwise = n * (n - 1) * head [n-k | k <- [1..], gcd n (n-k) == 1, gcd (n-1) (n-k) == 1]
    where n = read l1 :: Integer
main = do
    l1 <- getLine
    putStrLn (show (solve l1))

solve l1
    | n == 1 = 1
    | otherwise = n*(n-1)*(maximum [k `div` (gcd k (n*(n-1))) | k <- [1..n]])
    where n = read l1 :: Integer
main = do
    l1 <- getLine
    putStrLn (show (solve l1))


Bob is decorating his kitchen, more precisely, the floor. He has found a prime candidate for the tiles he will use. They come in a simple form factor — a square tile that is diagonally split into white and black part as depicted in the figure below.  The dimension of this tile is perfect for this kitchen, as he will need exactly $$$w \times h$$$ tiles without any scraps. That is, the width of the kitchen is $$$w$$$ tiles, and the height is $$$h$$$ tiles. As each tile can be rotated in one of four ways, he still needs to decide on how exactly he will tile the floor. There is a single aesthetic criterion that he wants to fulfil: two adjacent tiles must not share a colour on the edge — i.e. one of the tiles must have a white colour on the shared border, and the second one must be black.    The picture on the left shows one valid tiling of a $$$3 \times 2$$$ kitchen. The picture on the right shows an invalid arrangement, as the bottom two tiles touch with their white parts. Find the number of possible tilings. As this number may be large, output its remainder when divided by $$$998244353$$$ (a prime number).

The only line contains two space separated integers $$$w$$$, $$$h$$$ ($$$1 \leq w,h \leq 1\,000$$$) — the width and height of the kitchen, measured in tiles.

Output a single integer $$$n$$$ — the remainder of the number of tilings when divided by $$$998244353$$$.

magic :: Integer
magic = 998244353

power :: Integer -> Integer -> Integer -> Integer
power base expo modulo
    | expo == 0 = 1
    | expo == 1 = base `mod` modulo
    | odd expo  = base * (power base (expo - 1) modulo) `mod` modulo
    | otherwise = let root = power base (expo `div` 2) modulo
                  in root * root `mod` modulo

main = do
    nums <- (map (\tok -> read tok :: Integer) . words) <$> getLine
    putStrLn . show $ power 2 (sum nums) magic

{-# OPTIONS_GHC -O2 #-}

import Data.Char (ord)

parseInt = toInteger . foldl (\acc c -> acc * 10 + ord c - 48) 0

parseInts = fmap parseInt . words

power :: Integer -> Integer
power n
  | n == 0 = 1
  | even n = (ret * ret) `mod` mm
  | otherwise = (ret * ret) `mod` mm * 2 `mod` mm
  where
    ret = power (n `div` 2)
    mm = 998244353

main = do
  [n, m] <- fmap parseInts getLine
  print (power (n + m))

magic :: Int
magic = 998244353

power :: Int -> Int -> Int -> Int
power base expo modulo
    | expo == 0 = 1
    | expo == 1 = base `mod` modulo
    | odd expo  = base * (power base (expo - 1) modulo) `mod` modulo
    | otherwise = let root = power base (expo `div` 2) modulo
                  in root * root `mod` modulo

main = do
    nums <- (map (\tok -> read tok :: Int) . words) <$> getLine
    putStrLn . show $ power 2 (sum nums) magic


Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland.Now he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working.It is known that the garland contains light bulbs of four colors: red, blue, yellow and green. The garland is made as follows: if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. For example, the garland can look like "RYBGRYBGRY", "YBGRYBGRYBG", "BGRYB", but can not look like "BGRYG", "YBGRYBYGR" or "BGYBGY". Letters denote colors: 'R' — red, 'B' — blue, 'Y' — yellow, 'G' — green.Using the information that for each color at least one light bulb still works count the number of dead light bulbs of each four colors.

The first and the only line contains the string s (4 ≤ |s| ≤ 100), which describes the garland, the i-th symbol of which describes the color of the i-th light bulb in the order from the beginning of garland:    'R' — the light bulb is red,  'B' — the light bulb is blue,  'Y' — the light bulb is yellow,  'G' — the light bulb is green,  '!' — the light bulb is dead.  The string s can not contain other symbols except those five which were described.  It is guaranteed that in the given string at least once there is each of four letters 'R', 'B', 'Y' and 'G'.  It is guaranteed that the string s is correct garland with some blown light bulbs, it means that for example the line "GRBY!!!B" can not be in the input data.

In the only line print four integers kr, kb, ky, kg — the number of dead light bulbs of red, blue, yellow and green colors accordingly.

NoteIn the first example there are no dead light bulbs.In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements.

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

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import qualified Data.ByteString.Char8 as B
import Text.Printf

readInt = ( readLn :: IO Int )
readInts = map ( read :: String -> Int ) . words <$> getLine

getList = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

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

main = do
	s <- getLine
	let
		t = cycle $ head $ filter ( and . zipWith match s . cycle ) $ permutations chars
		r = broke s t
		[ a, b, c, d ] = map ( \c -> length $ filter ( == c ) r ) chars
	printf "%d %d %d %d\n" a b c d

chars = "RBYG"

match c t
	| c == '!' = True
	| otherwise = c == t

broke [] _ = ""
broke ( a : s ) ( b : t )
	| a == '!' = b : broke s t
	| otherwise = broke s t

import Data.List
import Data.Ord
import Data.Char
fi = fromIntegral

decim [] = []
decim (x:xs) =x:decim (drop 3 xs)

calc seq = (color,num) where
	color = head $ filter (/='!') seq
	num = length $ filter (=='!') seq

solve seq = unwords $ map show [r,b,y,g] where
	all= map (\n->calc.decim.drop n $ seq) [0..3]
	r=snd.head.filter (\(c,n)->c=='R') $ all
	b=snd.head.filter (\(c,n)->c=='B') $ all
	y=snd.head.filter (\(c,n)->c=='Y') $ all
	g=snd.head.filter (\(c,n)->c=='G') $ all

main=do
	d<-getLine
	putStrLn $ solve d

import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.List (foldl')
import Data.Maybe (fromMaybe)

main = do
  bulbs <- getLine
  putStrLn $
    let onlyBulbs = filter (flip S.member (S.fromList "RGBY!")) bulbs
        positioned = zip [0..] onlyBulbs
        onlyValid = filter ((/=) '!' . snd) positioned
        zero = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 0) onlyValid
        one = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 1) onlyValid
        two = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 2) onlyValid
        three = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 3) onlyValid
        findColor pos =
          case pos `mod` 4 of
            0 -> zero
            1 -> one
            2 -> two
            3 -> three
        counted =
          foldl
            (\map (pos, char) ->
                if char == '!' then
                  M.insertWith (+) (findColor pos) 1 map
                else map)
            M.empty
            positioned
    in
      unwords . map show . map (fromMaybe 0 . flip M.lookup counted) $ ['R', 'B', 'Y', 'G']


Meg the Rabbit decided to do something nice, specifically — to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse — as a square of side n. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are: (0, 0), (n, 0), (0, n) and (n, n).

The single line contains 5 space-separated integers: n, x1, y1, x2, y2 (1 ≤ n ≤ 1000, 0 ≤ x1, y1, x2, y2 ≤ n) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square.

You must print on a single line the shortest distance between the points.

module Main where

import Control.Monad

main = do
    [n, x1, y1, x2, y2] <- ( map read . words ) `liftM` getLine
    let p1 = linePos x1 y1
        p2 = linePos x2 y2
        linePos x y = if y == 0
            then x
            else if y == n
                then 3 * n - x
                else if x == n
                    then n + y
                    else 4 * n - y
        d = min ( abs $ p1 - p2 ) ( 4 * n - ( abs $ p1 - p2 ) )
    putStrLn $ show d

dist :: Int -> (Int, Int) -> Int
dist _ (0, y) = y
dist n (x, 0) = 4 * n - x
dist n (x, y)
	| y == n = n + x
	| otherwise = 3 * n - y

min_dist :: Int -> Int -> Int
min_dist n d = min d (4 * n - d)

main = do
	n:x1:y1:x2:y2:_ <- getLine >>= return . map read . words :: IO [Int]
	putStrLn . show $ min_dist n $ abs $ (dist n (x1, y1)) - (dist n (x2, y2))

main = interact $ show . solve . (map (read :: String -> Int)) . words
solve [n,x1,y1,x2,y2] = min (w2 - w1) (4 * n + w1 - w2)
   where
     z1 = f x1 y1
     z2 = f x2 y2
     w1 = min z1 z2
     w2 = max z1 z2
     f x y
       | x == 0    = y
       | y == n    = n + x
       | x == n    = 3 * n - y
       | otherwise = 4 * n - x

solve [n, x1, y1, x2, y2]
    | (dir x1 y1) == (dir x2 y2) = abs ((dist x1 y1) - (dist x2 y2))
    | otherwise                  = min summa (abs $ 4 * n - summa)
    where
      summa = (dist x1 y1) + (dist x2 y2)
      dist a b = a + b
      dir a b = a > b

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

main = interact (show . solve . parse)


One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen with the same probability), after which the winner is the player, whose number was closer to c. The boys agreed that if m and a are located on the same distance from c, Misha wins.Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number n. You need to determine which value of a Andrew must choose, so that the probability of his victory is the highest possible.More formally, you need to find such integer a (1 ≤ a ≤ n), that the probability that  is maximal, where c is the equiprobably chosen integer from 1 to n (inclusive).

The first line contains two integers n and m (1 ≤ m ≤ n ≤ 109) — the range of numbers in the game, and the number selected by Misha respectively.

Print a single number — such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.

NoteIn the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of a the probability of winning is less.

main :: IO()
main = do
    [n,m] <- fmap (map (\x -> read x::Int) . words) getLine
    if n==1 then (print 1) else if m > (n `quot` 2) then print (m-1) else print (m+1)

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

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

fdiv = (/) `on` fromIntegral

main = do
    [n, m] <- readInts
    print $ if n `fdiv` 2 < fromIntegral m then max 1 (m - 1)
                                           else min n (m + 1)

-- Codeforces 570B

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

main = interact $ show . f . map read . words
f (1:m:k) = 1
f (n:1:k) = 2
f (n:m:k) = if n==m then n-1 else (if n-m-1>m-1-1 then m+1 else m-1)

import Control.Applicative
import Control.Monad

-----

--be carefull with the type inference

-----

---------
-- get space separated something

getListSp :: Read a => IO [a]
getListSp = (map read . words) <$> getLine

---------

---------
-- output [a] in space separated form

putListSp :: Show a => [a]  -> IO()
putListSp ls = putStrLn $ show $ unwords $ map show ls

---------

--------
-- read an integer N, get N lines, make a list consist of them

getListLn :: Read a => IO [a]
getListLn = do n <- readLn
               s <- replicateM n getLine
	       return (map read s)
--------

main = do [n, m] <- getListSp
          print $ solve n m

solve:: Int -> Int -> Int
solve 1 _ = 1
solve n m | (n-m) <= (m-1) = m-1
          | otherwise      = m+1

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

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

toTuple :: [a] -> (a, a)
toTuple xs = (head xs, xs !! 1)

main = interact (show . solve . map read . words)
solve [1,1] = 1
solve [n,m] | m <= n`div` 2 = m+1
            | otherwise = m-1

import System.IO
import qualified Data.List as L

read_ints :: String -> [Int]
read_ints x = map read (words x) :: [Int]

main = do
	ln <- getLine
	let [n,m] = read_ints ln
	putStr $ show $ if n == 1 then 1 else ( if m - 1 < n - m then m+1 else m-1 )

probability::Int->Int->Int->Double
probability n m a
  | m > a = fromIntegral((a + m) `div` 2) / fromIntegral(n)
  | m < a = 1.0 - fromIntegral((a + m ) `div` 2) / fromIntegral(n)
  | otherwise = 0.0

small_candidates n m = if m - 1 >= 1 then [(m-1,probability n m (m-1))] else []
large_candidates n m = if m + 1 <= n then [(m+1,probability n m (m+1))] else []
candidates n m = small_candidates n m ++ large_candidates n m ++ [(m,0)]

getMax [] = (0,-1);
getMax (x:xs)
  | snd x >= snd candidate = x
  | otherwise = candidate
  where
    candidate = getMax xs

ans::Int->Int->Int
ans n m = fst(getMax(candidates n m))

main = do
  input <- getLine
  let [n,m] = [read x::Int | x <- words input]
  print(ans n m)

-- Codeforces Round #316 (Div. 2) - Problem B: Simple Game (https://codeforces.com/problemset/problem/570/B)

-- Corner case: só há uma escolha possível
solve 1 _ = 1
-- A resposta deve ser um dentre os dois vizinhos à esquerda ou a direita de m,
-- o mais distante dos extremos dentre eles
solve n m | (n - m) > (m - 1) = m + 1
          | otherwise         = m - 1

main = do
    line <- getLine
    let [n, m] = map read $ words line :: [Int]
    print $ solve n m

main :: IO()
main = do
    [n,m] <- fmap (map (\x -> read x::Int) . words) getLine
    if m > (n `quot` 2)
        then print (m-1)
        else print (m+1)

-- Codeforces 570B

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

-- Codeforces 570B

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

-- Codeforces 570B

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

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

solve :: [Int] -> Int
solve [n, m] | m * 2 <= n = m + 1
             | otherwise = m - 1
solve _ = undefined

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

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

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

solve :: [Int] -> Int
solve [n, m] | m * 2 < n = m + 1
             | otherwise = m - 1
solve _ = undefined

main = interact (show . solve . map read . words)
solve [n,m] | m <= n`div` 2 = m+1
            | otherwise = m-1

main = interact (show . solve . map read . words)
solve [n,m] | m < n`div` 2 = m+1
            | otherwise = m-1

-- Codeforces Round #316 (Div. 2) - Problem B: Simple Game (https://codeforces.com/problemset/problem/570/B)

-- Corner case: só há uma escolha possível
solve 1 _ = 1
-- A resposta deve ser um dentre os dois vizinhos à esquerda ou a direita de m,
-- o mais distante dos extremos dentre eles
solve n m | (n - m - 1) > (m - 1) = m + 1
          | otherwise             = m - 1

main = do
    line <- getLine
    let [n, m] = map read $ words line :: [Int]
    print $ solve n m


Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop and a d2 meter long road between his house and the second shop. Also, there is a road of length d3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house.  Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled.

The first line of the input contains three integers d1, d2, d3 (1 ≤ d1, d2, d3 ≤ 108) — the lengths of the paths.    d1 is the length of the path connecting Patrick's house and the first shop;  d2 is the length of the path connecting Patrick's house and the second shop;  d3 is the length of the path connecting both shops.

Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house.

NoteThe first sample is shown on the picture in the problem statement. One of the optimal routes is: house  first shop  second shop  house.In the second sample one of the optimal routes is: house  first shop  house  second shop  house.

module Main where

main :: IO ()
main = do
	inp <- getLine
	let [d1, d2, d3] = readInts inp
	print $ optimize d1 d2 d3

readInts :: String -> [Int]
readInts = map read . words

optimize :: Int -> Int -> Int -> Int
optimize d1 d2 d3 = minimum [2*(d1+d2), 2*(d1+d3), 2*(d2+d3), d1+d2+d3]

import Data.List

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

solve :: [Int] -> Int
solve [a, b, c] = minimum [a + a + b + b, a + c + b, a + c + c + a, b + c + c + b]

max' [x] = x
max' (x:xs) = max x (max' xs)

main = do
  line <- getLine
  let [d1, d2, d3] = map read $ words line :: [Int]
  let d4 = min d1 (d2+d3)
  let d5 = min d2 (d1+d3)
  let d6 = min d3 (d1+d2)
  let ans = d4 + d5 + d6
  print ans

solve :: [Integer] -> Integer
solve [d1, d2, d3] = minimum [(d1 + d2 + d3), (2 * (d1 + d2)), (2 * (d1 + d3)), (2 * (d2 + d3))]

main = getContents >>= putStrLn . show . solve . map read . words

main = do
  [a, b, c] <- fmap (map read . words) getLine

  print $ minimum [2*a+2*b, a+b+c, 2*a+2*c, 2*b+2*c]

--fast read
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Control.Applicative
readInts a = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.words a
readLists = readInts $ B.getLine
readLLists = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.lines $ B.getContents
readMatrix = fmap readInts $ fmap B.lines $ B.getContents
main = do
  [d0,d1,d2] <- readLists
  print $ minimum [2 * (d0 + d1),d0 + d1 + d2,2*(d0+d2),2*(d1+d2)]

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 [x,y,z] = print $ minimum [2*(x+y), x+y+z, 2*(x+z), 2*(z+y)]

main=(print.fun.map read.words)=<<getLine where{fun[]=0;fun(a:b:c:xv)=f a b+f c(a + b)+f(g a b)(c+f a b) where{f c d|c>d=d;f c d = c;g c d|c<d=d;g c d = c;}}

countPath [d1, d2, d3] = [d1 + d2 + d3, 2*(d1 + d2), 2*(d1 + d3), 2*(d2 + d3)]

main :: IO ()
main = do
       ds <- getLine
       print $ minimum $ countPath $ map (\x -> read x::Int) $ words ds

import Control.Applicative
import Data.List

main = do
  [a,b,c]<- map read <$> words <$> getLine ::IO [Int]
  print $ minimum  [(a+b+c), (2*a+2*b), (2*a+2*c),(2*b+2*c)]

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

solve :: [Int] -> Int
solve [ d1, d2, d3 ] = minimum [ d1 + d2 + d3, 2 * (d1 + d2), 2 * (d2 + d3), 2 * (d3 + d1) ]
solve _ = undefined

main = interact (show . solve . map read . words)
solve [d1,d2,d3] = minimum [d1+d3+d2, 2*(d1+d2), 2*(d1+d3), 2*(d2+d3)]

module Main where
import Data.Functor

solve :: [Int] -> Int
solve [x,y,z] = minimum [2 * (x + z), 2*(y+z), 2 * (x + y), (x + y + z)]

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

shortestRoute :: Int -> Int -> Int -> Int
shortestRoute d1 d2 d3 =
  minimum [(2*d1 + 2*d2), (d1 + d2 + d3), 2*d1+2*d3, 2*d2+2*d3]

parseSpace :: String -> [Int]
parseSpace ws = map read (words ws) :: [Int]

main :: IO ()
main = do
  [d1, d2, d3] <- fmap parseSpace getLine
  print $ shortestRoute d1 d2 d3

main = interact $ show . solve . map read . words
solve [d1,d2,d3] = minimum [d1*2+d2*2, d1+d2+d3, d1*2+d3*2, d2*2+d3*2]

import Control.Applicative

main= do
	[d1,d2,d3]<- map read .words <$> getLine
	print $ minimum [ (2*d1+2*d2) ,(d1+d2+d3), 2*d2+2*d3, 2*d1+2*d3]

import Data.List (sort)

process:: [Int] -> Int
process ds = min (sum ds) (2 * (d1 + d2))
    where d1:d2:_ = sort ds

readInt :: String -> Int
readInt = read

main = do
    ds <- fmap (map readInt.words) getLine
    print $ process ds

max' [x] = x
max' (x:xs) = max x (max' xs)

main = do
  line <- getLine
  let [d1, d2, d3] = map read $ words line :: [Int]
  let d4 = min d1 (d2+d3)
  let d5 = min d2 (d1+d3)
  let d6 = min d3 (d1+d2)
  let ans = 2 * max' [d4, d5, d6]
  print ans

solve [d1, d2, d3] = min (d1 + d2 + d3) (2 * (d1 + d2))

main = getContents >>= putStrLn . show . solve . map read . words

main = do
  [a, b, c] <- fmap (map read . words) getLine

  print $ minimum [2*a+2*b, a+b+c]

--fast read
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Control.Applicative
readInts a = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.words a
readLists = readInts $ B.getLine
readLLists = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.lines $ B.getContents
readMatrix = fmap readInts $ fmap B.lines $ B.getContents
main = do
  [d0,d1,d2] <- readLists
  print $ if d0 + d1 < d2 then 2 * (d0 + d1) else d0 + d1 + d2

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 [x,y,z] = print $ minimum [2*(x+y), x+y+z]

import Control.Applicative
import Data.List

main = do
  [a,b,c]<- map read <$> words <$> getLine ::IO [Int]
  print $ min (a+b+c) (2*a+2*b)

shortestRoute :: Int -> Int -> Int -> Int
shortestRoute d1 d2 d3 = min (2*d1 + 2*d2) (d1 + d2 + d3)

parseSpace :: String -> [Int]
parseSpace ws = map read (words ws) :: [Int]

main :: IO ()
main = do
  [d1, d2, d3] <- fmap parseSpace getLine
  print $ shortestRoute d1 d2 d3

import Control.Applicative

main= do
	[d1,d2,d3]<- map read .words <$> getLine
	print $ min (2*d1+2*d2) (d1+d2+d3)

process:: [Int] -> Int
process [d1, d2, d3]
    | d1 + d2 < d3 = 2 * (d1 + d2)
    | d2 + d3 < d1 = 2 * (d2 + d3)
    | d3 + d1 < d1 = 2 * (d3 + d1)
    | otherwise = d1 + d2 + d3

readInt :: String -> Int
readInt = read

main = do
    ds <- fmap (map readInt.words) getLine
    print $ process ds