The only difference between easy and hard versions are constraints on $n$ and $k$.

You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $k$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $0$).

Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.

You (suddenly!) have the ability to see the future. You know that during the day you will receive $n$ messages, the $i$-th message will be received from the friend with ID $id_i$ ($1 \le id_i \le 10^9$).

If you receive a message from $id_i$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.

Otherwise (i.e. if there is no conversation with $id_i$ on the screen):

  Firstly, if the number of conversations displayed on the screen is $k$, the last conversation (which has the position $k$) is removed from the screen.  Now the number of conversations on the screen is guaranteed to be less than $k$ and the conversation with the friend $id_i$ is not displayed on the screen.  The conversation with the friend $id_i$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. 

Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $n$ messages.


-----Input-----

The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5)$ — the number of messages and the number of conversations your smartphone can show.

The second line of the input contains $n$ integers $id_1, id_2, \dots, id_n$ ($1 \le id_i \le 10^9$), where $id_i$ is the ID of the friend which sends you the $i$-th message.


-----Output-----

In the first line of the output print one integer $m$ ($1 \le m \le min(n, k)$) — the number of conversations shown after receiving all $n$ messages.

In the second line print $m$ integers $ids_1, ids_2, \dots, ids_m$, where $ids_i$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $i$ after receiving all $n$ messages.


-----Examples-----
Input
7 2
1 2 3 2 1 3 2

Output
2
2 1 

Input
10 4
2 3 3 1 1 2 1 2 3 3

Output
3
1 3 2 



-----Note-----

In the first example the list of conversations will change in the following way (in order from the first to last message):

  $[]$;  $[1]$;  $[2, 1]$;  $[3, 2]$;  $[3, 2]$;  $[1, 3]$;  $[1, 3]$;  $[2, 1]$. 

In the second example the list of conversations will change in the following way:

  $[]$;  $[2]$;  $[3, 2]$;  $[3, 2]$;  $[1, 3, 2]$;  and then the list will not change till the end.

Difficulty: EASY
Tags: Data structures

Implementation
Skill types: Data structures

Starter code:

Solutions:
(N, K) = map(int, input().split())
id_list = list(map(int, input().split()))
from collections import deque
que = deque()
used = set()
for id in id_list:
	if id in used:
		continue
	else:
		if len(que) >= K:
			q = que.pop()
			used.remove(q)
		que.appendleft(id)
		used.add(id)
print(len(que))
while len(que) != 0:
	print(que.popleft(), end=' ')


import heapq

def order_of_contacts(k, ids_arr):
	time = 0
	contacts = set()
	times = []
	for id in ids_arr:
		if id not in contacts:
			if len(contacts) == k:
				min_time_id = heapq.heappop(times)
				contacts.remove(min_time_id[1])
			contacts.add(id)
			heapq.heappush(times, (time, id))
			time += 1
	print(len(contacts))
	ids_by_order = []
	while times:
		min_time_id = heapq.heappop(times)
		ids_by_order.append(str(min_time_id[1]))
	ids_by_order.reverse()
	return ids_by_order

def get_input(flag=0):
	if flag:
		return int(input())
	return [int(i) for i in input().split()]
(n, k) = get_input()
arr = get_input()
print(' '.join(order_of_contacts(k, arr)))


from collections import defaultdict
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
s = []
x = 0
d = defaultdict(int)
for i in l:
	if d[i] == 0:
		s.append(i)
		d[i] = 1
		x += 1
	if x > k:
		q = s.pop(0)
		d[q] = 0
		x -= 1
print(x)
print(*s[::-1])


from sys import stdin, stdout
from collections import deque
(n, k) = map(int, stdin.readline().split())
lis = deque()
sett = set()
arr = list(map(int, stdin.readline().split()))
for j in arr:
	if j not in sett:
		lis.appendleft(j)
		sett.add(j)
		if len(lis) > k:
			sett.discard(lis[-1])
			del lis[-1]
print(len(lis))
print(*lis)


from collections import deque
(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
q = deque()
hash = {}
for i in range(len(a)):
	if hash.get(a[i]) == 1:
		continue
	if len(q) == k:
		hash.pop(q[len(q) - 1])
		q.pop()
	q.appendleft(a[i])
	hash.update({a[i]: 1})
print(len(q))
for i in range(len(q)):
	print(q[i], end=' ')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
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 list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

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 I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = 1
	rr = []
	for _ in range(t):
		(n, k) = LI()
		a = LI()
		r = []
		s = set()
		for c in a:
			if c in s:
				continue
			r.append(c)
			s.add(c)
			if len(s) > k:
				s.remove(r[-k - 1])
		rr.append(min(len(r), k))
		rr.append(JA(r[-k:][::-1], ' '))
	return JA(rr, '\n')
print(main())


import queue
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
b = {}
mes = queue.Queue(maxsize=k)
mes.put(l[0])
b[l[0]] = 1
j = 1
for i in range(1, n):
	if b.get(l[i], 0) == 0:
		if j < k:
			mes.put(l[i])
			j += 1
			b[l[i]] = 1
		else:
			var = mes.get()
			mes.put(l[i])
			b[l[i]] = 1
			b[var] = 0
print(mes.qsize())
length = mes.qsize()
for i in range(length - 1, -1, -1):
	print(mes.queue[i], end=' ')


(n, k) = [int(x) for x in input().split()]
messages = [int(x) for x in input().split()]
curr = {}
array = [0] * (n + 1)
no = 0
p = n
for i in range(n):
	if messages[i] in curr:
		continue
	elif no == k:
		x = array.pop()
		del curr[x]
		array[p] = messages[i]
		p -= 1
		curr[messages[i]] = 1
	else:
		no += 1
		array[p] = messages[i]
		p -= 1
		curr[messages[i]] = 1
print(len(array) - array.count(0))
for i in range(len(array)):
	if array[i] != 0:
		print(array[i], end=' ')


(n, k) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
dic = {}
calls = []
qtd = 0
for i in range(n):
	x = arr[i]
	if x not in dic or dic[x] < qtd - k:
		dic[x] = qtd
		calls += [x]
		qtd += 1
st = ''
for i in range(min(qtd, k)):
	st += str(calls[qtd - i - 1]) + ' '
print(min(qtd, k))
print(st)


(n, k) = map(int, input().split())
id_list = input().split()
stack = []
curr = {}
count = 1
for ids in id_list:
	if curr.get(ids, 0) < count:
		curr[ids] = count + k
		stack += [ids]
		count += 1
stack = stack[::-1][:k]
print(len(stack))
print(' '.join(stack))


(n, m) = map(int, input().split())
s = list(map(int, input().split()))
x = 0
st = set()
arr = []
for i in range(n):
	if s[i] not in st:
		if x == m:
			y = arr[-x]
			st.remove(y)
			st.add(s[i])
		else:
			st.add(s[i])
			x += 1
		arr.append(s[i])
l = min(x, m)
print(l)
a = arr[-l:][::-1]
print(*a)


(n, k) = [int(x) for x in input().split()]
idd = list(map(int, input().split()))
msg = []
co = 0
dic = {}
for i in idd:
	dic[i] = 0
ans = 0
x = 0
for i in range(n):
	if co == k:
		if dic[idd[i]] == 0:
			dic[msg[x]] = 0
			x -= -1
			msg.append(idd[i])
			dic[idd[i]] = 1
	elif dic[idd[i]] == 0:
		msg.append(idd[i])
		dic[idd[i]] = 1
		co -= -1
		ans -= -1
msg = msg[::-1]
print(ans)
print(*msg[:k])


import io
import os
from collections import deque
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, k) = map(int, input().split())
texts = [int(z) for z in input().split()]
screen = set()
queue = deque()
friends = []
for i in range(n):
	e = texts[i]
	if e in screen:
		continue
	screen.add(e)
	queue.appendleft(e)
	if len(screen) == k + 1:
		screen.remove(queue[-1])
		queue.pop()
print(len(queue))
print(*list(queue))


from collections import defaultdict as dfd
(N, Q) = map(int, input().split())
A = list(map(int, input().split()))
val = dfd(int)
B = []
for i in range(len(A)):
	if len(B) < Q and val[A[i]] != 1:
		B.append(A[i])
		val[A[i]] = 1
	elif val[A[i]] == 1:
		continue
	else:
		temp = B.pop(0)
		val[temp] = 0
		B.append(A[i])
		val[A[i]] = 1
print(len(B))
print(*B[::-1])


import sys
from heapq import heapify, heappop, heappush
from itertools import *
from collections import *
from math import *
sys.setrecursionlimit(10 ** 6)
input = lambda : sys.stdin.readline()
inp = lambda : int(input())
nm = lambda : map(int, input().split())
arr = lambda : list(nm())
INF = int(1e+18)
mod = int(1000000000.0) + 7

def solve():
	(n, m) = nm()
	lst = arr()
	d1 = deque()
	d2 = defaultdict(int)
	ln = 0
	for i in range(n):
		t = lst[i]
		if d2[t] == 0:
			d1.appendleft(t)
			d2[t] += 1
			ln += 1
		if ln > m:
			ln -= 1
			temp = d1.pop()
			d2[temp] = 0
	print(ln)
	print(*d1)
t = 1
for i in range(t):
	solve()


import sys
input = sys.stdin.readline
(n, k) = map(int, input().split())
A = list(map(int, input().split()))
SET = set()
from collections import deque
Q = deque()
for a in A:
	if a in SET:
		continue
	elif len(Q) == k:
		x = Q.pop()
		SET.remove(x)
		Q.appendleft(a)
		SET.add(a)
	else:
		SET.add(a)
		Q.appendleft(a)
print(len(Q))
print(*Q)


from collections import deque

def solution(arr, k: int) -> None:
	s = set()
	d = deque()
	for x in arr:
		if x not in s:
			if len(d) == k:
				s.discard(d.popleft())
			s.add(x)
			d.append(x)
	print(len(d))
	print(' '.join(map(str, reversed(d))))

def main():
	(_, k) = map(int, input().split())
	arr = map(int, input().split())
	solution(arr, k)
main()


from collections import deque
(n, k) = map(int, input().split())
l = 0
ids = list(map(int, input().split()))
q = deque()
val = set()
for i in ids:
	if not i in val:
		l += 1
		val.add(i)
		q.appendleft(i)
		if len(q) > k:
			l -= 1
			t = q.pop()
			val.discard(t)
print(l)
print(*q)


from collections import deque
(n, k) = list(map(int, input().split()))
s = set()
d = deque()
for i in map(int, input().split()):
	if i in s:
		continue
	if len(d) == k:
		s.discard(d.pop())
	d.appendleft(i)
	s.add(i)
print(len(d))
print(*d)


from sys import stdin as cin
(n, k) = list(map(int, input().split()))
(b, b1, m) = ([], set(), 0)
while n - 1:
	n -= 1
	a = ''
	s = cin.read(1)
	while s != ' ':
		a += s
		s = cin.read(1)
	if a not in b1:
		b.append(a)
		b1.add(a)
		if len(b1) > k:
			b1.discard(b[m])
			m += 1
a = ''
s = cin.read(1)
o = set(map(str, range(10)))
while s in o:
	a += s
	s = cin.read(1)
if a not in b1:
	b.append(a)
	b1.add(a)
	if len(b1) > k:
		b1.discard(b[m])
		m += 1
print(len(b) - m)
print(*b[m:][::-1])


(a, b) = map(int, input().split())
p = list(map(int, input().split()))
(x, l) = ([], set())
for i in p:
	if i not in l:
		l.add(i)
		x.append(i)
	if len(x) > b:
		j = x.pop(0)
		l.remove(j)
print(len(x))
print(*x[::-1])


from collections import deque
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
d = deque()
s = set()
for v in arr:
	if v in s:
		continue
	d.append(v)
	s.add(v)
	if len(d) > k:
		s.remove(d.popleft())
print(len(d))
print(*reversed(d))


(n, m) = map(int, input().split())
q = []
d = {}
a = list(map(int, input().split()))
for i in a:
	if i not in d:
		d[i] = 1
		if len(q) < m:
			q.append(i)
		else:
			z = q.pop(0)
			del d[z]
			q.append(i)
print(len(q))
print(*q[::-1])


from collections import deque

def solution(arr, k: int) -> None:
	d = deque()
	s = set()
	for v in arr:
		if v not in s:
			s.add(v)
			if len(d) == k:
				s.discard(d.pop())
			d.appendleft(v)
	print(len(d))
	print(*d)

def main():
	(_, k) = map(int, input().split())
	arr = map(int, input().split())
	solution(arr, k)
main()


from collections import OrderedDict
(n, size) = map(int, input().split())
window = OrderedDict()
arr = list(map(int, input().split()))
for i in range(n):
	if arr[i] not in window:
		if len(window) == size:
			window.popitem(last=False)
		window[arr[i]] = 1
show = []
for x in window:
	show.append(x)
print(len(show))
for i in range(len(show) - 1, -1, -1):
	print(show[i], end=' ')


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
a = []
d = {}
for i in range(n):
	if len(a) < k:
		if d.get(l[i], 0):
			pass
		else:
			a.append(l[i])
			d[l[i]] = 1
	elif d.get(l[i], 0):
		pass
	else:
		c = a.pop(0)
		a.append(l[i])
		d[c] = 0
		d[l[i]] = 1
print(len(a))
print(*a[::-1])


from collections import defaultdict
dic = defaultdict(lambda : 0)
(n, k) = map(int, input().split())
ar = list(map(int, input().split()))
arp = []
for nm in ar:
	if dic[nm] == 0 and len(arp) < k:
		dic[nm] = 1
		arp.append(nm)
	elif len(arp) >= k and dic[nm] == 0:
		dic[nm] = 1
		x = arp.pop(0)
		dic[x] = 0
		arp.append(nm)
print(len(arp))
print(*arp[::-1])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for i in range(len(a)):
	d[a[i]] = 0
l = 0
i = 0
st = []
last_pos = 0
for i in range(len(a)):
	if d[a[i]] == 0:
		if l < k:
			st.append(a[i])
			d[a[i]] = 1
			l += 1
		else:
			d[st[last_pos]] = 0
			d[a[i]] = 1
			st.append(a[i])
			last_pos += 1
st.reverse()
if len(st) > k:
	print(k)
else:
	print(len(st))
print(*st[:k])


(n, k) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
scr = []
s = set()
i = 0
for msg in l:
	if msg not in s:
		s.add(msg)
		scr.append(msg)
		if len(scr) > k:
			s -= {scr[i]}
			i += 1
print(len(scr) - i)
print(*reversed(scr[i:]))


from collections import deque
(n, k) = map(int, input().split())
A = map(int, input().split())
D = deque()
s = set()
for a in A:
	if a in s:
		continue
	if len(D) < k:
		s.add(a)
		D.appendleft(a)
	else:
		s.remove(D.pop())
		D.appendleft(a)
		s.add(a)
print(len(D))
print(' '.join(map(str, D)))


from collections import deque
(n, m) = map(int, input().split())
l = [int(i) for i in input().split()]
S = set()
que = deque()
for a in l:
	if a in S:
		continue
	if len(que) == m:
		b = que.popleft()
		S.remove(b)
	que.append(a)
	S.add(a)
print(len(que))
print(*reversed(que))


from collections import deque
(n, k) = map(int, input().split())
q = list(map(int, input().split()))
z = {}
w = deque()
for i in q:
	if not (i in z and z[i] == 1):
		z[i] = 1
		if len(w) < k:
			w.append(i)
		else:
			x = w.popleft()
			z[x] = -1
			w.append(i)
print(len(w))
print(*reversed(w))


a = list(map(int, input().split()))
sc = a[1]
m = a[0]
a = list(map(int, input().split()))
d = {}
s = []
for i in a:
	if i in d:
		continue
	if len(s) < sc:
		s.append(i)
		d[i] = 1
	else:
		d.pop(s.pop(0))
		s.append(i)
		d[i] = 1
print(len(s))
print(*s[::-1])


from collections import deque
(n, k) = [int(x) for x in input().split()]
msg = input().split()
resp = deque([])
oi = set([])
for e in msg:
	if e not in oi:
		resp.append(e)
		oi.add(e)
		if len(resp) > k:
			oi.discard(resp.popleft())
print(len(resp))
resp.reverse()
print(' '.join(resp))


import sys
input = sys.stdin.readline
from collections import defaultdict, deque
(n, k) = map(int, input().split())
ids = list(map(int, input().split()))
flag = defaultdict(int)
q = deque([])
for id_i in ids:
	if flag[id_i] == 0:
		q.appendleft(id_i)
		flag[id_i] = 1
		if len(q) > k:
			remove_id = q.pop()
			flag[remove_id] = 0
print(len(q))
print(*q)


from collections import deque, defaultdict
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
l = deque()
d = defaultdict(lambda : 0)
for i in range(n):
	if d[a[i]] == 0:
		if len(l) == k:
			x = l.pop()
			l.appendleft(a[i])
			d[x] = 0
			d[a[i]] = 1
		else:
			l.appendleft(a[i])
			d[a[i]] = 1
print(len(l))
print(*l)


from collections import deque
(n, k) = (int(i) for i in input().split(' '))
ids = [int(i) for i in input().split(' ')]
current_screen_list = deque()
current_screen_set = set()
for _id in ids:
	if _id not in current_screen_set:
		if len(current_screen_set) == k:
			last = current_screen_list.pop()
			current_screen_set.discard(last)
		current_screen_list.appendleft(_id)
		current_screen_set.add(_id)
print(len(current_screen_list))
print(' '.join([str(i) for i in current_screen_list]))


from collections import deque

def main():
	(n, k) = map(int, input().split(' '))
	gl = list(map(int, input().split(' ')))
	main_list = deque()
	s = set()
	for i in gl:
		if len(main_list) < k:
			if i in s:
				continue
			main_list.appendleft(i)
			s.add(i)
		elif len(main_list) == k:
			if i in s:
				continue
			s.discard(main_list.pop())
			main_list.appendleft(i)
			s.add(i)
	print(len(main_list))
	print(*main_list)
main()


from collections import deque
(n, k) = map(int, input().split())
ids = list(map(int, input().split()))
d = deque()
s = set()
for id in ids:
	if id not in s:
		s.add(id)
		if len(d) == k:
			s.remove(d.popleft())
			d.append(id)
		else:
			d.append(id)
d.reverse()
print(len(d))
print(' '.join(map(str, d)))


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = []
s = set()
j = 0
for i in a:
	if i not in s:
		s.add(i)
		ans.append(i)
		if len(ans) > k:
			s -= {ans[j]}
			j += 1
print(len(ans) - j)
print(*reversed(ans[j:]))


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
d = dict()
ids = []
count = 0
for i in l:
	if count < k:
		if i in d:
			continue
		else:
			d[i] = True
			ids.append(i)
			count += 1
	elif count == k:
		if i in d:
			continue
		elif i not in d:
			d[i] = True
			ids.append(i)
			z = ids.pop(0)
			del d[z]
ids = ids[::-1]
print(count)
for j in ids:
	print(j)


import sys
from collections import deque, defaultdict
(n, k) = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
dq = deque()
on_dq = defaultdict(int)
length = 0
for ele in a:
	if not on_dq[ele]:
		on_dq[ele] = 1
		length += 1
		dq.appendleft(ele)
		if length > k:
			item = dq.pop()
			on_dq[item] = 0
			length -= 1
print(length)
print(*dq)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
window = []
contained = set()
for i in a:
	if i in contained:
		continue
	else:
		if len(window) == k:
			contained.remove(window[0])
			del window[0]
		window.append(i)
		contained.add(i)
print(len(window))
for i in range(len(window) - 1, -1, -1):
	print(window[i], end=' ')
print()


(n, k) = map(int, input().split())
nums = list(map(int, input().split()))
s = list(set(sorted(nums)))
r = {s[i]: i for i in range(len(s))}
mark = []
for i in range(len(s)):
	mark.append(0)
vis = []
len = 0
l = 0
for num in nums:
	if mark[r[num]] == 0 and len < k:
		vis.append(num)
		mark[r[num]] = 1
		len += 1
	elif mark[r[num]] == 0 and len == k:
		vis.append(num)
		mark[r[num]] = 1
		mark[r[vis[l]]] = 0
		l += 1
print(min(len, k))
for i in range(l + len - 1, l - 1, -1):
	print(vis[i], end=' ')


from queue import deque
(n, k) = [int(x) for x in input().split()]
idi = [int(x) for x in input().split()]
q = deque()
container = set()
for i in range(n):
	x = idi[i]
	if x in container:
		continue
	else:
		if len(q) == k:
			container.remove(q.popleft())
		q.append(x)
		container.add(x)
q.reverse()
print(len(q))
print(' '.join([str(x) for x in q]))


from collections import deque
screen = deque()
items = set()
(n, k) = list(map(int, input().split()))
msgs = list(map(int, input().split()))

def add(i):
	if i not in items:
		if len(screen) == k:
			items.remove(screen.pop())
		screen.appendleft(i)
		items.add(i)
for m in msgs:
	add(m)
print(len(screen))
print(' '.join((str(i) for i in screen)))


from collections import deque
(n, k) = map(int, input().split())
q = deque()
a = list(map(int, input().split()))
vis = set()
for i in range(n):
	if len(q) < k:
		if a[i] not in vis:
			q.append(a[i])
			vis.add(a[i])
	elif a[i] not in vis:
		q.append(a[i])
		vis.remove(q.popleft())
		vis.add(a[i])
print(len(q))
print(str(list(q)[::-1]).replace('[', '').replace(']', '').replace(', ', ' '))


from collections import deque
ints = list(map(int, input().split()))
(n, k) = (ints[0], ints[1])
conversations = list(map(int, input().split()))
screen = deque()
cache = {}
for i in range(len(conversations)):
	if conversations[i] in cache:
		continue
	if len(screen) >= k:
		remove = screen.popleft()
		cache.pop(remove)
		screen.append(conversations[i])
		cache[conversations[i]] = 1
	else:
		screen.append(conversations[i])
		cache[conversations[i]] = 1
print(len(screen))
screen.reverse()
print(*screen)


from queue import Queue

def main():
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	checked = {}
	for a in A:
		checked[a] = False
	q = Queue()
	for (i, a) in enumerate(A):
		if checked[a]:
			continue
		else:
			checked[a] = True
			q.put(a)
			if q.qsize() > K:
				b = q.get()
				checked[b] = False
	L = q.qsize()
	ans = []
	for i in range(L):
		a = q.get()
		ans.append(a)
	print(L)
	print(' '.join([str(a) for a in ans[::-1]]))

def __starting_point():
	main()
__starting_point()


from sys import stdin
from collections import deque
import math
(n, k) = list(map(int, stdin.readline().split()))
arr = list(map(int, stdin.readline().split()))
myq = deque([])
myset = set()
for i in range(n):
	if arr[i] in myset:
		pass
	else:
		if len(myq) == k:
			el = myq[-1]
			myset.remove(el)
			myq.pop()
		myq.appendleft(arr[i])
		myset.add(arr[i])
print(len(myq))
print(*myq)


from collections import deque
(n, k) = list(map(int, input().split()))
lst = list(map(int, input().split()))
q = deque()
s = set()
for i in range(n):
	if lst[i] not in s:
		if len(q) == k:
			s.remove(q.popleft())
			q.append(lst[i])
			s.add(lst[i])
		else:
			q.append(lst[i])
			s.add(lst[i])
res = []
num = len(q)
print(num)
q.reverse()
print(*q)


(a, b) = map(int, input().split())
l = list(map(int, input().split()))
x = 0
shan = set()
sree = []
for i in range(a):
	if l[i] not in shan:
		if x == b:
			y = sree[-x]
			shan.remove(y)
			shan.add(l[i])
		else:
			shan.add(l[i])
			x += 1
		sree.append(l[i])
m = min(x, b)
print(m)
shan = sree[-m:][::-1]
print(*shan)


from collections import deque
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
messages = deque([])
check = set()
for i in range(n):
	if len(messages) < k:
		if arr[i] not in check:
			messages.append(arr[i])
			check.add(arr[i])
	elif arr[i] not in check:
		elem = messages.popleft()
		check.remove(elem)
		messages.append(arr[i])
		check.add(arr[i])
print(len(messages))
for i in range(len(messages) - 1, -1, -1):
	print(messages[i], end=' ')


from sys import stdin, stdout
from collections import *
from math import gcd, floor, ceil
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	(n, k) = mp()
	a = li()
	q = [a[0]]
	q = deque(q)
	d = {a[0]: 1}
	for i in range(1, n):
		if a[i] not in d:
			if len(q) == k:
				del d[q.popleft()]
				q.append(a[i])
				d[a[i]] = 1
			else:
				q.append(a[i])
				d[a[i]] = 1
	print(len(q))
	q = list(q)
	print(*q[::-1])
for _ in range(1):
	solve()


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
from collections import deque
from collections import defaultdict
b = deque()
c = defaultdict(int)
for i in a:
	c[i] = 0
l = 0
i = 0
while i < n:
	if l < k:
		if c[a[i]] == 1:
			i = i + 1
			continue
		else:
			b.appendleft(a[i])
			c[a[i]] = 1
			l = l + 1
			i = i + 1
	elif c[a[i]] == 1:
		i = i + 1
		continue
	else:
		e = b.pop()
		c[e] = 0
		c[a[i]] = 1
		b.appendleft(a[i])
		i = i + 1
print(len(b))
print(*b)


from collections import deque
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
cur_friends = set()
queue = deque()
for i in a:
	if len(queue) < k and i not in cur_friends:
		queue.append(i)
		cur_friends.add(i)
	elif i not in cur_friends:
		cur_friends.remove(queue.popleft())
		cur_friends.add(i)
		queue.append(i)
queue.reverse()
print(len(queue))
print(' '.join(list(map(str, queue))))


(n, k) = list(map(int, input().split(' ')))
lastSeen = {}
curr = 0
arr = list(map(int, input().split(' ')))
for i in arr:
	if i in lastSeen:
		if curr - lastSeen[i] > k:
			lastSeen[i] = curr
			curr += 1
	else:
		lastSeen[i] = curr
		curr += 1
import operator
sorted_x = sorted(lastSeen.items(), key=operator.itemgetter(1), reverse=True)
ans = list(map(lambda x: str(x[0]), sorted_x))
ans = ans[:k]
print(len(ans))
print(' '.join(ans))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections
(n, k) = map(int, input().split())
vals = list(map(int, input().split()))
dis = collections.deque([])
cur = set([])
for s in range(n):
	if len(cur) < k:
		if not vals[s] in cur:
			cur.add(vals[s])
			dis.appendleft(vals[s])
		else:
			continue
	elif not vals[s] in cur:
		v0 = dis.pop()
		cur.remove(v0)
		cur.add(vals[s])
		dis.appendleft(vals[s])
	else:
		continue
print(len(dis))
print(*dis)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
(b, b1, m) = ([], set(), 0)
for i in a:
	if i not in b1:
		b.append(i)
		b1.add(i)
		if len(b1) > k:
			b1 -= {b[m]}
			m += 1
print(len(b) - m)
print(*reversed(b[m:]))


from collections import deque
(n, k) = map(int, input().split())
a = [int(x) for x in input().split()]
s = deque()
d = dict()
for i in range(len(a)):
	if a[i] not in d:
		f = False
	else:
		f = d[a[i]] == 1
	if len(s) == k and (not f):
		z = s.pop()
		d[z] = 0
	if not f:
		d[a[i]] = 1
		s.appendleft(a[i])
print(len(s))
print(*s)


import heapq
import math
import sys
input = sys.stdin.readline

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def value():
	return int(input())
(n, k) = li()
a = li()
curr = []
currd = {}
currd2 = {}
for i in range(n):
	if a[i] not in currd2:
		if len(curr) == k:
			currd2.pop(currd.pop(heapq.heappop(curr)))
		heapq.heappush(curr, i)
		currd[i] = a[i]
		currd2[a[i]] = i
curr.sort(reverse=1)
ans = []
for i in curr:
	ans.append(currd[i])
print(len(ans))
print(*ans)


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 1000010
MOD = 10 ** 9 + 7
spf = [i for i in range(MAXN)]

def sieve():
	for i in range(2, MAXN, 2):
		spf[i] = 2
	for i in range(3, int(MAXN ** 0.5) + 1):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def fib(n, m):
	if n == 0:
		return [0, 1]
	else:
		(a, b) = fib(n // 2)
		c = a % m * (b % m * 2 - a % m) % m
		d = a % m * (a % m) % m + b % m * b % m % m
		if n % 2 == 0:
			return [c, d]
		else:
			return [d, c + d]

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def ncr(n, r):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % MOD
		den = den * (i + 1) % MOD
	return num * pow(den, MOD - 2, MOD) % MOD

def flush():
	return sys.stdout.flush()
(n, k) = arrIN()
a = arrIN()
st = []
ls = 0
f = defaultdict(int)
j = 0
for i in a:
	if f[i] == 0:
		if ls < k:
			st.append(i)
			f[i] = 1
			ls += 1
		else:
			f[st[j]] = 0
			j += 1
			st.append(i)
			f[i] = 1
print(len(st) - j)
for i in range(len(st) - 1, j - 1, -1):
	print(st[i], end=' ')


from collections import deque
(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
now = set()
letters = deque()
for i in range(n):
	if a[i] not in now:
		now.add(a[i])
		if len(letters) < k:
			letters.append(a[i])
		else:
			now.remove(letters.popleft())
			letters.append(a[i])
print(len(letters))
print(*list(letters)[::-1])


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
dic = {}
out = list()
l = 0
for i in range(n):
	if dic.get(arr[i], 0) == 0:
		dic[arr[i]] = 1
		out.append(arr[i])
		if len(out) > k:
			x = out.pop(0)
			dic[x] = 0
print(len(out))
print(*out[::-1])


import sys
input = sys.stdin.readline
from collections import *
(n, k) = list(map(int, input().split()))
ids = list(map(int, input().split()))
display = deque()
friends = set()
for id in ids:
	if id not in friends:
		if len(display) == k:
			out = display.pop()
			friends.remove(out)
		display.appendleft(id)
		friends.add(id)
print(len(display))
print(*display)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
lt = dict()
timer = 0
for id in a:
	if id not in lt:
		timer += 1
		lt[id] = timer
	cur_time = lt[id]
	if cur_time <= timer - k:
		timer += 1
		lt[id] = timer
screen = list(lt.items())
screen.sort(key=lambda t: -t[1])
screen = screen[:min(n, k)]
print(len(screen))
for v in screen:
	print(v[0], end=' ')
print()


from collections import deque
(n, k) = map(int, input().split())
data = list(map(int, input().split()))
m = set()
q = deque()
for i in data:
	if i not in m:
		if len(q) >= k:
			m.remove(q.pop())
		q.appendleft(i)
		m.add(i)
print(len(q))
print(*q)


import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
(n, k) = mapin()
a = listin()
s = set([])
d = deque([])
for i in a:
	if len(d) < k:
		if i not in s:
			s.add(i)
			d.append(i)
	elif i not in s:
		s.remove(d.popleft())
		d.append(i)
		s.add(i)
print(len(d))
d = list(d)
d.reverse()
print(*d)


import collections
(a, b) = map(int, input().split())
ar = list(map(int, input().split()))
s = set()
ar1 = collections.deque([])
for i in ar:
	if i not in s:
		s.add(i)
		ar1.appendleft(i)
		if len(s) > b:
			s.remove(ar1[-1])
			ar1.pop()
print(len(s))
print(*ar1)


from collections import deque
(n, k) = input().split(' ')
n = int(n)
k = int(k)
c = 0
d = [int(x) for x in input().split(' ')]
e = deque()
f = set()
for i in d:
	if i in f:
		pass
	else:
		e.append(i)
		f.add(i)
		c += 1
	if c > k:
		c -= 1
		x = e.popleft()
		f.remove(x)
print(len(e))
for i in reversed(e):
	print(i)


from collections import deque

def main():
	(N, K, *A) = map(int, open(0).read().split())
	(length, in_q, q) = (0, set(), deque([]))
	for new in A:
		if new in in_q:
			continue
		length += 1
		(in_q.add(new), q.appendleft(new))
		if length > K:
			length -= 1
			removed = q.pop()
			in_q.discard(removed)
	print(length)
	print(' '.join(map(str, q)))
main()


from collections import deque
q = deque()
(n, k) = map(int, input().split())
l = 0
s = set()
for i in map(int, input().split()):
	if i not in s:
		if l == k:
			s.discard(q.popleft())
		else:
			l += 1
		q.append(i)
		s.add(i)
print(l)
print(*list(q)[::-1])


from collections import deque
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = dict()
for i in range(n):
	d[a[i]] = 0
ans = deque()
for i in range(n):
	if d[a[i]] == 0:
		ans.appendleft(a[i])
		if len(ans) > k:
			d[ans.pop()] = 0
		d[a[i]] = 1
print(len(ans))
print(' '.join(map(str, ans)))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = []
s = set()
for i in a:
	if i not in s:
		d.append(i)
		s.add(i)
	if len(d) > k:
		s.remove(d.pop(0))
print(len(d))
print(*d[::-1])


x = input().split()
arr = input().split()
queue = []

def search(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return search(arr, l, mid - 1, x)
		else:
			return search(arr, mid + 1, r, x)
	else:
		return -1
d = {}
for a in range(0, len(arr)):
	arr[a] = int(arr[a])
	if not arr[a] in d:
		d[arr[a]] = False
	if len(queue) < int(x[1]) and arr[a] in d and (not d[arr[a]]):
		queue.append(arr[a])
		d[arr[a]] = True
	elif arr[a] in d and (not d[arr[a]]) and (len(queue) == int(x[1])):
		d[queue[0]] = False
		del queue[0]
		queue.append(arr[a])
		d[arr[a]] = True
queue.reverse()
print(len(queue))
st = ''
for each in queue:
	st += str(each) + ' '
print(st[:-1])


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
import queue
s = set()
q = queue.Queue()
count = 0
for x in a:
	if x not in s:
		if count < k:
			q.put(x)
			s.add(x)
			count += 1
		else:
			s.remove(q.get())
			q.put(x)
			s.add(x)
ans = list()
for _ in range(count):
	ans.append(q.get())
print(count)
print(' '.join(map(str, ans[::-1])))


from collections import defaultdict, deque
(n, k) = list(map(int, input().split()))
arr = list(map(int, input().split()))
ans = deque([])
d = {}
uni1 = list(set(arr))
for i in uni1:
	d[i] = 0
for i in arr:
	if d[i] == 0:
		if len(ans) == k:
			d[ans[-1]] = 0
			ans.pop()
			ans.appendleft(i)
			d[i] = 1
		else:
			ans.appendleft(i)
			d[i] = 1
print(len(ans))
ans = list(ans)
print(*ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
st = []
d = {}
for i in a:
	if d.get(i, 0) == 0:
		st.append(i)
		d[i] = 1
	if len(st) > k:
		d.pop(st.pop(0))
st = st[::-1]
print(len(st))
print(*st)


from collections import deque
(n, k) = map(int, input().split())
arr = map(int, input().split())
convos = deque()
s = set()
for v in arr:
	if v in s:
		continue
	if len(s) == k:
		s.remove(convos.pop())
	convos.appendleft(v)
	s.add(v)
print(len(convos))
print(' '.join(map(str, list(convos))))


import sys
from collections import deque

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
(N, K) = MAP()
A = LIST()
que = deque()
S = set()
for a in A:
	if len(que) < K:
		if a not in S:
			que.appendleft(a)
			S.add(a)
	elif a not in S:
		b = que.pop()
		S.remove(b)
		que.appendleft(a)
		S.add(a)
print(len(que))
print(*que)


def solve(n, k, msgs):
	sc_dic = {}
	sc = []
	for m in msgs:
		if m not in sc_dic:
			sc_dic[m] = 1
			sc.append(m)
		if len(sc) > k:
			sc_dic.pop(sc.pop(0))
	sc = sc[::-1]
	print(len(sc))
	for a in sc:
		print(a, end=' ')

def main():
	(n, k) = map(int, input().split())
	msgs = list(map(int, input().split()))
	solve(n, k, msgs)
main()


from collections import defaultdict as dfd
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
mp = dfd(int)
indices = dfd(int)
size = 0
least_val = 1
for i in range(n):
	if size < k:
		if mp[arr[i]] == 0:
			size += 1
			mp[arr[i]] = size
			indices[size] = arr[i]
	if size >= k and mp[arr[i]] == 0:
		element_to_remove = indices[least_val]
		mp[element_to_remove] = 0
		least_val += 1
		size += 1
		mp[arr[i]] = size
		indices[size] = arr[i]
arr = []
for (i, j) in mp.items():
	if j > 0:
		arr.append((j, i))
arr.sort(reverse=True)
ans = []
for _ in range(len(arr)):
	ans.append(arr[_][1])
print(len(ans))
print(*ans)


def solve(nums, k):
	l = list()
	h = set()
	for j in nums:
		if j not in h:
			if len(l) == k:
				h.remove(l[0])
				l.pop(0)
			l.append(j)
			h.add(j)
	return l

def main():
	(_, k) = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	sol = solve(nums, k)
	print(len(sol))
	for i in reversed(sol):
		print(i, end=' ')
main()


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
y = []
x = set()
for i in l:
	if i not in x:
		x.add(i)
		y.append(i)
		if len(x) > k:
			x.remove(y[0])
			y.pop(0)
print(len(x))
print(*y[::-1])


from collections import deque
import sys
import heapq

def inp():
	return sys.stdin.readline().strip()
(n, k) = map(int, inp().split())
a = list(map(int, inp().split()))
mintime = 1
cache = {}
inverse = {}
time = 1
for i in a:
	if i in cache:
		continue
	cache[i] = time
	inverse[time] = i
	time += 1
	k -= 1
	while k < 0:
		if mintime not in inverse:
			mintime += 1
			continue
		if cache[inverse[mintime]] == mintime:
			k += 1
			j = inverse[mintime]
			del inverse[mintime]
			del cache[j]
		else:
			del inverse[mintime]
		mintime += 1
res = []
for i in range(mintime, time + 1):
	if i in inverse:
		if cache[inverse[i]] == i:
			res.append((i, inverse[i]))
res.sort()
res = [i[1] for i in res]
res = res[::-1]
print(len(res))
print(*res)


import queue
D = dict()
(N, K) = map(int, input().split())
arr = list(map(int, input().split()))
Q = queue.Queue(maxsize=K + 1)
size_of_Q = 0
for i in range(N):
	if arr[i] not in D:
		D[arr[i]] = 1
		Q.put(arr[i])
		size_of_Q += 1
		if size_of_Q > K:
			D[Q.get()] = 0
			size_of_Q -= 1
	elif D[arr[i]] == 0:
		D[arr[i]] = 1
		Q.put(arr[i])
		size_of_Q += 1
		if size_of_Q > K:
			D[Q.get()] = 0
			size_of_Q -= 1
ans = [Q.get() for i in range(size_of_Q)]
ans = list(reversed(ans))
print(size_of_Q)
print(' '.join(list(map(str, ans))))


(c, l) = map(int, input().split())
arr = [int(x) for x in input().split()]
q = list()
s = set()
k = 0
for i in arr:
	if i not in s:
		if len(s) == l:
			s -= {q[k]}
			k += 1
		s.add(i)
		q.append(i)
print(len(q) - k)
print(*reversed(q[k:]))


from collections import deque

def solve(n, k, arr):
	count = 0
	screen = deque([])
	dict = {}
	for i in arr:
		if dict.get(i, 0) == 0:
			if count == k:
				dict[screen.pop()] = 0
				count -= 1
			screen.appendleft(i)
			dict[i] = 1
			count += 1
	return str(len(screen)) + '\n' + ' '.join((str(x) for x in list(screen)))
output = ''
(n, k) = map(int, input().split(' '))
arr = [int(x) for x in input().split(' ')]
output += solve(n, k, arr)
print(output)


import heapq

def read_str():
	return list(map(int, input().split(' ')))
(n, k) = read_str()
id = read_str()
screen = set()
li = []
heapq.heapify(li)
for i in range(n):
	if id[i] not in screen:
		screen.add(id[i])
		heapq.heappush(li, i)
	if len(li) > k:
		j = heapq.heappop(li)
		screen.remove(id[j])
print(len(li))
print(*[id[heapq.heappop(li)] for _ in range(len(li))][::-1])


from queue import Queue
from collections import defaultdict
(m, k) = map(int, input().split())
Messages = [int(x) for x in input().split()]
q = Queue(maxsize=k)
Dict = defaultdict(bool)
for i in range(m):
	if q.full():
		if Dict[Messages[i]] == False:
			Dict[q.get()] = False
			q.put(Messages[i])
			Dict[Messages[i]] = True
	elif Dict[Messages[i]] == False:
		q.put(Messages[i])
		Dict[Messages[i]] = True
print(q.qsize())
List = []
for i in range(q.qsize()):
	List.append(q.get())
print(*List[::-1])


from collections import defaultdict as dc
from collections import deque as dq
import sys
import math
mod = 10 ** 9 + 7

def inp():
	p = sys.stdin.readline()
	return p
(n, k) = list(map(int, inp().split()))
a = list(map(int, inp().split()))
q = dc(int)
p = dq([])
i = 0
while len(p) <= k or i < n:
	if q[a[i]] == 0:
		p.appendleft(a[i])
		q[a[i]] = 1
		if len(p) == k:
			break
	i += 1
	if i >= n:
		break
if i >= n:
	print(len(p))
	print(*p)
else:
	for j in range(i + 1, n):
		if q[a[j]] == 0:
			z = p.pop()
			p.appendleft(a[j])
			q[a[j]] = 1
			q[z] = 0
	print(len(p))
	print(*p)


import collections

def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, k) = mi()
d = li()
b = collections.deque([])
s = set(d)
l1 = list(s)
di = {}
for i in range(len(l1)):
	di[l1[i]] = 0
for i in range(n):
	if len(b) < k and di[d[i]] != 1:
		b.appendleft(d[i])
		di[d[i]] = 1
	elif di[d[i]] != 1:
		di[b[-1]] = 0
		b.pop()
		b.appendleft(d[i])
		di[d[i]] = 1
l = list(b)
print(len(l))
for i in l:
	print(i, end=' ')


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
(n, k) = mi()
a = li()
v = set()
p = deque([])
x = 0
for i in range(n):
	if a[i] not in v:
		p.append(a[i])
		v.add(a[i])
		x += 1
		if x > k:
			z = p.popleft()
			v.discard(z)
			x -= 1
p = list(p)
print(x)
print(*p[::-1])


import sys, collections
(n, k) = map(int, sys.stdin.readline().split())
q = collections.deque()
arr = list(map(int, sys.stdin.readline().split()))
check = dict()
size = 0
for i in arr:
	if i not in check:
		check[i] = False
for i in arr:
	if size == 0:
		size += 1
		check[i] = True
		q.appendleft(i)
	elif check[i]:
		continue
	elif size + 1 > k:
		check[q[-1]] = False
		q.pop()
		check[i] = True
		q.appendleft(i)
	else:
		check[i] = True
		q.appendleft(i)
		size += 1
print(size)
print(' '.join(map(str, q)))


from collections import deque
(n, k) = map(int, input().split())
A = list(map(int, input().split()))
B = deque()
B.append(A[0])
nc = {}
for i in set(A):
	nc[i] = 0
nc[A[0]] = 1
for i in range(1, n):
	if nc[A[i]] == 0:
		if len(B) == k:
			nc[B[0]] -= 1
			B.popleft()
		B.append(A[i])
		nc[A[i]] += 1
	else:
		pass
print(len(B))
print(*list(B)[::-1])


import collections
(n, k) = map(int, input().split())
A = [*map(int, input().split())]
order = collections.deque()
hashset = set()
for a in A:
	if a in hashset:
		pass
	else:
		order.appendleft(a)
		hashset.add(a)
		if len(order) > k:
			b = order.pop()
			hashset.remove(b)
print(len(order))
print(' '.join(map(str, order)))


from collections import deque
(n, k) = [int(x) for x in input().split()]
vals = [int(x) for x in input().split()]
S = set()
Q = deque()
for ids in vals:
	if not ids in S:
		S.add(ids)
		Q.appendleft(ids)
		if len(Q) > k:
			S.remove(Q.pop())
print(len(Q))
print(*Q)


import collections
(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
ans = []
s = set(a)
d = collections.Counter(s)
for i in d:
	d[i] -= 1
if k >= n or len(set(a)) <= k:
	for i in range(0, n):
		if d[a[i]] == 0:
			ans.append(a[i])
		d[a[i]] += 1
	print(len(ans))
	ans = ans[::-1]
	print(*ans)
else:
	i = 0
	while len(ans) != k:
		if d[a[i]] == 0:
			ans.append(a[i])
		d[a[i]] += 1
		i += 1
	for j in range(i, n):
		if d[a[j]] == 0:
			d[ans[0]] = 0
			ans.pop(0)
			ans.append(a[j])
		d[a[j]] += 1
	print(len(ans))
	ans = ans[::-1]
	print(*ans)


from collections import deque
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = set()
b = deque([], k)
for i in a:
	if i not in s:
		if len(b) >= k:
			x = b.popleft()
			s.remove(x)
		b.append(i)
		s.add(i)
print(len(b))
print(*list(b)[::-1])


ent = input().split(' ')
n = int(ent[0])
k = int(ent[1])
atual = -1
tela = set()
tela2 = []
menssagens = input().split(' ')
for i in range(n):
	atual = int(menssagens[i])
	grupo = set()
	grupo.add(atual)
	if grupo.isdisjoint(tela):
		if len(tela) < k:
			tela.add(atual)
			tela2.append(atual)
		else:
			remover = tela2[0]
			tela2.remove(remover)
			tela.remove(remover)
			tela.add(atual)
			tela2.append(atual)
t = len(tela2)
print(t)
for u in range(t):
	if u != t - 1:
		print(tela2[t - 1 - u], end=' ')
	else:
		print(tela2[t - 1 - u])


(n, k) = map(int, input().split())
d = {}
ls = []
for x in list(map(int, input().split())):
	if d.get(x) == None:
		d[x] = 0
		ls.append(x)
		if len(ls) > k:
			d[ls[0]] = None
			del ls[0]
print(len(ls))
ls.reverse()
print(' '.join(map(str, ls)))


from collections import deque
(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
b = deque([0] * k)
c = set()
nul = k
for i in a:
	if i not in c:
		if b[-1] != 0:
			c.remove(b[-1])
		else:
			nul -= 1
		b.appendleft(i)
		b.pop()
		c.add(i)
print(k - nul)
for i in b:
	if i == 0:
		break
	print(i, end=' ')


from collections import OrderedDict
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = OrderedDict()
arr = [-1] * 800001
start = -1
end = -1
for i in a:
	if i in d:
		pass
	else:
		d[i] = 1
		if start == end and start == -1:
			start += 1
			end += 1
			arr[start] = i
		elif end - start + 1 < k:
			end += 1
			arr[end] = i
		else:
			del d[arr[start]]
			start += 1
			end += 1
			arr[end] = i
print(end - start + 1)
for i in range(end, start - 1, -1):
	print(arr[i], end=' ')


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = []
c = set()
for i in a:
	if i not in c:
		b.append(i)
		c.add(i)
		if len(b) > k:
			c.remove(b[0])
			del b[0]
b.reverse()
print(len(b))
print(*b)


from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10 ** 7)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
(n, k) = lin()
a = lin()
ans = []
ch = 0
ch1 = 0
sa = set()
for i in a:
	if i not in sa:
		ans.append(i)
		if ch >= k:
			sa.remove(ans[-k - 1])
		else:
			ch += 1
		sa.add(i)
	ch1 += 1
l = len(ans)
sol = ans[max(l - k, 0):][::-1]
print(len(sol))
print(*sol)


from sys import stdin
input = stdin.readline
from collections import deque
(n, k) = map(int, input().split())
arr = map(int, input().split())
dd = deque()
ss = set()
for v in arr:
	if v not in ss:
		if len(dd) >= k:
			ss.remove(dd.pop())
		ss.add(v)
		dd.appendleft(v)
print(len(dd))
print(' '.join((str(v) for v in dd)))


from collections import deque

class Queue:

	def __init__(self, size):
		self.flag = False
		self.queue = deque(maxlen=size)
		self.items_set = set()
		self.count = 0

	def push(self, item):
		if item not in self.items_set:
			self.items_set.add(item)
			if self.count == self.queue.maxlen:
				last_item = self.queue[-1]
				self.items_set.remove(last_item)
			if self.count < self.queue.maxlen:
				self.count += 1
			self.queue.appendleft(item)
			self.flag = True
(_, k) = map(int, input().split())
ids = input().split()
q = Queue(size=k)
for id in ids:
	q.push(id)
print(len(q.queue))
print(' '.join(q.queue))


from collections import deque
(n, k) = [int(x) for x in input().split()]
a = [int(j) for j in input().split()]
s = set()
q = deque()
for x in a:
	if x in s:
		continue
	else:
		q.appendleft(x)
		s.add(x)
		if len(q) > k:
			t = q.pop()
			s.remove(t)
print(len(q))
for x in q:
	print(x, end=' ')


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
(n, k) = f()
l = il()
y = 0
x = 0
dq = deque()
s = set()
for i in l:
	if not i in s:
		if len(dq) == k:
			s.discard(dq[-1])
			dq.pop()
		dq.appendleft(i)
		s.add(i)
print(len(dq))
for i in dq:
	print(i, end=' ')


from collections import deque

def soln() -> None:

	def logic() -> None:
		(n, k) = [int(x) for x in input().split()]
		arr = [int(x) for x in input().split()]
		s = set()
		q = deque(maxlen=k)
		for i in arr:
			if i not in s:
				if len(q) == k:
					s.remove(q[-1])
				q.appendleft(i)
				s.add(i)
		print(len(q))
		for i in q:
			print(i, end=' ')
		print()
	logic()
soln()


(n, k) = [int(j) for j in input().split()]
arr = [int(j) for j in input().split()]
b = []
d = dict()
for i in range(len(arr)):
	d[arr[i]] = 0
for i in range(len(arr)):
	if len(b) < k and d[arr[i]] == 0:
		b.append(arr[i])
		d[arr[i]] = 1
	elif d[arr[i]] == 0:
		d[b[0]] = 0
		d[arr[i]] = 1
		b.pop(0)
		b.append(arr[i])
print(len(b))
b.reverse()
print(*b, sep=' ')


from collections import deque
lis = deque()
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
dic = {}
for i in arr:
	dic[i] = 0
for i in arr:
	if dic[i] == 0:
		if len(lis) == k:
			f = lis.popleft()
			dic[f] = 0
		lis.append(i)
		dic[i] = 1
result = list(lis)
print(len(result))
print(*result[::-1])


import sys, bisect
from collections import deque

def main():

	def input():
		return sys.stdin.readline()[:-1]
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	u = set()
	l = deque()
	for e in a:
		if len(l) == k:
			if e not in u:
				l.appendleft(e)
				t = l.pop()
				u.remove(t)
				u.add(e)
			else:
				pass
		elif e not in u:
			l.appendleft(e)
			u.add(e)
		else:
			pass
	print(len(l))
	print(*l, sep=' ')
main()


import heapq
(n, k) = (int(i) for i in input().split())
a = list(map(int, input().split()))
ans = []
heapq.heapify(ans)
s = set()
for (i, j) in enumerate(a):
	if j in s:
		continue
	else:
		s.add(j)
		if len(ans) < k:
			heapq.heappush(ans, (i, j))
		else:
			temp = heapq.heappop(ans)
			s.remove(temp[1])
			heapq.heappush(ans, (i, j))
			s.add(j)
ans2 = []
flag = False
while len(ans) != 0:
	ans2.append(heapq.heappop(ans)[1])
print(len(ans2))
for i in range(len(ans2) - 1, -1, -1):
	print(ans2[i], end=' ')
	flag = True
if flag:
	print()


from collections import deque
se = set([])
s = deque([])
(n, k) = map(int, input().split())
li = list(map(int, input().split()))
cnt = 0
for i in li:
	if i in se:
		continue
	if cnt < k:
		s.append(i)
		se.add(i)
		cnt += 1
	else:
		s.append(i)
		se.add(i)
		se.discard(s[0])
		s.popleft()
print(len(s))
print(*list(s)[::-1])


from collections import deque
(a, b) = list(map(int, input().split()))
array = list(map(int, input().split()))
hashmap = {}
arr = deque()
current = 0
x = 0
while 6 == 6:
	if x == a:
		break
	if array[x] in hashmap:
		pass
	elif current < b:
		hashmap[array[x]] = 5
		arr.appendleft(array[x])
		current += 1
	else:
		if hashmap != {}:
			hashmap.pop(arr[-1])
		arr.pop()
		arr.appendleft(array[x])
		hashmap[array[x]] = 5
	x += 1
print(len(arr))
print(*list(arr))


from collections import deque
(a, b) = map(int, input().split())
li = list(map(int, input().split()))
s = set()
l = deque([])
le = 0
for i in li:
	if i in s:
		pass
	elif le < b:
		le += 1
		s.add(i)
		l.appendleft(i)
	else:
		s.remove(l.pop())
		s.add(i)
		l.appendleft(i)
print(len(l))
print(*list(l))


import sys
input = sys.stdin.readline
from collections import deque
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
q = [a[0]]
q = deque(q)
d = {a[0]: 1}
for i in range(1, n):
	if a[i] not in d:
		if len(q) == k:
			del d[q.popleft()]
			q.append(a[i])
			d[a[i]] = 1
		else:
			q.append(a[i])
			d[a[i]] = 1
print(len(q))
q = list(q)
print(*q[::-1])


a = input().split(' ')
n = int(a[0])
k = int(a[1])
a = list(map(int, input().split(' ')))
d = {}
l = []
s = -1
import collections
l = collections.deque([])
for i in range(n):
	if a[i] in d:
		continue
	elif len(l) < k:
		l.appendleft(a[i])
		d[a[i]] = True
	else:
		cu = l.pop()
		del d[cu]
		d[a[i]] = True
		l.appendleft(a[i])
print(len(l))
for k in l:
	print(k, end=' ')


(x, y) = map(int, input().split())
l1 = list(map(int, input().split()))
l = []
s = {}
for i in l1:
	if len(l) < y:
		if len(l) == 0:
			l.append(i)
			s[i] = 1
		elif i not in s:
			l.append(i)
			s[i] = 1
	elif i not in s:
		f = l.pop(0)
		l.append(i)
		del s[f]
		s[i] = 1
l = l[::-1]
print(len(l))
print(*l)


(n, k) = input().split()
n = int(n)
k = int(k)
lis = list(map(int, input().split()))
dic = set()
j = 0
lis1 = []
for i in lis:
	if i not in dic:
		dic.add(i)
		lis1.append(i)
		if len(lis1) > k:
			dic.discard(lis1[0])
			del lis1[0]
print(len(lis1))
for i in range(len(lis1)):
	print(lis1[len(lis1) - 1 - i], end=' ')


import collections
(n, k1) = map(int, input().split())
k = list(map(int, input().split()))
q = collections.deque()
h = set()
for i in range(len(k)):
	if k[i] not in h:
		q.appendleft(k[i])
		h.add(k[i])
	if len(q) > k1:
		h.discard(q[len(q) - 1])
		q.pop()
print(len(q))
print(*q)


(n, k) = list(map(int, input().split()))
queue = [0] * k
cache = set()
last = k - 1
ip = list(map(int, input().split()))
for elem in ip:
	if elem not in cache:
		cache.add(elem)
		if queue[last] in cache:
			cache.remove(queue[last])
		queue[last] = elem
		last = (last + 1) % k
m = len(cache)
print(m)
i = 0
while i != m:
	last -= 1
	if last < 0:
		last = k - 1
	if queue[last] != 0:
		print(queue[last], end=' ')
		i += 1


from collections import deque
for i in range(1):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	q = deque()
	w = {}
	c = 0
	for j in range(len(a)):
		w[a[j] - 1] = 0
	for j in range(len(a)):
		if w[a[j] - 1] == 0:
			q.append(a[j])
			c = c + 1
			w[a[j] - 1] = 1
		if c > k:
			qw = q.popleft()
			w[qw - 1] = 0
			c = c - 1
	print(len(q))
	q.reverse()
	print(*q)


from collections import deque
(n, k) = map(int, input().split())
friends = list(map(int, input().split()))
d = deque()
q = set()
for ID in friends:
	if ID in q:
		continue
	if len(d) >= k:
		q.remove(d.pop())
	d.appendleft(ID)
	q.add(ID)
print(len(d))
print(*d)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools
from collections import deque, defaultdict, OrderedDict
import collections

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		(n, k) = ria()
		a = ria()
		d = {}
		curcon = deque([])
		for i in range(n):
			z = a[i]
			if z not in d:
				if len(curcon) == k:
					del d[curcon[-1]]
					curcon.pop()
				curcon.appendleft(z)
				d[z] = 1
		wi(len(list(curcon)))
		wia(list(curcon))
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from collections import deque
(numberOfmsg, numberOfMaxDialogs) = map(int, input().split())
numberOfMaxOnScreen = set()
numberInOrder = deque()
msg = map(int, input().split())
for mes in msg:
	if len(numberOfMaxOnScreen) < numberOfMaxDialogs and mes not in numberOfMaxOnScreen:
		numberOfMaxOnScreen.add(mes)
		numberInOrder.appendleft(mes)
	elif mes not in numberOfMaxOnScreen:
		numberOfMaxOnScreen.remove(numberInOrder.pop())
		numberOfMaxOnScreen.add(mes)
		numberInOrder.appendleft(mes)
print(len(numberInOrder))
print(*numberInOrder)


(n, k) = map(int, input().split())
s = [int(i) for i in input().split()]
(d, e) = ([], {})
b = k
a = 0
for i in range(n):
	if k == 0:
		break
	if s[i] not in e:
		d.append(s[i])
		k -= 1
		e[s[i]] = 0
c = 0
r = []
for i in range(n):
	if s[i] not in e:
		d.append(s[i])
		e[s[i]] = 0
		del e[d[a]]
		d[a] = 0
		a += 1
for i in range(len(d) - 1, -1, -1):
	if b == 0 or d[i] == 0:
		break
	c += 1
	r.append(d[i])
print(c, '\n', *r)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
s = []
simple = set([])
ans = set([])
l = -1
r = -1
for i in range(0, n):
	if l == -1 and r == -1:
		s.append(a[i])
		simple.add(a[i])
		l = 0
		r = 0
	elif a[i] not in simple:
		if r - l + 1 < k:
			s.append(a[i])
			simple.add(a[i])
			r += 1
		elif r - l + 1 == k:
			simple.remove(s[l])
			s.append(a[i])
			simple.add(a[i])
			r += 1
			l += 1
print(r - l + 1)
for i in range(r, l - 1, -1):
	print(s[i], end=' ')


import collections
(n, k) = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
de = collections.deque([])
se = set([])
p = 0
for item in x:
	if item not in se:
		if p < k:
			p += 1
			de.appendleft(item)
			se.add(item)
		else:
			de.appendleft(item)
			se.add(item)
			se.discard(de[len(de) - 1])
			de.pop()
print(len(de))
print(*list(de)[:])


from collections import OrderedDict

class LRUCache:

	def __init__(self, capacity):
		self.capacity = capacity
		self.cache = OrderedDict()

	def set(self, key, value):
		if len(self.cache) == self.capacity:
			self.cache.popitem(last=False)
		self.cache[key] = value
(n, k) = map(int, input().strip().split())
ids = map(int, input().strip().split())
convs = LRUCache(k)
for i in ids:
	if i in convs.cache:
		continue
	else:
		convs.set(i, None)
print(len(convs.cache))
print(' '.join(map(str, reversed(convs.cache.keys()))))


import sys
from math import *
from collections import *
inp = lambda : map(int, input().split())
(n, k) = inp()
res = deque()
a = set(res)
for i in inp():
	if i not in a:
		a.add(i)
		if len(res) == k:
			a.remove(res[len(res) - 1])
			res.pop()
			res.appendleft(i)
		else:
			res.appendleft(i)
print(len(res))
print(*res)


from math import ceil, sqrt, floor, sqrt
import collections
from typing import List
from fractions import gcd
from collections import deque
from collections import Counter, deque
(n, k) = list(map(int, input().split()))
IDs = list(map(int, input().split()))
d = {}
q = deque()
for id in IDs:
	if id not in d:
		if len(d) == k:
			del d[q.popleft()]
		d[id] = 1
		q.append(id)
res = list(d.keys())[:k]
print(len(res))
print(' '.join((str(i) for i in reversed(res))))


from collections import deque
from sys import stdin, stdout
input = stdin.readline
t = 1
for _ in range(t):
	(n, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	d = {}
	arr = deque()
	for i in a:
		if not (i in d and d[i] == 1):
			d[i] = 1
			if len(arr) < k:
				arr.append(i)
			else:
				val = arr.popleft()
				d[val] = -1
				arr.append(i)
	stdout.write(str(len(arr)) + '\n')
	for i in range(len(arr) - 1, -1, -1):
		stdout.write(str(arr[i]) + ' ')
	stdout.write('\n')


import queue
(n, s) = map(int, input().split())
arr = list(map(int, input().split()))
m = []
st = set()
L = queue.Queue(maxsize=s)
for i in range(n):
	if L.full() != True and arr[i] not in st:
		st.add(arr[i])
		L.put(arr[i])
	elif L.full() == True and arr[i] not in st:
		x = L.get()
		st.remove(x)
		L.put(arr[i])
		st.add(arr[i])
while L.empty() == False:
	x = L.get()
	m.append(x)
print(len(m))
print(*m[::-1])


from collections import deque
(n, k) = map(int, input().split())
ids = map(int, input().split())
screen = deque(maxlen=k)
screen_set = set()
for i in ids:
	if i in screen_set:
		continue
	if len(screen) == k:
		left = screen.pop()
		screen_set.remove(left)
	screen_set.add(i)
	screen.appendleft(i)
print(len(screen))
print(' '.join(map(str, screen)))


from collections import deque
(n, k) = map(int, input().split())
ids = list(map(int, input().split()))
q = deque()
used = set()
for id in ids:
	if id not in used:
		if len(q) == k:
			used.remove(q.pop())
		q.appendleft(id)
		used.add(id)
print(len(q))
print(*q)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
ans = [0] * 200005
d = {}
q = 200004
lns = 0
for t in l:
	if lns < k:
		val = d.get(t, 0)
		if val == 0:
			ans[q] = t
			d[t] = val + 1
			lns += 1
			q -= 1
	elif lns == k:
		val1 = d.get(t, 0)
		if val1 == 0:
			xc = ans[-1]
			vc = d.get(xc, 0)
			ans.pop(-1)
			d[xc] = vc - 1
			ans[q] = t
			q -= 1
			vb = d.get(t, 0)
			d[t] = vb + 1
print(lns)
for i in ans:
	if i > 0:
		print(i, end=' ')


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
a = []
s = set()
for i in arr:
	if i not in s:
		s.add(i)
		a.append(i)
	if len(a) > k:
		s.discard(a[0])
		del a[0]
print(len(a))
for i in range(len(a) - 1, -1, -1):
	print(a[i], end=' ')


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
d = {}
for i in range(n):
	d[l[i]] = 0
l1 = []
cnt = 0
ind = 0
for i in range(n):
	if d[l[i]] != 1:
		if cnt < k:
			l1.append(l[i])
			cnt += 1
			d[l[i]] = 1
		else:
			d[l1[ind]] = 0
			l1.append(l[i])
			d[l[i]] = 1
			ind += 1
print(cnt)
for i in range(len(l1) - 1, ind - 1, -1):
	print(l1[i], end=' ')


def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid - 1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
ans = []
s = list(set(l))
s.sort()
sl = len(s)
cou = []
for j in range(sl):
	cou.append(0)
m = 0
h = 0
for j in range(n):
	a = binarySearch(s, 0, sl - 1, l[j])
	if cou[a] == 0:
		if m == k + h:
			b = binarySearch(s, 0, sl - 1, ans[h])
			cou[b] -= 1
			h += 1
			ans.append(l[j])
			cou[a] += 1
			m += 1
		else:
			ans.append(l[j])
			cou[a] += 1
			m += 1
print(m - h)
for j in range(m - 1, h - 1, -1):
	print(ans[j], end=' ')


from collections import deque
(n, k) = [x for x in map(int, input().split())]
A = [x for x in map(int, input().split())]
d = deque()
res = set()
for i in A:
	if i in res:
		continue
	res.add(i)
	d.appendleft(i)
	if len(d) > k:
		res.discard(d.pop())
print(len(d))
print(*d)


import os
import sys
import math
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def dijkstra(start, distance, path, n):
	visited = [False for _ in range(n)]
	distance[start] = 0
	for i in range(n):
		v = -1
		for j in range(n):
			if not visited[v] and (v == -1 or distance[j] < distance[v]):
				v = j
		if distance[v] == math.inf:
			break
		visited[v] = True
		for edge in adj[v]:
			destination = edge[0]
			weight = edge[1]
			if distance[v] + weight < distance[destination]:
				distance[destination] = distance[v] + weight
				path[destination] = v

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

def seive(n):
	primes = [True] * (n + 1)
	ans = []
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	for p in range(2, n + 1):
		if primes[p]:
			ans += [p]
	return ans

def factors(n):
	factors = []
	x = 1
	while x * x <= n:
		if n % x == 0:
			if n // x == x:
				factors.append(x)
			else:
				factors.append(x)
				factors.append(n // x)
		x += 1
	return factors

def main():
	try:
		from collections import deque
		(n, k) = invr()
		q = deque()
		d = {}
		a = inlt()
		c = 0
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = 1
				q.appendleft(a[i])
				if c < k:
					c += 1
				else:
					del d[q.pop()]
		print(c)
		print(*q, sep=' ')
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(n, k) = map(int, input().split())
li = list(map(int, input().split()))
s = []
d = {}
for i in li:
	if d.get(i, 0) == 0:
		s.append(i)
		d[i] = 1
	if len(s) > k:
		d.pop(s.pop(0))
s = s[::-1]
print(len(s))
print(*s)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
(n, k) = inpl()
a = inpl()
se = set()
q = deque([])
ln = 0
for x in a:
	if x in se:
		continue
	if ln < k:
		q.appendleft(x)
		se.add(x)
		ln += 1
	else:
		p = q.pop()
		se.discard(p)
		q.appendleft(x)
		se.add(x)
print(len(q))
print(*list(q))


from collections import defaultdict as df
import heapq
import bisect
import math
from itertools import combinations, permutations
from collections import deque
(n, k) = list(map(int, input().split()))
a = list(map(int, input().rstrip().split()))
ans = deque()
p = df(int)
for i in range(n):
	p[i] = 0
for i in a:
	if p[i] > 0:
		pass
	elif len(ans) >= k:
		g = ans.pop()
		ans.appendleft(i)
		p[i] += 1
		p[g] -= 1
	else:
		ans.appendleft(i)
		p[i] += 1
print(len(ans))
print(*ans)


(n, k) = list(map(int, input().split()))
Massage = []
id_all = list(map(int, input().split()))
A = set()
for i in range(n):
	id = id_all[i]
	if id not in A:
		Massage.append(id)
		A.add(id)
		if len(A) > k:
			A.remove(Massage[-k - 1])
if len(Massage) > k:
	print(k)
	print(*Massage[-1:-(k + 1):-1])
else:
	print(len(Massage))
	print(*Massage[::-1])


from collections import deque
(n, k) = list(map(int, input().strip().split()))
deq = deque()
s = set()
for i in list(map(int, input().strip().split())):
	if i not in s:
		if len(deq) == k:
			s.remove(deq.pop())
		deq.appendleft(i)
		s.add(i)
print(len(deq))
print(*deq)


(n, k) = map(int, input().split())
a = [*map(int, input().split())]
from collections import deque
ans = deque()
ins = set()
for i in a:
	if i not in ins:
		if len(ans) == k:
			ins.remove(ans[0])
			ans.popleft()
		ans.append(i)
		ins.add(i)
ans.reverse()
print(len(ans))
print(*ans)


from collections import deque
(n, k) = map(int, input().split())
q = deque()
x = [int(n) for n in input().split()]
a = {}
for i in x:
	a[i] = 0
c = 0
ct = 0
for i in range(n):
	ct += 1
	if a[x[i]] == 0:
		c += 1
		a[x[i]] = 1
		q.appendleft(x[i])
	if c == k:
		break
for i in range(ct, n):
	if a[x[i]] == 0:
		q.appendleft(x[i])
		a[x[i]] = 1
		w = q.pop()
		a[w] = 0
print(len(q))
print(*q)


import sys, math, queue, collections
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
last = collections.deque()
listed = dict()
for ai in a:
	if ai not in listed:
		listed[ai] = False
	if not listed[ai]:
		if len(last) == k:
			listed[last.popleft()] = False
		last.append(ai)
		listed[ai] = True
last = list(reversed(last))
print(len(last))
print(*last)