import Control.Monad import Control.Monad.Fix import Control.Monad.ST import Control.Monad.State import Data.Bits import Data.Bool import qualified Data.ByteString as BS import qualified Data.ByteString.Builder as BSB import qualified Data.ByteString.Char8 as BSC8 import qualified Data.ByteString.Lazy.Char8 as BSLC8 import qualified Data.ByteString.Unsafe as BSU import Data.Char import Data.Coerce import qualified Data.Foldable as F import Data.IORef import Data.Int import qualified Data.List as L import Data.STRef import qualified Data.Vector as V import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Generic as VG import qualified Data.Vector.Generic.Mutable as VGM import qualified Data.Vector.Mutable as VM import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Unboxed.Mutable as VUM import Data.Word import qualified GHC.Integer.GMP.Internals as GMP import System.CPUTime import System.IO import Unsafe.Coerce main :: IO () main = do d <- readLn :: IO Int xs <- seqInput (d + 1) a <- VU.unsafeThaw xs forM_ [d - 3, d - 4 .. 0] $ \i -> do ai3 <- VUM.unsafeRead a (i + 3) VUM.unsafeModify a (+ ai3) (i + 1) VUM.unsafeWrite a (i + 3) 0 b <- VUM.unsafeNew 1 :: IO (VUM.IOVector Int) forM_ [0..d] $ \i -> do ai <- VUM.unsafeRead a i when (ai /= 0) $ VUM.unsafeWrite b 0 i n <- VUM.unsafeRead b 0 print n VU.unsafeFreeze a >>= putStrLn . L.unwords . L.map show . VU.toList . VU.take (n + 1) return () type Parser a = StateT BSC8.ByteString Maybe a runParser :: Parser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString) runParser = runStateT {-# INLINE runParser #-} int :: Parser Int int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace {-# INLINE int #-} seqInput :: Int -> IO (VU.Vector Int) seqInput n = VU.unfoldrN n (runParser int) <$> BSC8.getLine {-# INLINE seqInput #-}