main :: IO () main = interact $ show . solve . map read . words solve :: [Int] -> Int solve (n:m:as) = foldl f 1 $ reverse as where f :: Int -> Int -> Int f acc n | acc == 1 = n | acc <= n = acc - 1 | otherwise = acc