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

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[]str = Console.ReadLine().Split();
		int N = int.Parse(str[0]);
		long K = long.Parse(str[1]);
		int[]A = new int[N];
		string[]str2 = Console.ReadLine().Split();
		for(var i=0;i<N;i++){
			A[i] = int.Parse(str2[i]);
		}
		var dic = new Dictionary<int,int>();
		var dic2 = new Dictionary<int,int>();
		var dics = new Dictionary<int,int>();
		int[]B = new int[N];
		int[]B2 = new int[N];
      	int[]BS = new int[N];
		for(var i=0;i<N;i++){
			if(dic.ContainsKey(A[i])){
				dic[A[i]]++;
				B2[i] = dic2[A[i]];
			} else {
				dic[A[i]] = 1;
				B2[i] = -1;
              	dics[A[i]] = i;
            }
			dic2[A[i]]=i;
			B[i] = dic[A[i]];
		}
		var dic3 = new Dictionary<int,int>();
		var dic4 = new Dictionary<int,int>();
		int[]B3 = new int[N];
		int[]B4 = new int[N];
		for(var i=N-1;i>=0;i--){
			if(dic3.ContainsKey(A[i])){
				dic3[A[i]]++;
				B4[i] = dic4[A[i]];
			} else {
				dic3[A[i]] = 1;
				B4[i] = -1;
			}
			dic4[A[i]]=i;
			B3[i] = dic3[A[i]];
		}
		int di = 0;
      	long[]L = new long[N];
      	var ans = new List<int>();
		while(true){
          	//Console.Error.WriteLine(K+" "+di);
          	if(L[di]>0){
              	K=K%(L[di]-K);
            }
          	L[di] = K;
			int T = dic[A[di]];
          	int T2 = B4[di];
			//Console.Error.WriteLine(T+" "+T2);
          	if(T2==-1){
              	if(K==1){
                  for(var i=di;i<N;i++){
                    ans.Add(A[i]);
                  }
                  break;
                } else {
                  	if(T==1){
                      	K -= 1;
                      	di += 1;
                    } else {
                      	K -= 1;
	                  	di = dics[A[di]] + 1;
                      	if(di==N){
                          K -= 1;
                          di = 0;
                          if(K==0){
                            break;
                          }
                        }
                    }
                }
            } else {
              	di = T2;
              	di = di + 1;
              	if(di==N){
                  K -= 1;
                  di = 0;
                  if(K==0){
                    break;
                  }
                }
            }
		}
      	if(ans.Count>0){
          var dic5 = new Dictionary<int,int>();
          int[]B5 = new int[N];
          for(var i=ans.Count-1;i>=0;i--){
            if(dic5.ContainsKey(ans[i])){
              dic5[ans[i]]++;
              B5[i] = dic5[ans[i]];
            } else {
              dic5[ans[i]]=1;
              B5[i] = 1;
            }
          }
          var ans2 = new List<int>();
          int flag = -1;
          for(var i=0;i<ans.Count;i++){
            if(flag == -1){
            if(B5[i]==1){
              ans2.Add(ans[i]);
            } else {
              flag = ans[i];
            }
            } else {
              if(ans[i]==flag){
                flag = -1;
              }
            }
          }
          if(ans2.Count>0){
          Console.Write(ans2[0]);
      		for(var i=1;i<ans2.Count;i++){
          	Console.Write(" "+ans2[i]);
        	}
          }
        }
	}
}