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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>
#include<unordered_map>

using namespace std;
#define int long long
#define lint long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
typedef pair<int,int> P;
const lint inf=1e18+7;
const int MOD=1000000007;
struct Node{
    lint no;
    lint ind;
    vector<P> nxt;
    void input(int i,int in_ind){
        no=i;nxt.push_back({in_ind,0});
        this->ind=in_ind;
    }
};
signed main(){
  lint n,K;cin>>n>>K;
  vector<Node> node(n);
  unordered_map<int,int> memo;
  rep(i,n){
      int a;cin>>a;
      node[i].input(a,i);
      if(a==node[0].no&&i>0){
          if(node[0].nxt.size()==1){
              if(i==n-1){
                  node[0].nxt.push_back({0,1});    
              }
              else node[0].nxt.push_back({i+1,0});
          }
      }
  }
  if(n==1){
      if(K%2!=0){
          cout<<node[0].no<<endl;
          return 0;
      }
      else{
          return 0;
      }
  }
  if(node[0].nxt.size()==1){
      node[0].nxt.push_back({1,1});
  }
  rep(i,n){
      if(memo[node[i].no]==0){
          memo[node[i].no]=i;
      }
      else{
          int buf=memo[node[i].no];
          memo[node[i].no]=i;
          if(node[buf].nxt.size()==1){
              if(i==n-1){
                  node[buf].nxt.push_back({0,1});
              }
              else node[buf].nxt.push_back({i+1,0});
          }
      }
  }

  rep(i,n){
      if(memo[node[i].no]==0){
          memo[node[i].no]=i;
      }
      else{
          int buf=memo[node[i].no];
          memo[node[i].no]=i;
          if(node[buf].nxt.size()==1){
              if(i==n-1){
                  node[buf].nxt.push_back({0,2});
              }
              else node[buf].nxt.push_back({i+1,1});
          }
      }
  }
  int sup=0;
  rep1(k,100){
      int mx=0;
      rep(i,n){
          node[i].nxt.push_back({node[node[i].nxt[k].first].nxt[k].first,node[node[i].nxt[k].first].nxt[k].second+node[i].nxt[k].second});
          mx=max(mx,node[i].nxt[k].second+node[node[i].nxt[k].first].nxt[k].second);
      }
      if(mx>K){
          sup=k;
          //break;
      }
  }
  rep(i,n){
      //printf("i = %lld, nxt = %lld\n",i,node[i].nxt[1].first);
  }
  int k=K-1;
  int cur=0;
  //return 0;
  while(1){
      if(k<=1){
          break;
      }
      for(int kkk=0;kkk<1;++kkk){
          int right=100;
          int left=0;
          while(right-left>1){
              int mid=(right+left)/2;
              if(node[cur].nxt[mid].second<=k){
                  left=mid;
              }
              else right=mid;
          }
          int i=left;
          if(node[cur].nxt[i].second<=k){
              k-=node[cur].nxt[i].second;
              cur=node[cur].nxt[i].first;
              break;
          }
      }
  }
  vector<int> res;
  for(cur;cur<n;++cur){
      res.push_back(node[cur].no);
  }
  if(k==1){
      rep(i,n){
          res.push_back(node[i].no);
      }
  }
  unordered_map<int,int> hoge;
  int nr=res.size();
  /*
  rep(i,nr){
      cout<<res[i]<<" ";
  }
  cout<<endl;*/
  rep(i,nr){
      hoge[res[i]]+=1;
  }
  int last=inf;
  rep(i,nr){
      if(last==inf){
          if(hoge[res[i]]==1){
              cout<<res[i]<<" ";
          }
          else{
              last=res[i];
          }
          hoge[res[i]]-=1;
      }
      else if(last!=inf&&res[i]!=last){
          hoge[res[i]]-=1;
      }
      else if(last!=inf&&res[i]==last){
          last=inf;
          hoge[res[i]]-=1;
      }
  }
  cout<<endl;
  return 0;
}