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:
import bisect
import sys
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(100000000000)
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(1000000)
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(1000000)
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]:
  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:
N, K = map(int, input().split())
A = list(map(int, input().split()))

def add(S, e, A):
    for Ai in A:
      we = e
      S[e] = Ai
      e = S.index(Ai, 0, e)
      if we == e:
        e = e + 1
    return S, e

ok = False
S = [0] * (2 * N)
e = 0
for i in range(K):
    S, e = add(S, e, A)
    if e == 0:
        break
else:
    ok = True
    print(" ".join(map(str, S[:e])))

if not ok:
    for i in range(K%(i+1)):
        S, e = add(S, e, A)
    print(" ".join(map(str, S[:e])))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
L = [-1] * (2*10**5+2)  # aを入れてindexを返す
Plus = [0]*N  # indexを入れて1か0
Next = [0]*N  # indexを入れてindex
for i, a in enumerate(A):
    l = L[a]
    if l==-1:
        L[a] = i
    else:
        Next[l] = i+1
        L[a] = i

if l!=-1:
    Next[l] = 0
    Plus[l] = 1
#print(Next)

for i, a in enumerate(A):
    l = L[a]  # あなたはどこから来たの
    if i <= l:
        Next[l] = i+1
        Plus[l] += 1
        L[a] = -1


#
# print(Next)
# print(Plus)

loop = Plus[0]
i = Next[0]
while i!=0:
    i_next = Next[i]
    if i_next==N:
        loop += 1
        i_next = 0
    #if i_next <= i:
    #    loop += 1
    loop += Plus[i]

    i = i_next
#
# print(loop)

TrueNext = Next.copy()
for i in range(N-1,-1,-1):
    p = Plus[i]
    if p==0:
        nn = TrueNext[i]
        if nn==N:
            nn = 0
        TrueNext[i] = TrueNext[nn]
#
# print(TrueNext)

k = K % loop
if k==0:
    exit()

start = 0
i=0
while i<k-1:
    if start==N:
        start=0
        i+=1
    if i==K-1:
        break
    start = TrueNext[start]
    i+=1
# print(start)

ans = []#[A[start]]
S = [0] * (2*10**5+2)
#S[ans[0]] = 1
for a in A[start:]:
    #print(a, ans)
    if S[a]==0:
        ans.append(a)
        S[a] = 1
    else:
        while S[a] != 0:
            a_ = ans.pop()
            S[a_] = 0
print(*ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
As = [int(i) for i in input().split()]

s = []
extra = {}
loop_num = 0

for i in range(K):
    for val in As:
        if val not in s:
            s.append(val)
        elif s.index(val) == 0:
            s = []
        else:
            s = s[0:s.index(val)]
    extra[i] = s[:]

    if len(s) == 0:
        loop_num = i + 1
        break

if K%loop_num == 0:
    print("")
else:
    print(*extra[K%loop_num - 1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
As = [int(i) for i in input().split()]

s = []
extra = [[] for i in range(K)]
loop_num = 0

for i in range(K):
    for val in As:
        if val not in s:
            s.append(val)
        elif s.index(val) == 0:
            s = []
        else:
            s = s[0:s.index(val)]
    extra[i] = s[:]

    if len(s) == 0:
        loop_num = i + 1
        break

print(*extra[K%loop_num - 1])

Submission status: Runtime Error

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

from collections import deque

encount = []
for i in range(200001):
    encount.append(deque([]))

def solve(offset):
    counter = 0
    lencache = len(cache[offset])
    leninlist = len(inlist)

    for i in range(len(inlist)):
        encount[inlist[i]].append(i + lencache)

    c = 0

    while c < (lencache + leninlist):

        if c < lencache:
            curval = cache[offset][c]
        else:
            curval = inlist[c - lencache]

        while True:
            p = encount[curval].popleft()
            if p < c:
                continue

            elif p == c:
                if len(encount[curval]) == 0:
                    encount[curval].appendleft(counter)
                    counter += 1
                    cache[offset + 1].append(curval)
                    c += 1
                    break
                else:
                    pn = encount[curval].popleft()
                    c = pn + 1
                    break

cache = []

for i in range(200001):
    cache.append(deque([]))

loop = -1

for i in range(k):
    solve(i)
    if len(cache[i+1]) == 0:
        loop = i
        break

if loop == 0:
    print("")
elif loop != -1:
    print(" ".join(list(map(str,cache[(k % loop + 1)]))))
else:
    print(" ".join(list(map(str, cache[i + 1]))))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
jump_to = [None] * (n * 2)
indices = dict()
for i in range(n * 2):
    idx = i % n
    a = a_list[idx]
    if a in indices and indices[a] is not None:
        jump_to[indices[a]] = idx + 1 if idx < n - 1 else 0
    indices[a] = i
jump_to = jump_to[:n]

loop = 0
i = 0
while True:
    j = jump_to[i]
    if j <= i + 1:
        loop += 1
    if j == 0:
        break
    i = j
k = k % loop

counts = dict()
search_list = a_list * k
for a in a_list:
    counts.setdefault(a, 0)
    counts[a] += k

ans = []
ignoreUnless = -1
for a in search_list:
    counts[a] -= 1
    if ignoreUnless >= 0:
        if a == ignoreUnless:
            ignoreUnless = -1
        continue
    count = counts[a]
    if count % 2 == 1:
        ignoreUnless = a
    elif count == 0:
        ans.append(a)
print(' '.join(map(str, ans)))

Submission status: Runtime Error

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

s_index = {}
s_list = []
    
loop_i = N  - 1
        

if loop_i != -1:
    s_index = {}
    s_list = []
    for i in range(K % loop_i):
        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(s_list)
        
    
print( ' '.join(list(map(str, s_list)))  )

Submission status: Runtime Error

Solution code:
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

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

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

def minus():
	return -1

D = defaultdict(minus)
D2 = defaultdict(minus)
jump = deque()
first = deque()
for i in range(N):
	if D[A[i]] != -1:
		bisect.insort(jump, (D[A[i]], i))
	D[A[i]] = i
	if D2[A[i]] == -1:
		D2[A[i]] = i
	first.append(D2[A[i]])

x = 0
roop = [0]
syugo = set()
syugo.add(x)
while True:
	while True:
		loc = bisect.bisect_left(jump, (x, x))
		if loc < len(jump):
			if x == jump[loc][0]:
				x = jump[loc][1]
			else:
				break
			x += 1
			if x == N:
				x = 0
				break
		else:
			break
	x = first[x]
	size1 = len(syugo)
	syugo.add(x)
	size2 = len(syugo)
	if size1 == size2:
		break
	else:
		roop.append(x)
mod = K % len(roop)
x = (roop[mod] + 1)%N
output = []
while x < N:
	loc = bisect.bisect_left(jump, (x, x))
	if loc < len(jump):
		if x == jump[loc][0]:
			x = jump[loc][1]
			x = x + 1
		else:
			output.extend(A[x:jump[loc][0]])
			x = jump[loc][0]
			break
	else:
		output.extend(A[x:N])

for i in range(len(output)):
	print(output[i], end='')
	print(" ", end='')
print('\n')

Submission status: Runtime Error

Solution code:

N, K = map(int, input().split())
A = list(map(int, input().split()))
L = [-1] * (2*10**5+2)  # aを入れてindexを返す
Plus = [0]*N  # indexを入れて1か0
Next = [0]*N  # indexを入れてindex
for i, a in enumerate(A):
    l = L[a]
    if l==-1:
        L[a] = i
    else:
        Next[l] = i+1
        L[a] = i

if l!=-1:
    Next[l] = 0
    Plus[l] = 1
#print(Next)

for i, a in enumerate(A):
    l = L[a]  # あなたはどこから来たの
    if i <= l:
        Next[l] = i+1
        Plus[l] += 1
        L[a] = -1


#
# print(Next)
# print(Plus)

loop = Plus[0]
i = Next[0]
while i!=0:
    i_next = Next[i]
    if i_next==N:
        loop += 1
        i_next = 0
    #if i_next <= i:
    #    loop += 1
    loop += Plus[i]

    i = i_next
#
# print(loop)

TrueNext = Next.copy()
for i in range(N-1,-1,-1):
    p = Plus[i]
    if p==0:
        TrueNext[i] = TrueNext[TrueNext[i]]
#
# print(TrueNext)

k = K % loop
if k==0:
    exit()

start = 0
i=0
while i<k-1:
    if start==N:
        start=0
        i+=1
    if i==K-1:
        break
    start = TrueNext[start]
    i+=1
# print(start)

ans = []#[A[start]]
S = [0] * (2*10**5+2)
#S[ans[0]] = 1
for a in A[start:]:
    #print(a, ans)
    if S[a]==0:
        ans.append(a)
        S[a] = 1
    else:
        while S[a] != 0:
            a_ = ans.pop()
            S[a_] = 0
print(*ans)

Submission status: Runtime Error

Solution code:
import sys
N , K = map(int, input().split())
a = list(input().split())
#print(a)
a = [int(s) for s in a]
for i in range(len(a)-1):
        if a[i] == a[i+1]:
                a = a[:i] + a[i+2:]
                i -= 2
b = a
q = 0
while True:
        check = True
        if b == []: break
        for i in range(q+1,len(b)):
                if b[q] == b[i]:
                        b = b[:q] + b[i+1:]
                        check = False
                        break
        if check:
                q += 1
                if q >= len(b): break
#print(b)
if b == []:
        print()
        sys.exit()
#print(a)
x = b
for i in range(K-1):
        x += a
p = 0
 
while True:
#       print(x)
        check = True
        if x == []: break
        for i in range(p+1,len(x)):
                if x[p] == x[i]:
                        x = x[:p] + x[i+1:]
                        check = False
                        break
        if check:
                p+=1
                if p >= len(x): break
 
 
for i in range(len(x)-1):
        print("{} ".format(x[i]),end="")
if len(x): print("{}".format(x[-1]))
else: print()

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
a = list(map(int, input().split()))
ans = []
idx = 0 

while True:
  x = idx
  for j in range(idx, n):
    if a[j] == a[idx] and j != idx:
      x = j
      break
  if x != idx:
    idx = x + 1
    if idx == n:
      break
  else:
    ans.append(a[idx:])
   # print("check", idx)
    for j in range(idx):
      if a[j] == a[idx] and j != idx:
        idx = j + 1 
        break
    else:
      idx = idx + 1
count =len(ans) + 1
k %= count
#print(ans, k)
if k != 0:
  L = ans[k-1]
  idx = 1
  m = len(L)
  if m > 3:
    while True:
      for i in range(idx+1, m):
        if L[i] == L[idx]:
          L = L[0:idx] + L[i+1:m]
          break
      if m == len(L):
        idx += 1
      else:
        m = len(L)
      if m <= idx:
        break
  print(" ".join(map(str, L)))

Submission status: Runtime Error

Solution code:
import sys
N , K = map(int, input().split())
a = list(input().split())
#print(a)
a = [int(s) for s in a]
for i in range(len(a)-1):
        if a[i] == a[i+1]:
                a = [:i] + a[i+2:]
                i -= 2
b = a
q = 0
while True:
        check = True
        if b == []: break
        for i in range(q+1,len(b)):
                if b[q] == b[i]:
                        b = b[:q] + b[i+1:]
                        check = False
                        break
        if check:
                q += 1
                if q >= len(b): break
#print(b)
if b == []:
        print()
        sys.exit()
#print(a)
x = b
for i in range(K-1):
        x += a
p = 0

while True:
#       print(x)
        check = True
        if x == []: break
        for i in range(p+1,len(x)):
                if x[p] == x[i]:
                        x = x[:p] + x[i+1:]
                        check = False
                        break
        if check:
                p+=1
                if p >= len(x): break


for i in range(len(x)-1):
        print("{} ".format(x[i]),end="")
if len(x): print("{}".format(x[-1]))
else: print()

Submission status: Runtime Error

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

s_index = {}
s_list = []
    

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)
        
        #print(s_list)
    if len(s_list) == 0: 
        #print(s_list, i)
        loop_i = i+1
        break
        
s_index = {}
s_list = []
for i in range(K % loop_i):
    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(s_list)
    if len(s_list) == 0: 
        #print(s_list, i)
        loop_i = i+1
        break        
        
    
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()))
L = [-1] * (2*10**5+2)  # aを入れてindexを返す
Plus = [0]*N  # indexを入れて1か0
Next = [0]*N  # indexを入れてindex
for i, a in enumerate(A):
    l = L[a]
    if l==-1:
        L[a] = i
    else:
        Next[l] = i+1
        L[a] = i

if l!=-1:
    Next[l] = 0
    Plus[l] = 1
#print(Next)

for i, a in enumerate(A):
    l = L[a]  # あなたはどこから来たの
    if i <= l:
        Next[l] = i+1
        Plus[l] += 1
        L[a] = -1


#
# print(Next)
# print(Plus)

loop = Plus[0]
i = Next[0]
while i!=0:
    i_next = Next[i]
    if i_next==N:
        loop += 1
        i_next = 0
    #if i_next <= i:
    #    loop += 1
    loop += Plus[i]

    i = i_next
#
# print(loop)

TrueNext = Next.copy()
for i in range(N-1,-1,-1):
    p = Plus[i]
    if p==0:
        TrueNext[i] = TrueNext[TrueNext[i]]
#
# print(TrueNext)

k = K % loop
if k==0:
    exit()

start = 0
for i in range(k-1):
    start = TrueNext[start]
# print(start)

ans = []#[A[start]]
S = [0] * (2*10**5+2)
#S[ans[0]] = 1
for a in A[start:]:
    #print(a, ans)
    if S[a]==0:
        ans.append(a)
        S[a] = 1
    else:
        while S[a] != 0:
            a_ = ans.pop()
            S[a_] = 0
print(*ans)

Submission status: Runtime Error

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

s = []

for _ in range(k%(n-1)):
    for i in range(n):
        if a[i] in s:
            while a[i] in s:
                del s[-1]
        else:
            s.append(a[i])


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

Submission status: Runtime Error

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

s = []

for _ in range(k%(n-1)):
    for i in range(n):
        if a[i] in s:
            while a[i] in s:
                del s[-1]
        else:
            s.append(a[i])
    print(' '.join(map(str, s)))


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

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()

a = na()

import numpy as np
from collections import deque

d =[]
ans = []
for q in range(k):
    g = d+a
    m = len(g)
    d = []
    i = 0
    while True:
        for j in range(i+1,m):
            if g[i] == g[j]:
                i = j+1
                break
        else:
            d.append(g[i])
            i += 1
        if i == m-1:
            d.append(g[m-1])
            break
        if i >= m:
            break
    ans.append(d)
    if len(d) == 0:
        print(ans[k%(q+1)-1])
        exit()

print(d)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
X=A*k
s=[]
for i in range(0,n*k):
    if s.count(X[i])==0:
       s.append(X[i])
    else:
       while s.count(X[i])!=0:
            del s[-1]
for j in range(0,len(s)):
    s[j]=str(s[j])
print(" ".join(s))

Submission status: Runtime Error

Solution code:
import random #入力整数列Aの生成に使用

class Node:
    def __init__(self, data): #コンストラクタ
        self.data = data #ノードがもつ数値
        self.left = None #左エッジ
        self.right = None #右エッジ


class BST:
    def __init__(self, number_list): #コンストラクタ
        self.root = None #ルートノード初期化
        for node in number_list: #数値を持つ配列から二分木を生成
            self.insert(node) #挿入メソッドを使ってノードを挿入する
    #挿入
    def insert(self, data):
        n = self.root
        if n == None:
            self.root = Node(data)
            return
        else:
            while True:
                entry = n.data
                if data < entry:
                    if n.left is None:
                        n.left = Node(data)
                        return
                    n = n.left
                elif data > entry:
                    if n.right is None:
                        n.right = Node(data)
                        return
                    n = n.right
                else:
                    n.data = data
                    return
    #検索機能(インターフェース)
    def search(self, search):
        searcher = self._search_bool(search)
        if searcher is None:
            return False
        elif searcher == True:
            return True
        elif searcher == False:
            return False

    #検索機能本体(出力:boolean),深さ優先探索
    #nodeのvisitedはpopで代用
    def _search_bool(self, search):
        n = self.root
        if n is None:
            return None
        else:
            lst = []
            lst.append(n)
            while len(lst) > 0:
                node = lst.pop()
                if node.data == search:
                    return True
                if node.right is not None:
                    lst.append(node.right)
                if node.left is not None:
                    lst.append(node.left)
            return False

    def inorder(self,node): #中順探索 l->r->p^n
        if node is not None:
            self.inorder(node.left)
            print(node.data)
            self.inorder(node.right)
B = BST([0])
import bisect
N, K = map(int, input().split())
A = list(map(int, input().split()))
index = [-1]*(10**5*2+10)
last = [-1]*(N+1)
for i in range(N)[::-1]:
    ind = index[A[i]]
    if ind>=0:
        last[i] = last[ind+1]
        index[A[i]] = -1
    else:
        last[i] = A[i]
        index[A[i]] = i
first_index = [-1]*(10**5*2+10)
for i in range(N):
    if first_index[A[i]]<0:
        first_index[A[i]] = i
loop = [0]
ind = 0
while True:
    next_index = (first_index[last[ind]]+1)%N
    if B.search(next_index):
        for i in range(len(loop)):
            if loop[i]==next_index:
                start = i
                break
        break
    loop.append(next_index)
    B.insert(next_index)
    ind = next_index
    
ll = len(loop)

index = [[] for _ in range(10**5*2+10)]
ans = []
if K-1>=ll:
    ii = (K-start)%(ll-start)-1+start
    if (K-start)%(ll-start)==0:
        ii+=ll
else:
    ii = K-1
for i in range(loop[ii], N):
    index[A[i]].append(i)
i=loop[ii]
while i<N:
    if index[A[i]][-1]==i:
        ans.append((str(A[i])))
        i+=1
    else:
        i=index[A[i]][bisect.bisect_left(index[A[i]], i)+1]+1
print(' '.join(ans))

Submission status: Runtime Error

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

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

#aa = [ ai for i in range(k) for ai in a]
#aa = [ ai for ai in a]

s = []
j = 0
#for j in range(5):
while j < k :
  if i in aa:
    p = s.index(i)
    del s[p:]
  else:
    s.append(i)
      
  if j == 0 and len(s) == 0:
    break
  elif j > 1 and len(s) == 0:
    j = k - k % j - 1
    
  else:
    j += 1

if len(s) > 0:
  print(s[0:1][0],end="")
  
  if len(s) > 1:
    for ss in s[1:]:
      print(" {}".format(ss),end="")

print()

Submission status: Runtime Error

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

count = 0
j = 0
def f(s,j,count):
    cho = False
    for t in range(j+1,n):
        if a[s] == a[t]:
            cho == True
            if t+1==n:
                return count
            else:
                return f(t+1,t+1,count)
    if not cho:
        return f(s,-1,count+1)

lasta = []
if f(0,0,0) == 0:
    lasta = []
else:
    for i in range(0,k%f(0,0,0)):
        lasta += a

def lastf(s,lista):
    cho = False
    for t in range(s+1,len(lista)):
        if lista[s] == lista[t]:
            cho = True
            lista = lista[0:s] + lista[t+1:len(lasta)]
            if len(lista)==s:
                return lista
            else:
                return lastf(s,lista)
    if not cho:
        if s < len(lista)-1:
            return lastf(s+1,lista)
        else:
            return lista


ans = lastf(0,lasta)
print(" ".join(map(str,ans)))

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
A = list(map(int,input().split()))
MAX = 10**5+1
nxt = [-1 for i in range(MAX)]

last_index = [-1 for i in range(MAX)]
next_index = [-1 for i in range(MAX)]
first_index = [-1 for i in range(MAX)]
for i in range(N):
	a = A[i]
	last = last_index[a]
	if last >= 0:
		next_index[last] = i
	else:
		first_index[a] = i
	last_index[a] = i

start_stat = 0
while True:
	current = first_index[start_stat] + 1
	while current < N and next_index[current] != -1:
		current = next_index[current] + 1
	if current >= N:
		nxt[start_stat] = 0
		break
	else:
		stat = A[current]
		nxt[start_stat] = A[current]
		if nxt[A[current]] == -1:
			start_stat = A[current]
			continue
		else:
			break

order = [0]
in_order = [False for i in range(MAX)]
in_order[0] = True
last_stat = nxt[0]
while not in_order[last_stat]:
	order.append(last_stat)
	in_order[last_stat] = True
	last_stat = nxt[last_stat]
a,b = 0,len(order)
for x in order:
	if x == last_stat:
		break
	else:
		a += 1
		b -= 1
stat_index = K if K < a else (K-a) % b
init_status = order[stat_index]
before_stat = order[(stat_index - 1) % len(order)]
current = first_index[before_stat] + 1
if init_status == 0:
	print("")
	exit()
ans = []
while current < N:
	if next_index[current] != -1:
		current = next_index[current] + 1
	else:
		ans.append(A[current])
		current += 1
print( " ".join(list(map(str, ans))))

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list
def summarize_list(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res

def main():
  n,k=LI()

  l=LI()
  l=l*k

  # print(summarize_list(l))

  a=[]

  for x in l:
    y=a.count(x)
    
    if y>0:
      while True:
        if y==0:
          break

        p=a.pop()
        # print(a)
        if p==x:
          y-=1

    else:
      a.append(x)
      # print(a)

  return ' '.join([str(x) for x in a])

# main()
print(main())

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10000)
N, K = [int(i) for i in input().split()]
A = tuple(int(i) for i in input().split())
super_flag = False
try:
    A = A * K
except BaseException:
    super_flag = True


@lru_cache(maxsize=2**20)
def add_to_list(li, num):
    if num in li:
        li = li[:li.index(num)]
        return li
    else:
        li = li + (num,)
        return li


@lru_cache(maxsize=2**20)
def get(li):
    if len(li) != 1:
        yoi_list_b = get(li[:-1])
        yoi_list = add_to_list(yoi_list_b, li[-1])
    else:
        yoi_list = li
    return yoi_list


if not super_flag:
    li = get(A)
else:
    li = tuple()
    count = 0
    for _ in range(K):
        li = get(li + A)
        count += 1
        if not li:
            li = tuple()
            for _ in range(K % count):
                li = get(li + A)
            break
if li:
    for i in li[:-1]:
        print(i, end=" ")
    print(li[-1])
else:
    pass