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



Sample input:
3 2
1 2 3


Sample output:
2 3

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())

a_list = list(map(int, input().split()))

def print_ans(s):
    if len(s)==0:
        print()
    else:
        ans = str(s[0])
        for val in s[1:]:
            ans = ans + " {}".format(val)
        print(ans)

s = []
count = 0
flag = True
done_list = [[]]
while(flag):
    count += 1
    for a in a_list:
        if a in s:
            s = s[0:s.index(a)]
        else:
            s.append(a)

    if count == k:
        pritn_ans(s)
        exit()

    if s in done_list:
        flag = False
    else:
        done_list.append(s.copy())


loop_num = len(done_list)
s = done_list[ k % loop_num ]
print_ans(s)

Submission status: Runtime Error

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
nxt = [0]*(2*n)
first = [-1]*n
prev = {}
for i in range(2*n):
    x = a[i%n]
    if x in prev:
        nxt[prev[x]] = i
    prev[x] = i

ddprint(nxt)
#print("hoge")
#exit()  #- no RE.  i.e. RE after here


loopfound = False
i=0
first[0] = 0
while True:
    i0 = i%n
    j = nxt[i0]+1
    nxtj = j + i - i0
    ddprint("  i {} j {} nxtj {} aj0 {}".format(i,j,nxtj,a[j%n]))
    if nxtj >= n*k:
        break
    j0 = j%n
    if first[j0] != -1:
        loopfound = True
        break
    first[j0] = nxtj
    i = nxtj


ddprint(first)
ddprint("nxtj {} j0 {} lpfd {}".format(nxtj,j0,loopfound))
print("bar")
exit()  # still RE.  i.e. RE before here

if loopfound:
    dif = nxtj - first[j0]
    if dif<=0:  # NTBR
        print("foo")
        exit()
    i = nxtj + ((n*k - nxtj) // dif) * dif
    ddprint("nxt {} j0 {} dif {} i {}".format(nxtj,j0,dif,i))
    while True:
        i0 = i%n
        j = nxt[i0] + 1
        nxtj = j+i-i0
        if nxtj >= n*k:
            break
        i = nxtj

ddprint(i)

done = [False] * 200001
stk = []
for j in range(i,n*k):
    ddprint(j)
    ddprint(stk)
    ddprint(done[:11])
    j0 = j%n
    if done[a[j0]]:
        ddprint(a[j0])
        m = len(stk)
        for k in range(m-1,-1,-1):
            x = stk[k]
            ddprint(x)
            stk.pop()
            done[x] = False
            if x==a[j0]:
                break
    else:
        stk.append(a[j0])
        done[a[j0]] = True

for i in stk:
    printn(str(i) + ' ')
print("")

Submission status: Runtime Error

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
nxt = [0]*(2*n)
first = [-1]*n
prev = {}
for i in range(2*n):
    x = a[i%n]
    if x in prev:
        nxt[prev[x]] = i
    prev[x] = i

ddprint(nxt)

loopfound = False
i=0
first[0] = 0
while True:
    i0 = i%n
    j = nxt[i0]+1
    nxtj = j + i - i0
    ddprint("  i {} j {} nxtj {} aj0 {}".format(i,j,nxtj,a[j%n]))
    if nxtj >= n*k:
        break
    j0 = j%n
    if first[j0] != -1:
        loopfound = True
        break
    first[j0] = nxtj
    i = nxtj

ddprint(first)
ddprint("nxtj {} j0 {} lpfd {}".format(nxtj,j0,loopfound))

if loopfound:
    dif = nxtj - first[j0]
    if dif<=0:  # NTBR
        print("foo")
        exit()
    i = nxtj + ((n*k - nxtj) // dif) * dif
    ddprint("nxt {} j0 {} dif {} i {}".format(nxtj,j0,dif,i))
    while True:
        i0 = i%n
        j = nxt[i0] + 1
        nxtj = j+i-i0
        if nxtj >= n*k:
            break
        i = nxtj

ddprint(i)

done = [False] * 200001
stk = []
for j in range(i,n*k):
    ddprint(j)
    ddprint(stk)
    ddprint(done[:11])
    j0 = j%n
    if done[a[j0]]:
        ddprint(a[j0])
        m = len(stk)
        for k in range(m-1,-1,-1):
            x = stk[k]
            ddprint(x)
            stk.pop()
            done[x] = False
            if x==a[j0]:
                break
    else:
        stk.append(a[j0])
        done[a[j0]] = True

for i in stk:
    printn(str(i) + ' ')
print("")

Submission status: Runtime Error

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
nxt = [0]*(2*n)
first = [-1]*n
prev = {}
for i in range(2*n):
    x = a[i%n]
    if x in prev:
        nxt[prev[x]] = i
    prev[x] = i

ddprint(nxt)
#print("hoge")
#exit()  #- no RE.  i.e. RE after here


loopfound = False
i=0
first[0] = 0
while True:
    i0 = i%n
    j = nxt[i0]+1
    nxtj = j + i - i0
    ddprint("  i {} j {} nxtj {} aj0 {}".format(i,j,nxtj,a[j%n]))
    if nxtj >= n*k:
        break
    j0 = j%n
    if first[j0] != -1:
        loopfound = True
        break
    first[j0] = nxtj
    i = nxtj

#print("bar")
#exit()  # still RE.  i.e. RE before here

ddprint(first)
ddprint("nxtj {} j0 {} lpfd {}".format(nxtj,j0,loopfound))

if loopfound:
    dif = nxtj - first[j0]
    if dif<=0:  # NTBR
        print("foo")
        exit()
    i = nxtj + ((n*k - nxtj) // dif) * dif
    ddprint("nxt {} j0 {} dif {} i {}".format(nxtj,j0,dif,i))
    while True:
        i0 = i%n
        j = nxt[i0] + 1
        nxtj = j+i-i0
        if nxtj >= n*k:
            break
        i = nxtj

ddprint(i)

done = [False] * 200001
stk = []
for j in range(i,n*k):
    ddprint(j)
    ddprint(stk)
    ddprint(done[:11])
    j0 = j%n
    if done[a[j0]]:
        ddprint(a[j0])
        m = len(stk)
        for k in range(m-1,-1,-1):
            x = stk[k]
            ddprint(x)
            stk.pop()
            done[x] = False
            if x==a[j0]:
                break
    else:
        stk.append(a[j0])
        done[a[j0]] = True

for i in stk:
    printn(str(i) + ' ')
print("")

Submission status: Runtime Error

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
nxt = [0]*(2*n)
first = [-1]*n
prev = {}
for i in range(2*n):
    x = a[i%n]
    if x in prev:
        nxt[prev[x]] = i
    prev[x] = i

ddprint(nxt)

loopfound = False
i=0
first[0] = 0
while True:
    i0 = i%n
    j = nxt[i0]+1
    nxtj = j + i - i0
    ddprint("  i {} j {} nxtj {} aj0 {}".format(i,j,nxtj,a[j%n]))
    if nxtj >= n*k:
        break
    j0 = j%n
    if first[j0] != -1:
        loopfound = True
        break
    first[j0] = nxtj
    i = nxtj

ddprint(first)
ddprint("nxtj {} j0 {} lpfd {}".format(nxtj,j0,loopfound))
print("bar")
exit()

if loopfound:
    dif = nxtj - first[j0]
    if dif<=0:  # NTBR
        print("foo")
        exit()
    i = nxtj + ((n*k - nxtj) // dif) * dif
    ddprint("nxt {} j0 {} dif {} i {}".format(nxtj,j0,dif,i))
    while True:
        i0 = i%n
        j = nxt[i0] + 1
        nxtj = j+i-i0
        if nxtj >= n*k:
            break
        i = nxtj

ddprint(i)

done = [False] * 200001
stk = []
for j in range(i,n*k):
    ddprint(j)
    ddprint(stk)
    ddprint(done[:11])
    j0 = j%n
    if done[a[j0]]:
        ddprint(a[j0])
        m = len(stk)
        for k in range(m-1,-1,-1):
            x = stk[k]
            ddprint(x)
            stk.pop()
            done[x] = False
            if x==a[j0]:
                break
    else:
        stk.append(a[j0])
        done[a[j0]] = True

for i in stk:
    printn(str(i) + ' ')
print("")

Submission status: Runtime Error

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
nxt = [0]*(2*n)
first = [-1]*n
prev = {}
for i in range(2*n):
    x = a[i%n]
    if x in prev:
        nxt[prev[x]] = i
    prev[x] = i

ddprint(nxt)

loopfound = False
i=0
first[0] = 0
while True:
    i0 = i%n
    j = nxt[i0]+1
    nxtj = j + i - i0
    ddprint("  i {} j {} nxtj {} aj0 {}".format(i,j,nxtj,a[j%n]))
    if nxtj >= n*k:
        break
    j0 = j%n
    if first[j0] != -1:
        loopfound = True
        break
    first[j0] = nxtj
    i = nxtj

ddprint(first)
ddprint("nxtj {} j0 {} lpfd {}".format(nxtj,j0,loopfound))

if loopfound:
    dif = nxtj - first[j0]
    i = nxtj + ((n*k - nxtj) // dif) * dif
    ddprint("nxt {} j0 {} dif {} i {}".format(nxtj,j0,dif,i))
    while True:
        i0 = i%n
        j = nxt[i0] + 1
        nxtj = j+i-i0
        if nxtj >= n*k:
            break
        i = nxtj

ddprint(i)

done = [False] * 200001
stk = []
for j in range(i,n*k):
    ddprint(j)
    ddprint(stk)
    ddprint(done[:11])
    j0 = j%n
    if done[a[j0]]:
        ddprint(a[j0])
        m = len(stk)
        for k in range(m-1,-1,-1):
            x = stk[k]
            ddprint(x)
            stk.pop()
            done[x] = False
            if x==a[j0]:
                break
    else:
        stk.append(a[j0])
        done[a[j0]] = True

for i in stk:
    printn(str(i) + ' ')
print("")

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

table = {}
init_table = {}
move = {}

for i in range(N):
    if A[i] not in table:
        table[A[i]] = i
        init_table[A[i]] = i
    else:        
        move[table[A[i]]], move[i] = i+1, init_table[A[i]]+1
        table[A[i]] = i

T = K
next_idx = 0
remain = [0]
for i in range(1, K+1):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    remain.append(idx)
    if idx in move:
        idx = move[idx]
    else:
        idx += 1
    if(idx == N and i != K):
        T = K % i
        break

if(T == 0):
    print()
    exit()
idx = remain[T]
ret = []
while idx < len(A):
    while idx in move and idx < move[idx]:
        idx = move[idx]
    if(idx < len(A)):
        ret.append(A[idx])
    idx += 1

print(*ret)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))*K
s = []
for X_i in X:
    if not X_i in s:
        s.append(X_i)
    else:
        while(X_i in s):
            del s[-1]

print(*s)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in a:
  if i in s:
    while i != s.pop(-1):
      break
  else :
    s.append(i)
print(" ".join(s))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in a:
  if i in s:
    while i ! s.pop(-1):
      break
  else :
    s.append(i)
print(s)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 23:25:47 2019

@author: Yamazaki Kenichi
"""
import bisect

N, K = map(int,input().split())
A = list(map(int,input().split()))

T = [[] for _ in range(2*10**5+1)]

for i in range(N):
    T[A[i]].append(i)

s = A[0]
pointa = 0
counta, res = 0, [-1]
while True:
    tmp = bisect.bisect_right(T[s], pointa) % len(T[s])
    p_pointa = pointa
    pointa = T[s][tmp] + 1
    if pointa == p_pointa + 1 or pointa < p_pointa:
        counta += 1
        res.append(p_pointa)
    elif pointa == N:
        counta += 1
        res.append(p_pointa)
        break
    s = A[pointa]

ans = []
if K % counta != 0:
    pointa = res[K % counta]
#    ans.append(A[pointa])
    while pointa < N:
        if pointa < T[A[pointa]][-1]:
            tmp = bisect.bisect_right(T[A[pointa]],pointa)
            pointa = T[A[pointa]][tmp] + 1
        else:
            ans.append(A[pointa])
            pointa += 1
#        print(ans)
print(' '.join(map(str,ans)))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 23:25:47 2019

@author: Yamazaki Kenichi
"""
import bisect

N, K = map(int,input().split())
A = list(map(int,input().split()))

T = [[] for _ in range(2*10**5+1)]
appearing = [0 for _ in range(2*10**5+1) ]

for i in range(N):
    c = A[i]
#    T[c][0] += 1
    T[c].append(i)

q = [A[0]]
pointa = 0
counta, res = 0, []
while q:
    s = q.pop()
    tmp = bisect.bisect_left(T[s], pointa) + 1
    tmp %= len(T[s])
    p_pointa = pointa
    pointa = T[s][tmp] + 1
#    print(s,tmp,pointa)
#    ans.append([s,tmp,pointa])
    if pointa == p_pointa + 1 or pointa < p_pointa:
        counta += 1
        res.append(p_pointa)
    if pointa == N:
        break
    next_s = A[pointa]
    q.append(next_s)

counta += 1
ans = []
if K % counta != 0:
    tmp = res[K % counta] + 1
    ans.append(A[tmp+1])
    for i in range(tmp + 1,N):
        if ans[0] == A[i]:
            ans = []
        else:
            ans.append(A[i])
        print(ans)
print(' '.join(map(str,ans)))

Submission status: Runtime Error

Solution code:
def step(s,i):
    if i in s:
        return s[:s.index(i):]
    else:
        return s+[i]

N,K=[int(i) for i in input().split(" ")]
A=[int(j) for j in input().split(" ")]

s=[]


for i in range(K):
    for a in A:
        s=step(s,a)
    if len(s)!=0:
        continue
    else:
        loop=i
        break

s=[]
for i in range(K%(loop+1)):
    for a in A:
        s=step(s, a)

print(" ".join([str(c) for c in s]))

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=list(map(int,input().split()))

M=N*K

logNK=math.floor(math.log2(N*K))
f=([[0]*N for i in range(logNK)])

#calc f0(n)
idx=[[] for i in range(200001)]
for i, a in enumerate(A):
    idx[a].append(i)

for i, a in enumerate(A):
    l=idx[a]
    n=l.index(i)
    if n+1 == len(l):
        j = N + l[0]
    else:
        j = l[n+1]
    f[0][i]=j-i+1

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=list(map(int,input().split()))

M=N*K

#logNK=math.floor(math.log2(N*K))
#f=([[0]*N for i in range(logNK)])

#calc f0(n)
idx=[[] for i in range(200001)]
for i, a in enumerate(A):
    idx[a].append(i)

for i, a in enumerate(A):
    l=idx[a]
    n=l.index(i)
    if n+1 == len(l):
        j = N + l[0]
    else:
        j = l[n+1]
    f[0][i]=j-i+1


for k in range(1,logNK):
    for i in range(N):
        f[k][i]=f[k-1][ i ] + f[ k-1 ][(i + f[k-1][i])%N ]

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=list(map(int,input().split()))

M=N*K

logNK=math.floor(math.log2(N*K))
f=([[0]*N for i in range(logNK)])

#calc f0(n)
idx=[[] for i in range(200001)]
for i, a in enumerate(A):
    idx[a].append(i)

for i, a in enumerate(A):
    l=idx[a]
    n=l.index(i)
    if n+1 == len(l):
        j = N + l[0]
    else:
        j = l[n+1]
    f[0][i]=j-i+1

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=list(map(int,input().split()))

M=N*K

A2=A*2

logNK=math.floor(math.log2(N*K))
f=([[0]*N for i in range(logNK)])

#calc f0(n)
tmp=0
for i in range(N):
    x=A[i]
    j=A2[i+1:].index(x)
    f[0][i]=j+2

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=list(map(int,input().split()))

M=N*K

A2=A*2

logNK=math.floor(math.log2(N*K))
f=([[0]*N for i in range(logNK)])

#calc f0(n)
for i in range(N):
    tmp=0
    x=A[i]
    for j in range(1,N+1):
        if A2[i+j]==x:
            f[0][i]=j+1
            break

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=list(map(int,input().split()))

M=N*K

A2=A*2

logNK=math.floor(math.log2(N*K))
f=([[0]*N for i in range(logNK)])

#calc f0(n)
for i in range(N):
    tmp=0
    x=A[i]
    for j in range(1,N+1):
        if A2[i+j]==x:
            f[0][i]=j+1
            break
#calc fk(n)
for k in range(1,logNK):
    for i in range(N):
        f[k][i]=f[ k-1 ][ i ] + f[ k-1 ][ (i + f[k-1][i])%N ]

Submission status: Runtime Error

Solution code:
import math

N,K=map(int,input().split())
A=[Ai for Ai in map(int,input().split())]

M=N*K

A2=A*2

logNK=math.floor(math.log2(N*K))
f=([[0]*N for i in range(logNK)])

#calc f0(n)
for i in range(N):
    tmp=0
    x=A[i]
    for j in range(1,N+1):
        if A2[i+j]==x:
            f[0][i]=j+1
            break

#calc fk(n)
for k in range(1,logNK):
    for i in range(N):
        f[k][i]=f[ k-1 ][ i ] + f[ k-1 ][ (i + f[k-1][i])%N ]

#step2
n=0
for k in range(logNK)[::-1]:
    i=n%N
    if n + f[k][i] < M:
        n+=f[k][i]

#step3
tmp=[]
for i in range(n,M):
    imodn=i%N
    if A[imodn] in tmp:
        tmp=tmp[0:tmp.index(A[imodn])]
    else:
        tmp.append(A[imodn])

print(' '.join(map(str,tmp)))

Submission status: Runtime Error

Solution code:
from collections import Counter

n, k = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]
OVER_RUN = n*k

cnt = [-1] * (2 * 10**5 + 10)
transition = [[-1] * n for _ in range(58)]
transition[0] = [0 for _ in range(1,n+1)]
for i, item in enumerate(a + a):
    if cnt[item] == -1:
        cnt[item] = i
        continue
    if cnt[item] < n:
        transition[1][cnt[item]] = i+1 - cnt[item]
    cnt[item] = i

for i in range(2,58):
    for j in range(n): 
        walk = transition[i-1][j] + transition[i-1][(transition[i-1][j] + j) % n]
        if walk > OVER_RUN:
            transition[i][j] = OVER_RUN
        else:
            transition[i][j] = walk

step = 0
step_mod = 0
while True:
    itr = 57 
    while transition[itr][step_mod] + step >= OVER_RUN:
        itr -= 1
    step += transition[itr][step_mod]
    step_mod = step % n
    if itr == 0:
        break

# Simulation
target = a[step_mod:]
ans = []
cnt = Counter()
for item in target:
    if cnt[item] > 0:
        while True:
            p = ans.pop()
            if p == item:
                break
    else:
        cnt[item] += 1
        ans.append(item)
print(" ".join([str(item) for item in ans]))

Submission status: Runtime Error

Solution code:
from collections import Counter

n, k = [int(item) for item in input().split()]
a = [int(item) for item in input().split()]
OVER_RUN = n*k

cnt = [-1] * (2 * 10**5 + 10)
transition = [[-1] * n for _ in range(64)]
transition[0] = [0 for _ in range(1,n+1)]
for i, item in enumerate(a + a):
    if cnt[item] == -1:
        cnt[item] = i
        continue
    if cnt[item] < n:
        transition[1][cnt[item]] = i+1 - cnt[item]
    cnt[item] = i

for i in range(2,64):
    for j in range(n): 
        walk = transition[i-1][j] + transition[i-1][(transition[i-1][j] + j) % n]
        if walk > OVER_RUN:
            transition[i][j] = OVER_RUN
        else:
            transition[i][j] = walk

step = 0
step_mod = 0
while True:
    itr = 63
    while transition[itr][step_mod] + step >= OVER_RUN:
        itr -= 1
    step += transition[itr][step_mod]
    step_mod = step % n
    if itr == 0:
        break

# Simulation
target = a[step_mod:]
ans = []
cnt = Counter()
for item in target:
    if cnt[item] > 0:
        while True:
            p = ans.pop()
            if p == item:
                break
    else:
        cnt[item] += 1
        ans.append(item)
print(" ".join([str(item) for item in ans]))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

last = dict()
v = [-1] * N

for i, a in enumerate(A + A) :
    if a in last and last[a] < N :
        v[last[a]] = i + 1
    
    last[a] = i

dist = [-1] * N
cur = 0
cycle = 0
while dist[cur] < 0 :
    dist[cur] = cycle 
    cycle += v[cur] - cur
    cur = v[cur] % N

NK = N * K

m = NK % cycle
cur = v[cur] % N
s = 0
while m > dist[cur] > 0 :
    s = dist[cur]
    cur = v[cur] % N
m -= s

ret = []
seen = set()
for a in A[N-m:] :
    if a in seen :
        p = ret.pop()
        while p != a :
            seen.remove(p)
            p = ret.pop()
    else :
        ret.append(a)
        seen.add(a)
        
print(' '.join([str(i) for i in ret]))

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n, k = map(int, input().split())
aa = list(map(int, input().split()))
idx = defaultdict(list)
to = [-1]*n
for i, a in enumerate(aa):
    idx[a].append(i)
for ik in idx.values():
    for i in range(len(ik)):
        to[ik[i - 1]] = ik[i]
start = []
ds=[]
i = 0
#print(idx)
#print(to)
while 1:
    ni = (to[i] + 1) % n
    d=(ni - i) % n
    if d==1:
        d+=n
    ds.append(d)
    start.append(i)
    if ni == 0: break
    i=ni
alld=sum(ds)
m=k*n%alld
s=0
for i,d in enumerate(ds):
    s+=d
    if s>m:
        s-=d
        break
mm=m-s
#print(start)
#print(ds)
#print(m,mm)
i=start[i]
s=[]
for ii in range(i,i+mm):
    if aa[ii] in s:
        i = s.index(aa[ii])
        s = s[:i]
    else:
        s += [aa[ii]]

print(*s)

Submission status: Runtime Error

Solution code:
import collections
N, K = [int(_) for _ in input().split()]
A1 = [int(_) for _ in input().split()]
A = A1 * 2
a_i = collections.defaultdict(lambda: [])
for i, a in enumerate(A):
    a_i[a] += [i]
i_delta = [0] * (2 * N)
for a in a_i:
    array = a_i[a]
    for i in range(len(array) - 1):
        i_delta[array[i]] = array[i + 1] - array[i] + 1
first = [-1] * N
second = [-1] * N
i = 0
while second[i % N] == -1:
    if first[i % N] == -1:
        first[i % N] = i
    else:
        second[i % N] = i
        break
    i += i_delta[i % N]
i %= N
start = first[i] + (second[i] - first[i]) * (
    (N * K - first[i]) // (second[i] - first[i]))
end = N * K
B = A1[N - (end - start) % N:] + A1 * ((end - start) // N)
dp = [{}]
for i, b in enumerate(B):
    if b in dp[-1]:
        dp = dp[:dp[-1][b]]
    else:
        s = dp[-1].copy()
        s[b] = len(dp)
        dp += [s]
print(*list(a for a, _ in sorted(dp[-1].items(), key=lambda xs: xs[1])))

Submission status: Runtime Error

Solution code:
import math

# input
#
# n = 5
# k = 10
# a = [1, 2, 3, 2, 3]

n, k = map(int, input().split())
a = [int(ai) for ai in input().split()]

# make jumpto
#
# 0 -> 6 (5)
# 1 -> 4 (2)
# 2 -> 5 (2)
# 3 -> 7 (3)
# 4 -> 8 (3)

jumpto = [[-1] * n]
la = -1
li = -1
for (ca, ci) in sorted(zip(a, range(n))):
    if ca == la:
        jumpto[0][ci] = jumpto[0][li] + li - ci
        jumpto[0][li] = ci - li
    else:
        jumpto[0][ci] = n
    la = ca
    li = ci

# doubling jumpto
#
# 0 -> 6 ->  9 (8)
# 1 -> 4 ->  8 (6)
# 2 -> 5 -> 11 (8)
# 3 -> 7 -> 10 (6)
# 4 -> 8 -> 12 (7)
#
# 0 -> 6 ->  9 -> 13 -> 17  (16)
# 1 -> 4 ->  8 -> 12 -> 15  (13)
# 2 -> 5 -> 11 -> 14 -> 18  (15)
# 3 -> 7 -> 10 -> 16 -> 19  (15)
# 4 -> 8 -> 12 -> 15 -> 21  (16)

levels = int(math.log2(k))
for l in range(1, levels + 1):
    jumpto.append([-1] * n)
    for i in range(n):
        f = jumpto[l - 1][i]
        s = jumpto[l - 1][(i + f + 1) % n]
        jumpto[l][i] = f + 1 + s

# step1
s = 0
while True:
    jumped = False
    for l in range(levels, -1, -1):
        if s + jumpto[l][s % n] < n * k:
            s += jumpto[l][s % n] + 1
            jumped = True
            break
    if not jumped:
        break

# step2
ret = []
while s < n * k:
    if s + jumpto[0][s % n] < n * k:
        s = s + jumpto[0][s % n] + 1
    else:
        ret.append(str(a[s % n]))
        s += 1

print(' '.join(ret))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

s_index = {}
s_list = []


s_index = {}
s_list = []
loop_i = K
for i in range(K):
    for a in A:
        if a in s_index:
            del_keys = [a]
            for key, val in s_index.items():
                if val > s_index[a]:
                    del_keys.append(key)

            s_list = s_list[:s_index[a]]
            for key in  del_keys :
                s_index.pop(key)
        else:
            s_index[a] = len(s_list)
            s_list.append(a)

    if len(s_list) == 0:
        loop_i = i
        break
#print(loop_i)
        

s_index = {}
s_list = []
for i in range(K%loop_i + 1):
    for a in A:
        if a in s_index:
            del_keys = [a]
            for key, val in s_index.items():
                if val > s_index[a]:
                    del_keys.append(key)

            s_list = s_list[:s_index[a]]
            for key in  del_keys :
                s_index.pop(key)
        else:
            s_index[a] = len(s_list)
            s_list.append(a)
            
print( ' '.join(list(map(str, s_list)))  )

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in range(n*k):
  if s in a[i%n]:
    s = s[:s.index(a[i%n])]
  else:
    s.append(a[i%n])
print(s)

Submission status: Runtime Error

Solution code:
import bisect
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(index):
  if index == N:
    ans.append(-1)
    return
  num = a[index]
  if len(data[num]) == 1:
    ans.append(index)
    tami(index+1)
  elif index == data[num][-1]:
    ans.append(index)
    tami(data[num][0] +1)
  else:
    tmp = bisect.bisect_right(data[num],index)
    #print(num,data[num],tmp)
    tami(data[num][tmp]+1)

N,K = IL()
a = IL()

data = [[] for i in range(N+1)]
ans = []

for i in range(N):
  data[a[i]].append(i)

#print(data)

tami(0)

#print(ans)
#print(len(ans),K)
#print(K%len(ans))

if K%len(ans) == 0:
  print()
else:
  x = ans[K%len(ans)-1]
  d = []
  d.append(a[x])
  while x != N-1:
    x += 1
    if len(data[a[x]]) == 1:
      d.append(a[x])
    elif x == data[a[x]][-1]:
      d.append(a[x])
    else:
      tmp = bisect.bisect_right(data[a[x]],x)
      x = data[a[x]][tmp]

  print(" ".join(map(str,d)))

"""
for i in range(K):
  for j in range(N):
    if a[j] in data:
      num = data.index(a[j])
      data = data[:num]
    else:
      data.append(a[j])
  print(data)

11 10
1 2 3 0 4 5 6 0 4 5 6

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

10 10
1 4 8 3 5 4 2 3 4 6
"""

Submission status: Runtime Error

Solution code:
import bisect
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(index):
  if index == N:
    ans.append(-1)
    return
  num = a[index]
  if len(data[num]) == 1:
    ans.append(index)
    tami(index+1)
  elif index == data[num][-1]:
    ans.append(index)
    tami(data[num][0] +1)
  else:
    tmp = bisect.bisect_right(data[num],index)
    #print(num,data[num],tmp)
    tami(data[num][tmp]+1)

def judge(x):
  d = []
  d.append(a[x])
  while True:
    x += 1
    if x == N:
      break
    if len(data[a[x]]) == 1:
      d.append(a[x])
    elif x == data[a[x]][-1]:
      d.append(a[x])
    else:
      tmp = bisect.bisect_right(data[a[x]],x)
      x = data[a[x]][tmp]
  return d

N,K = IL()
a = IL()

data = [[] for i in range(N+1)]
ans = []

for i in range(N):
  data[a[i]].append(i)

#print(data)

tami(0)
#print(ans)
#print(len(ans),K)
#print(K%len(ans))

if ans == [-1] or K%len(ans) == 0:
  print()
else:
  print(" ".join(map(str,(judge(ans[K%len(ans)-1])))))
"""
for i in range(K):
  for j in range(N):
    if a[j] in data:
      num = data.index(a[j])
      data = data[:num]
    else:
      data.append(a[j])
  print(data)

11 10
1 2 3 0 4 5 6 0 4 5 6

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

10 10
1 4 8 3 5 4 2 3 4 6
"""

Submission status: Runtime Error

Solution code:
import bisect
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(10000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(index):
  if index == N:
    ans.append(-1)
    return
  num = a[index]
  if len(data[num]) == 1:
    ans.append(index)
    tami(index+1)
  elif index == data[num][-1]:
    ans.append(index)
    tami(data[num][0] +1)
  else:
    tmp = bisect.bisect_right(data[num],index)
    #print(num,data[num],tmp)
    tami(data[num][tmp]+1)

def judge(x):
  d = []
  d.append(a[x])
  while True:
    x += 1
    if x == N:
      break
    if len(data[a[x]]) == 1:
      d.append(a[x])
    elif x == data[a[x]][-1]:
      d.append(a[x])
    else:
      tmp = bisect.bisect_right(data[a[x]],x)
      x = data[a[x]][tmp]
  return d

N,K = IL()
a = IL()

data = [[] for i in range(N+1)]
ans = []

for i in range(N):
  data[a[i]].append(i)

#print(data)

tami(0)
#print(ans)
#print(len(ans),K)
#print(K%len(ans))

if ans == [-1] or K%len(ans) == 0:
  print()
else:
  print(" ".join(map(str,(judge(ans[K%len(ans)-1])))))
"""
for i in range(K):
  for j in range(N):
    if a[j] in data:
      num = data.index(a[j])
      data = data[:num]
    else:
      data.append(a[j])
  print(data)

11 10
1 2 3 0 4 5 6 0 4 5 6

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

10 10
1 4 8 3 5 4 2 3 4 6
"""