Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
A = list(map(int, input().split()))

remain = [''] * (N+1) 
d = {}
for i, a in enumerate(A[::-1]):
  j = N - 1 - i
  if a in d:
    idx = d[a][-1]
    remain[j] = remain[idx+1]
    d[a].append(j)
  else:
    remain[j] = str(a) + ' ' + remain[j+1]
    d[a] = [j]

cands = []
idx = 0
for k in range(1, K+1):
  S = remain[idx]
  cands.append(S)
  if S == '':
    break
  s = int(S.split()[0])
  idx = d[s][-1] + 1

K = K % k
print(cands[K-1])

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

first = [0] * (N+1)
follow_idx = [-1] * (N+1)
d = {}
for i, a in enumerate(A[::-1]):
  j = N - 1 - i
  if a in d:
    idx = d[a][-1]
    d[a].append(j)
    if first[idx + 1] < 0:
      first[j] = first[idx + 1]
      follow_idx[j] = follow_idx[idx + 1]
    else:
      first[j] = -first[idx + 1]
      follow_idx[j] = idx + 1
  else:
    first[j] = a
    d[a] = [j]
    follow_idx[j] = j + 1


indices = [-1]
s0 = first[0]
if s0 >= 0:
  indices.append(0)
else:
  s0 = -s0
  indices.append(follow_idx[0])
    
if s0 == 0:
  print('')
  quit()

d2 = {0:0, s0:1}
for k in range(2, K+1):
  idx = d[s0][-1] + 1
  s0 = first[idx]
  if s0 >= 0:
    indices.append(idx)
  else:
    s0 = -s0
    indices.append(follow_idx[idx])
  print(s0)
  if s0 in d2:
    break
  d2[s0] = k

if len(indices) < K:
  K = d2[s0] + (K - d2[s0]) % (k - d2[s0])

if K == 0:
  print('')
  quit()

ans = []
idx = indices[K]
while idx < N:
  s = first[idx]
  if s > 0:
    ans.append(s)
  idx = follow_idx[idx]

print(*ans)

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






first = [0] * (N+1)
follow_idx = [-1] * (N+1)

d = {}
for i, a in enumerate(A[::-1]):
  j = N - 1 - i
  
  if a in d:
    idx = d[a][-1] + 1 
    d[a].append(j)
    if first[idx] < 0: 
      first[j] = first[idx] 
      follow_idx[j] = follow_idx[idx] 
    else: 
      first[j] = -first[idx] 
      follow_idx[j] = idx 
  
  
  else:
    first[j] = a 
    d[a] = [j]
    follow_idx[j] = j + 1 



indices = [-1] 
s0 = first[0] 
if s0 >= 0: 
  indices.append(0) 
else:
  s0 = -s0 
  indices.append(follow_idx[0]) 

if s0 == 0: 
  print('')
  quit()




first_a = {0:0, s0:1}
for k in range(2, K+1):
  idx = d[s0][-1] + 1 
  s0 = first[idx] 
  if s0 >= 0: 
    indices.append(idx) 
  else: 
    s0 = -s0 
    indices.append(follow_idx[idx]) 
  if s0 in first_a: 
    break
  first_a[s0] = k 

if len(indices) < K: 
  # 1, 2, ..., first_a[s0], ..., k
  # ?, ?, ..., s0,          ..., s0
  #              <------------->
  
  K = first_a[s0] + (K - first_a[s0]) % (k - first_a[s0])

if K == 0: 
  print('')
  quit()

ans = []
idx = indices[K] 
while idx < N: 
  s = first[idx]
  if s > 0: 
    ans.append(s)
  idx = follow_idx[idx]

print(*ans)

Verdicts: Runtime Error

Wrong Answer

Accepted