Problem:
Score : 700 points

Problem Statement

We have a sequence of N \times K integers: X=(X_0,X_1,\cdots,X_{N \times K-1}).
Its elements are represented by another sequence of N integers: A=(A_0,A_1,\cdots,A_{N-1}). For each pair i, j (0 \leq i \leq K-1,\ 0 \leq j \leq N-1), X_{i \times N + j}=A_j holds.

Snuke has an integer sequence s, which is initially empty.
For each i=0,1,2,\cdots,N \times K-1, in this order, he will perform the following operation:

If s does not contain X_i: add X_i to the end of s.

If s does contain X_i: repeatedly delete the element at the end of s until s no longer contains X_i. Note that, in this case, we do not add X_i to the end of s.

Find the elements of s after Snuke finished the operations.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq K \leq 10^{12}

1 \leq A_i \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 \cdots A_{N-1}

Output

Print the elements of s after Snuke finished the operations, in order from beginning to end, with spaces in between.

Sample Input 1

3 2
1 2 3

Sample Output 1

2 3

In this case, X=(1,2,3,1,2,3).
We will perform the operations as follows:

i=0: s does not contain 1, so we add 1 to the end of s, resulting in s=(1).

i=1: s does not contain 2, so we add 2 to the end of s, resulting in s=(1,2).

i=2: s does not contain 3, so we add 3 to the end of s, resulting in s=(1,2,3).

i=3: s does contain 1, so we repeatedly delete the element at the end of s as long as s contains 1, which causes the following changes to s: (1,2,3)→(1,2)→(1)→().

i=4: s does not contain 2, so we add 2 to the end of s, resulting in s=(2).

i=5: s does not contain 3, so we add 3 to the end of s, resulting in s=(2,3).

Sample Input 2

5 10
1 2 3 2 3

Sample Output 2

3

Sample Input 3

6 1000000000000
1 1 2 2 3 3

Sample Output 3

s may be empty in the end.

Sample Input 4

11 97
3 1 4 1 5 9 2 6 5 3 5

Sample Output 4

9 2 6

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash, UnboxedTuples,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns, PolyKinds,
  TypeFamilies, OverloadedStrings, FlexibleInstances, UndecidableInstances #-}
{-# OPTIONS_GHC -O2 #-}

import Prelude
import Data.Bits
import Data.List
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Int
import Data.Word
import Data.Char
import Data.Ratio
import Data.Function
import Data.STRef
import Data.IORef
import Data.Monoid
import Data.Functor
import System.IO
import Control.Arrow
import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.State.Strict
import Control.Monad.ST
import Control.Monad.ST.Lazy (strictToLazyST, lazyToStrictST)
import qualified Control.Monad.ST.Lazy as STL
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import Data.Coerce
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Builder as BSB
import Data.IntMap (IntMap)
import qualified Data.IntMap.Strict as IMS
import qualified Data.IntMap.Lazy as IML
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as VSM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Primitive.Mutable as VPM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import Debug.Trace
import GHC.Exts (build, Int(..), Int#,
                 (+#), (*#), (-#), (<#), (>=#), (==#), quotRemInt#,
                 remInt#, uncheckedIShiftRL#, andI#, orI#,
                isTrue#)

main :: IO ()
main = do
  [n,k] <- map readInt . words <$> getLine
  as <- VU.unfoldrN n (runStateT rInt) <$> BSL.getContents
  printVecInSpcSepLn $ query as k
  return ()

aMax = 2 * 10^5 + 2 :: Int

query :: VU.Vector Int -> Int -> VU.Vector Int
query as !k = simulate
  (VU.drop drops as VU.++ VU.concat (replicate repRest as))
  where (!droppedRep,!drops) = findLastJumped (jumps as) k
        repRest = k-1 - droppedRep

simulate :: VU.Vector Int -> VU.Vector Int
simulate xs = VU.create $ do
  ret <- VUM.new n
  aPos <- VUM.replicate aMax (-1 :: Int)
  len <- VU.foldM'
    (\ !i !x -> do
        !prevI <- VUM.read aPos x
        if prevI >= 0
          then do
            forM_ [prevI..i-1] $ \ !j -> do
              sj <- VUM.read ret j
              VUM.write aPos sj (-1)
            return prevI
          else do
            VUM.write aPos x i
            VUM.write ret i x
            return $! (i+1))
    0 xs
  return $! VUM.take len ret
  where
    n = VU.length xs

findLastJumped :: VU.Vector (Int,Int) -> Int -> (Int,Int)
findLastJumped vec k = VU.head lastJumpVec
  where
    n = VU.length vec
    powpow2s = pows vec k
    lastJumpVec = V.foldr' (\ !powpow2 !le -> let !mid = comp le powpow2
                                              in if fst (VU.head mid) < k
                                                 then mid else le)
                 (VU.generate n (0,)) powpow2s

jumps :: VU.Vector Int -> VU.Vector (Int,Int)
jumps vec = VU.create $ do
  res <- VUM.replicate n (-1,-1)
  aPos <- VUM.replicate aMax (-1)
  VU.forM_ (VU.reverse (VU.indexed vec)) $ \(!i,!a) -> do
    prevI <- VUM.read aPos a
    VUM.write aPos a i
    when (prevI >= 0) 
      $ VUM.write res i
        $! if prevI >= n-1 then (1,0) else (0,prevI+1)
  VU.forM_ (VU.reverse (VU.indexed vec)) $ \(!i,!a) -> do
    (x,y) <- VUM.read res i
    when (x < 0) $ do
      prevI <- VUM.read aPos a
      VUM.write res i
        $! if prevI >= n-1 then (2,0) else (1,prevI+1)
  return res
  where
    n = VU.length vec

pows :: VU.Vector (Int,Int) -> Int -> V.Vector (VU.Vector (Int,Int))
pows vec k = V.create $ do
  ret <- VM.new capacity
  let go !i !vec = do
        VM.write ret i $! vec
        if fst (VU.head vec) >= k
          then return $! (i+1)
          else go (i+1) (comp vec vec)
  !len <- go 0 vec
  return $ VM.take len ret
  where
    n = VU.length vec
    capacity = finiteBitSize (n*k) - countTrailingZeros (n*k) + 1

comp :: VU.Vector (Int,Int) -> VU.Vector (Int,Int) -> VU.Vector (Int,Int)
comp goSecond goFirst = (`VU.map` goFirst) $ \ (!cnt1,!a) ->
  let (!cnt2,!b) = goSecond VU.! a
      !cnt = cnt1+cnt2
  in (cnt,b) 

#define IL(f) {-# INLINE f #-}; f


class ShowAsBuilder a where
  showAsBuilder :: a -> BSB.Builder

#define OI(t,f) instance {-# OVERLAPPING #-} ShowAsBuilder t where \
  { {-# INLINE showAsBuilder #-}; showAsBuilder = f }
OI(Int,BSB.intDec)
OI(Int8,BSB.int8Dec)
OI(Int16,BSB.int16Dec)
OI(Int32,BSB.int32Dec)
OI(Int64,BSB.int64Dec)
OI(Word,BSB.wordDec)
OI(Word8,BSB.word8Dec)
OI(Word16,BSB.word16Dec)
OI(Word32,BSB.word32Dec)
OI(Word64,BSB.word64Dec)
OI(Integer,BSB.integerDec)
OI(Float,BSB.floatDec)
OI(Double,BSB.doubleDec)
#undef OI

instance {-# OVERLAPPABLE #-} (Show a) => ShowAsBuilder a where
  showAsBuilder = BSB.string8 . show

{-# INLINE putBuilder #-}
putBuilder = BSB.hPutBuilder stdout

{-# INLINE printVecInLines #-}
printVecInLines :: (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
printVecInLines = putBuilder . v2BLines

{-# INLINE printVecInSpcSepLn #-}
printVecInSpcSepLn :: (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
printVecInSpcSepLn = putBuilder . v2BSpcSepLn



{-# INLINE v2BSpcSepPostf #-}
v2BSpcSepPostf :: (VG.Vector v a, ShowAsBuilder a)
  => BS.ByteString -- ^ postfix
  -> v a -> BSB.Builder
v2BSpcSepPostf = (`v2BSpcSepPostfWith` showAsBuilder)

{-# INLINE v2BSpcSepPostfWith #-}
v2BSpcSepPostfWith :: (VG.Vector v a)
  => BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
v2BSpcSepPostfWith = vecToBuilder "" " "

{-# INLINE v2BSpcSepLn #-}
v2BSpcSepLn :: (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
v2BSpcSepLn = v2BSpcSepLnWith showAsBuilder

{-# INLINE v2BSpcSepLnWith #-}
v2BSpcSepLnWith :: (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
v2BSpcSepLnWith = v2BSpcSepPostfWith "\n"

{-# INLINE v2BSpcSep #-}
v2BSpcSep :: (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
v2BSpcSep = v2BSpcSepWith showAsBuilder

{-# INLINE v2BSpcSepWith #-}
v2BSpcSepWith :: (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
v2BSpcSepWith = v2BSpcSepPostfWith ""

{-# INLINE v2BConcat #-}
v2BConcat :: (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
v2BConcat = v2BConcatWith showAsBuilder

{-# INLINE v2BConcatWith #-}
v2BConcatWith :: (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
v2BConcatWith showFct
  = VG.foldr ((<>) . showFct) mempty

{-# INLINE v2BLines #-}
v2BLines :: (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
v2BLines = v2BLinesWith showAsBuilder

{-# INLINE v2BLinesWith #-}
v2BLinesWith :: (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
v2BLinesWith showFct
  = VG.foldr (\ a -> (showFct a <>) . (BSB.char7 '\n' <>)) mempty





{-# INLINE vecToBuilder_ #-}
vecToBuilder_ :: (VG.Vector v a)
  => BSB.Builder -- ^ prefix
  -> BSB.Builder -- ^ separator
  -> BSB.Builder -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder_ !prefix !separator !postfix showFct vec
  = prefix <> VG.foldr
    (\ a rest !prefx -> prefx <> (showFct a <> rest separator))
    (const postfix) vec mempty

{-# INLINE vecToBuilder #-}
vecToBuilder :: (VG.Vector v a)
  => BS.ByteString -- ^ prefix
  -> BS.ByteString -- ^ separator
  -> BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder !prefix !separator !postfix
  = vecToBuilder_ (BSB.byteString prefix)
                  (BSB.byteString separator)
                  (BSB.byteString postfix)

IL(evalVals) :: [a] -> [a]
evalVals xs = build $ \c n -> foldr (c $!) n xs
IL(forceVals) :: (NFData a) => [a] -> [a]
forceVals xs = build $ \c n -> foldr (c $!!) n xs

rInt :: StateT BSL.ByteString Maybe Int
IL(rInt) = StateT $ BSL.readInt . BSL.dropWhile (<'!')
rIntS :: StateT BS.ByteString Maybe Int
IL(rIntS) = StateT $ BS.readInt . BS.dropWhile (<'!')

unlessM, whenM :: (Monad m) => m Bool -> m () -> m ()
IL(whenM) = (. flip when) . (>>=)
IL(unlessM) = (. flip unless) . (>>=)

IL(wrA) = A.writeArray
IL(rdA) = A.readArray
IL(mdA) = \arr f !i -> do
  ai <- rdA arr i
  let fai = f ai 
  wrA arr i fai
  return (ai,fai)
{-# INLINE mdA' #-}
mdA' = \arr f !i -> do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
IL(swapA) = \arr !i !j -> do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai

#define D(f,r,d)\
  IL(f) :: Integral a=>a->d; f=fromIntegral;\
  IL(r) :: String->d; r=read
#define C(f,r,g,h,d) D(f,r,d);\
  g,h :: RealFrac a=>a->d; IL(g)=floor; IL(h)=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

#define N(f,g,h,a,m)\
  IL(f) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e);\
  f=A.newArray;\
  IL(g) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e);\
  g=A.newArray_;\
  IL(h) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> [e] -> m (a i e);\
  h=A.newListArray
#define C(a,m)
N(newIOA,newIOA_,newIOAL,IOArray,IO)
N(newSTA,newSTA_,newSTAL,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m), 
N(newIOUA,newIOUA_,newIOUAL,IOUArray,IO)
N(newSTUA,newSTUA_,newSTUAL,STUArray s,ST s)
#undef C
#undef N

#undef IL