cut :: Int -> Int -> [Int] -> [Int] cut p c xs = case splitAt (p-1) xs of (ps,ys) -> case splitAt c ys of (cs,zs) -> cs ++ ps ++ zs shuffle :: [(Int,Int)] -> [Int] -> [Int] shuffle xs cs = foldl (\ys (p,c) -> cut p c ys) cs xs run :: [(Int,Int)] -> [Int] run [(0,0)] = [] run ((n,r):xs) = case splitAt r xs of (ys,zs) -> head (shuffle ys (reverse [1..n])) : run zs parseLine :: String -> (Int,Int) parseLine s = case break (' '==) s of (a,b) -> (read a, read b) main :: IO () main = do s <- getContents mapM_ (putStrLn . show) . run . map parseLine . lines $ s