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: Wrong Answer

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
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: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
s=[]
b=0
for n in range(N*K):
    j=n-b*N
    if j>=N:
        b+=1
        j=n-b*N
    if (a[j] in s) :
        while True:
            if s.pop()==a[j]:
                break
    else:
        s.append(a[j])
    j=0

print(s)

Submission status: Wrong Answer

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

cnt = 0
s = ''
tmp = []
for i in range(K):
        for n in A:
                if n in s:
                        while n in s:
                                s = s[:-1]
                else:
                        s = s + n
        if s in tmp:
                break
        else:
                tmp.append(s)
else:
        for ans in tmp:
                print(ans)
                exit()

tmp = tmp[K % len(tmp) - 1]
for ans in tmp:
        print(ans)
        exit()

Submission status: Wrong Answer

Solution code:
N=[int(n) for n in input().split()]
A=[int(n) for n in input().split()]
s=[]
for j in range(min(N[0]+1,N[1])):
  for i in range(N[0]):
    if A[i] in s:
      s=s[:s.index(A[i])]
    else:
      s.append(A[i])
print(s)

Submission status: Wrong Answer

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

# N = 200000
# K = 2
# As = [i+1 for i in range(N)]
if K >= 2:
    K = 2
X = [As[i%N] for i in range(N*K)]
s = []
Nmax = 200160

dp = [0] * Nmax #最初に入ってあるiの数

for i,x in enumerate(X):
    if dp[x] == 0:
        dp[x] = i+1
    else:
        dp = [0 if dp[x]-x1<=0 else x1 for x1 in dp]

ans1 = sorted([(x,i) for i,x in enumerate(dp) if x != 0])
ans = [i for x,i in ans1]
print(*ans)

Submission status: Wrong Answer

Solution code:
import math
# import collections


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

    dp = {}
    memo = set()

    
    L = [0] * N
    arrays = {}
    values = set()
    index = 0
    # K = min(K, K % 10 + 10)
    for k in range(K):
        for a in A:
            # print(L, memo)
            if a in memo:
                index = dp[a]
                memo = set(L[:index])
            else:
                L[index] = a
                dp[a] = index
                memo.add(a)
                index += 1
        if index == 0:
            s = " "
        else:
            s = " ".join(map(str,L[:index]))
        if s in values:
            loop = len(arrays)
            # print(loop)
            # print(arrays)
            print(arrays[(K-1) % loop])
            return
        else:
            arrays[k] = s
            values.add(s)



    print(*L, sep=" ")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_right
from collections import defaultdict
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))

N, K = inpl()
A = inpl()
places = defaultdict(list)
nextplace = [-1]*N
newloop = [-1]*N
for i in range(N):
    places[A[i]].append(i)
for i in range(N):
    pl = places[A[i]]
    k = bisect_right(pl,i)
    if k >= len(pl):
        nextplace[i] = (pl[0]+1)%N
        if nextplace[i] == 0:
            newloop[i] = 2
        else:
            newloop[i] = 1
    else:
        nextplace[i] = (pl[k]+1)%N
        if nextplace[i] == 0:
            newloop[i] = 1
        else:
            newloop[i] = 0

Nloop = 0
prev_loop = [-1]*N
j = 0
while prev_loop[j] < 0 and (Nloop+newloop[j] < K 
                          or (Nloop+newloop[j]==K and nextplace[j]==0) ):
    prev_loop[j] = Nloop
    Nloop += newloop[j]
    j = nextplace[j]
if Nloop == K:
    exit()
if prev_loop[j] >= 0:
    period = Nloop - prev_loop[j]
    Nloop = K - (K - Nloop) % period
    while Nloop+newloop[j] < K or (Nloop+newloop[j]==K and nextplace[j]==0):
        Nloop += newloop[j]
        j = nextplace[j]
    if Nloop == K:
        exit()

S = [0]*N
lenS = 0
XinS = defaultdict(lambda: -1)
for i in range(j,N):
    if XinS[A[i]] < 0:
        S[lenS] = A[i]
        XinS[A[i]] = lenS
        lenS += 1
    else:
        lenS = XinS[A[i]]
        XinS[A[i]] = -1
if lenS > 0:
    print(' '.join(map(str,S[:lenS])))

Submission status: Wrong Answer

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()

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

  l=LI()
  l=l+l

  a=[]

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

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

    else:
      a.append(x)

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

# main()
print(main())

Submission status: Wrong Answer

Solution code:
N=[int(n) for n in input().split()]
A=[int(n) for n in input().split()]
s=[]
for j in range(min(2+N[1]%2,N[1])):
  for i in range(N[0]):
    if A[i] in s:
      s=s[:s.index(A[i])-1]
    else:
      s.append(A[i])
print(s)

Submission status: Wrong Answer

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

s_index = {}
s_list = []

for _ in range(K%4+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: Wrong Answer

Solution code:
N_str, K_str = input().split()
N = int(N_str)
K = int(K_str)

A_str = input().split()
A = [int(a) for a in A_str]

X = []

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def check(J):
    p = 0
    while True:
        inner = my_index_multi(J, J[p])
        if 2 <= len(inner):
            J = J[:inner[0]] + J[inner[1] + 1:]
        else:
            p += 1

        if len(J) <= p:
            return J


air = 1

for i in range(K):
    X.extend(A)
    X = check(X)
    if len(X) == 0:
        air = i + 1
        break

n = int(K / air)

K = K - n * air

for i in range(K):
    X = A * K
    check(X)


for i in range(len(X)):
    print(X[i], end=' ')

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [0] * N
dic = {}
for i in range(N):
    a = A[i]
    if dic.get(a, None) is None:
        dic[a] = 1
    else:
        dic[a] += 1

count = 0
for i in range(N):
    a = A[i]
    v = dic.get(a)
    if v % 2 == 1:
        B[count] = a
        count += 1
A = B[0:count]
N = count
dic = {}
for i in range(N):
    a = A[i]
    v = dic.get(a, None)
    if v is None:
        dic[a] = [i]
    else:
        v.append(i)
result = []
result_dic = {}
result_dic_index = {}
count = 0
loop = 0
while count < K:
    index = 0
    if count != 0:
        if result:
            a = result[index]
            v = dic.get(a)
            next_i = v[0]
            index = next_i + 1
            result = []
    while index < N:
        a = A[index]
        v = dic.get(a)
        next_i = -1
        for i in v:
            if i > index:
                next_i = i
                break
        if next_i == -1:
            result.append(a)
            index += 1
        else:
            index = next_i + 1
    tu = tuple(result)
    t = result_dic.get(tu, None)
    if t is None:
        result_dic[tu] = count
        result_dic_index[count] = tu
    else:
        loop = count
        break
    count += 1

answers = None
if loop == 0:
    answers = tu
else:
    index = (K - 1) % loop
    answers = result_dic_index[index]

for answer in answers:
    print(answer, end=" ")
print()

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [0] * N
dic = {}
for i in range(N):
    a = A[i]
    if dic.get(a, None) is None:
        dic[a] = 1
    else:
        dic[a] += 1

count = 0
for i in range(N):
    a = A[i]
    v = dic.get(a)
    if v % 2 == 1:
        B[count] = a
        count += 1
A = B[0:count]
N = count
dic = {}
for i in range(N):
    a = A[i]
    v = dic.get(a, None)
    if v is None:
        dic[a] = [i]
    else:
        v.append(i)
print(dic)
result = []
result_dic = {}
result_dic_index = {}
count = 0
loop = 0
while count < K:
    index = 0
    if count != 0:
        if result:
            a = result[index]
            v = dic.get(a)
            next_i = v[0]
            index = next_i + 1
            result = []
    while index < N:
        a = A[index]
        v = dic.get(a)
        next_i = -1
        for i in v:
            if i > index:
                next_i = i
                break
        if next_i == -1:
            result.append(a)
            index += 1
        else:
            index = next_i + 1
    tu = tuple(result)
    t = result_dic.get(tu, None)
    if t is None:
        result_dic[tu] = count
        result_dic_index[count] = tu
    else:
        loop = count
        break
    count += 1

answers = None
if loop == 0:
    answers = tu
else:
    index = (K - 1) % loop
    answers = result_dic_index[index]

for answer in answers:
    print(answer, end=" ")
print()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n, k = rli()
    la = rli()
    MOD = len(la)
    lb = [-1 for _ in range(len(la))]
    periods = dict()
    for i, a in enumerate(la):
        periods.setdefault(a, [])
        periods[a].append(i)
    for v in periods.values():
        for i, e in enumerate(v):
            if i == len(v)-1:
                lb[e] = (v[0] + 1) % MOD
            else:
                lb[e] = (v[i+1] + 1) % MOD
    loop = [0]
    b = 0
    while True:
        nb = lb[b]
        if nb == 0:
            break
        if nb-b <= 1:
            loop.append(nb)
        b = nb
    start = loop[(k-1)%len(loop)]
    start2 = loop[(k+len(loop)-2)%len(loop)]
    lc = la[start2:] + la

    counts = dict()
    for i, a in enumerate(lc):
        counts.setdefault(a, 0)
        counts[a] += 1

    ans = []
    target = None
    for a in lc:
        if target is None:
            count = counts[a]
            if counts[a] >= 2:
                target = a
            else:
                ans.append(a)
        else:
            counts[a] -= 1
            if a == target:
                counts[a] -= 1
                target = None
    # print(la[start:], lc)
    # print(lb, loop)
    print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def add(S, A):
    for Ai in A:
        try:
            S = S[:S.index(Ai)]
        except ValueError as _:
            S.append(Ai)
    return S
kstr = bin(K)[2:]

As = [None] * (len(kstr)+1)
As[1] = add([], A)

for i in range(2, len(kstr)+1):
    As[i] = add(As[i-1].copy(), As[i-1])

S = []
for i in range(len(kstr), 0, -1):
    if kstr[-i] == "1":
        S = add(S, As[i])

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

Submission status: Wrong Answer

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

s_index = {}
s_list = []

for _ in range(K%2+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: Wrong Answer

Solution code:
N=[int(n) for n in input().split()]
A=[int(n) for n in input().split()]
s=[]
for j in range(min(2,N[1])):
  for i in range(N[0]):
    if A[i] in s:
      s=s[:s.index(A[i])-1]
    else:
      s.append(A[i])
print(s)

Submission status: Wrong Answer

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

memo = {}
nxts = [None] * N
for i, x in enumerate(A + A):
  if x in memo:
    nxts[memo[x] % N] = i % N
  memo[x] = i

k = 0
visited = [-1] * N
p = 0
flag = False
ans = []
while k < K:
  if flag == False and visited[p] >= 0:
    vp = visited[p]
    k = vp + ((K-1) - vp) // (k - vp) * (k - vp)
    flag = True
  visited[p] = k
  nxt = nxts[p]
  if k == K-1 and nxt <= p:
    ans.append(A[p])
    nxt = p + 1
    if nxt >= N:
      break
  else:
    nxt = (nxt + 1) % N
    if nxt <= p + 1:
      k += 1
  p = nxt
if len(ans):
  print(' ' .join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a_list=list(map(int,input().split()))
dp=[-1]*(n+1)
dp[n]=0
D={}
for i in range(n-1,-1,-1):
    if not a_list[i] in D:
        dp[i]=a_list[i]
        D[a_list[i]]=i+1
    else:
        dp[i]=dp[D[a_list[i]]]
        D[a_list[i]]=i+1
first={}
for i in range(n):
    if not a_list[i] in first:
        first[a_list[i]]=i
cycle=[dp[0]]
i=dp[0]
while True:
    if i==0:
        break
    ne=first[i]
    i=dp[ne+1]
    cycle.append(i)
cycle.pop()
cycle=[0]+cycle
T=len(cycle)
order=k%T
if 1<=order:
    start=cycle[order-1]
    check={}
    if start==0:
        ans=[]
        for i in range(n):
            if ans==[]:
                ans.append(a_list[i])
                check[a_list[i]]=0
            else:
                if a_list[i] in check:
                    ans=ans[:check[a_list[i]]]
                else:
                    ans.append(a_list[i])
                    check[a_list[i]]=len(ans)-1
    else:
        ans=[]
        x=first[start]
        for i in range(x+1,n):
            if ans==[]:
                ans.append(a_list[i])
                check[a_list[i]]=0
            else:
                if a_list[i] in check:
                    ans=ans[:check[a_list[i]]]
                else:
                    ans.append(a_list[i])
                    check[a_list[i]]=len(ans)-1
    for i in ans:
        print(i,end='')
        print(' ',end='')
else:
    print()

Submission status: Wrong Answer

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 = []
for i in range(loop[(K-start)%(ll-start)-1], N):
    index[A[i]].append(i)
i=loop[(K-start)%(ll-start)-1]
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: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
hoge = [[]for i in range(200001)]
for i in range(N):
    hoge[A[i]].append(i)
NEXT = [-1]*N
for i in hoge:
    for j in range(-len(i),0):
        NEXT[i[j]] = i[j+1]
i = 1
p = 0
while i < K:
    #print(i,p)
    if NEXT[p] <= p or NEXT[p] == N-1:
        i += 1
    p = (NEXT[p]+1)%N
    if p == 0:
        i = (K//(i-1))*(i-1)+1
while p < N:
    if NEXT[p] <= p:
        print(A[p],end = " ")
        p += 1
    else:
        p = NEXT[p]+1
print()

Submission status: Wrong Answer

Solution code:
N=[int(n) for n in input().split()]
A=[int(n) for n in input().split()]
s=[]
for j in range(N[1]):
  for i in range(N[0]):
    if A[i] in s:
      s=s[:s.index(A[i])-1]
    else:
      s.append(A[i])
print(s)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
hoge = [[]for i in range(200001)]
for i in range(N):
    hoge[A[i]].append(i)
NEXT = [-1]*N
for i in hoge:
    for j in range(-len(i),0):
        NEXT[i[j]] = i[j+1]
i = 1
p = 0
while i < K:
    #print(i,p)
    if NEXT[p] <= p or NEXT[p] == N-1:
        i += 1
    p = (NEXT[p]+1)%N
    if p == 0:
        i = min(((K-1)//(i-1))*(i-1)+1,K-2)
while p < N:
    if NEXT[p] <= p:
        print(A[p],end = " ")
        p += 1
    else:
        p = NEXT[p]+1
print()

Submission status: Wrong Answer

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

s = []
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)]

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

if len(s) != 0:
    print(*s)
else:
    for i in range(K%loop_num):
        for val in As:
            print(s)
            if val not in s:
                s.append(val)
            elif s.index(val) == 0:
                s = []
            else:
                s = s[0:s.index(val)]
    print(*s)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
hoge = [[]for i in range(200001)]
for i in range(N):
    hoge[A[i]].append(i)
NEXT = [-1]*N
for i in hoge:
    for j in range(-len(i),0):
        NEXT[i[j]] = i[j+1]
i = 1
p = 0
while i < K:
    #print(i,p)
    if NEXT[p] <= p or NEXT[p] == N-1:
        i += 1
    p = (NEXT[p]+1)%N
    if p == 0:
        i = ((K-1)//(i-1))*(i-1)+1
while p < N:
    if NEXT[p] <= p:
        print(A[p],end = " ")
        p += 1
    else:
        p = NEXT[p]+1
print()

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]

s = []
for i in range(k):
    print(s)
    for num,j in enumerate(a):
        if j not in s:
            s.append(j)
        else:
            s = s[0:s.index(j)]
    if not s and i == 0:
        break
s = [str(i) for i in s]
print(' '.join(s))

Submission status: Wrong Answer

Solution code:
N=[int(n) for n in input().split()]
A=[int(n) for n in input().split()]
s=[]
for j in range(N[1]):
  for i in range(N[0]):
    if A[i] in s:
      while A[i] in s:
        s.pop(-1)
    else:
      s.append(A[i])
print(s)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
a_list = list(map(int, input().split()))
counts = dict()
if k == 1:
    search_list = a_list
    for a in a_list:
        counts.setdefault(a, 0)
        counts[a] += 1
else:
    search_list = a_list * 2
    for a in a_list:
        counts.setdefault(a, 0)
        counts[a] += 2

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: Wrong Answer

Solution code:
#!venv/bin/python
#from collections import defaultdict 
from itertools import combinations
from collections import defaultdict
from pprint import pprint
import math
import datetime
import sys
N, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

memo = defaultdict(dict)

head = None
cycle_start = cycle_end = None
for global_cnt in range(N * K):
    i = global_cnt % N
    if head is None:
        head = A[i]
        if not memo[i].get(A[i]) is None:
            cycle_start = memo[i].get(A[i])
            cycle_end = global_cnt
            break
        memo[i][A[i]] = global_cnt
        continue
    if head != A[i]:
        continue
    else:
        head = None

starting = 0
if cycle_end:
    cycle_term = cycle_end - cycle_start
    repeat = (N * K - cycle_start) // cycle_term
    starting = cycle_start + repeat * cycle_term

head = None
i = starting % N
last_empty_point = starting
for global_cnt in range(starting, N * K):
    i = global_cnt % N
    if head is None:
        head = A[i]
        last_empty_point = global_cnt
        continue
    if head != A[i]:
        continue
    else:
        head = None

nums = []
for global_cnt in range(last_empty_point, N * K):
    i = global_cnt % N
    if A[i] in nums:
        idx = nums.index(A[i])
        nums = nums[0:idx]
        continue
    nums.append(A[i])
print(nums)

Submission status: Wrong Answer

Solution code:
# import numpy as np
# np.set_printoptions(suppress=True)
# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())
import sys
sys.setrecursionlimit(10**7)
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
#debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)


def solve():
    N, K = LI()
    a_list = LI()

    pos_dict = {}
    for i, a in enumerate(a_list):
        if a not in pos_dict:
            pos_dict[a] = [i]
        else:
            pos_dict[a].append(i)

    from bisect import bisect_right

    # 開始のj
    head = 0
    irow = 0
    irowth_head = {}
    isloopstop = False

    while True:

        a = a_list[head]

        pos_list = pos_dict[a]
        # 次の位置
        idx = bisect_right(pos_list, head)


        if idx == len(pos_list):
            # これが末尾の場合、次の行の先頭へ
            # 次の行に行く前のこの行の last_head を記録
            irowth_head[irow] = head

            head = pos_list[0] + 1
            irow += 1


            # ループせず目的の行まで来たらいったんbreak
            if irow == K:
                break

        else:
            # 同じ行の場合は続ける
            head = pos_list[idx] + 1


        # 次のheadが先頭の場合、ループするので break
        if head == len(a_list):
            isloopstop = True
            break

    # 最終行は真面目にやる
    # ループしてストップした場合は head を取り直す
    if isloopstop == True:
        nrow_loop = irow + 1
        kthrow_in_loop = (K-1) - nrow_loop * (K // nrow_loop)
        if kthrow_in_loop == - 1:
            # ちょうどループ末尾の場合は出力しない
            print('')
            return
        head = irowth_head[kthrow_in_loop]
    else:
        head = irowth_head[K-1]

    # head からスタートして末尾まで入れていく
    ans = []
    ans_dict = {}
    for i, j in enumerate(range(head, N)):
        a = a_list[j]
        if a not in ans_dict:
            ans.append(a_list[j])
            ans_dict[a] = i
        else:
            last = ans_dict[a]
            for k in ans[last:]:
                del ans_dict[k]
            ans = ans[:last]


    print(' '.join([str(a) for a in ans]))

solve()