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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Set;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B_DoNotDuplicate solver = new B_DoNotDuplicate();
        solver.solve(1, in, out);
        out.close();
    }

    static class B_DoNotDuplicate {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            final int N = (int) (2e5 + 1);
            int n = in.nextInt();
            long k = in.nextLong();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }

            int[] whoLeavesIfStartAt = new int[n + 1];
            int[] latest = new int[N + 1];
            Arrays.fill(latest, n);
            whoLeavesIfStartAt[n] = N;
            for (int i = n - 1; i >= 0; i--) {
                int x = a[i];
                if (latest[x] == n) {
                    whoLeavesIfStartAt[i] = x;
                } else {
                    whoLeavesIfStartAt[i] = whoLeavesIfStartAt[latest[x] + 1];
                }
                latest[x] = i;
            }

            int[] whoLeavesIfXEnters = new int[N + 1];
            for (int x = 0; x < N; x++) {
                if (latest[x] == n) {
                    continue;
                }
                whoLeavesIfXEnters[x] = whoLeavesIfStartAt[latest[x] + 1];
            }
            whoLeavesIfXEnters[N] = whoLeavesIfStartAt[0];

            --k;
            int[] seen = new int[N + 1];
            Arrays.fill(seen, -1);
            int cur = N;
            int step;
            for (step = 0; step < k; step++) {
                if (seen[cur] >= 0) {
                    break;
                }
                seen[cur] = step;
                cur = whoLeavesIfXEnters[cur];
            }

            int cycleLength = step - seen[cur];
            long stepsLeft = step == k ? 0 : (k - step) % cycleLength;
            while (stepsLeft > 0) {
                cur = whoLeavesIfXEnters[cur];
                --stepsLeft;
            }

            int pos = Math.min(n, cur == N ? 0 : latest[cur] + 1);
            int[] c = simulate(Arrays.copyOfRange(a, pos, n));

            for (int i = 0; i < c.length; i++) {
                if (i > 0) {
                    out.print(" ");
                }
                out.print(c[i]);
            }
            out.println();
        }

        private int[] simulate(int[] a) {
            int[] b = new int[a.length];
            int p = 0;
            Set<Integer> set = new HashSet<>();
            for (int x : a) {
                if (!set.contains(x)) {
                    b[p++] = x;
                    set.add(x);
                } else while (set.contains(x)) {
                    set.remove(b[--p]);
                }
            }
            return Arrays.copyOf(b, p);
        }

    }

    static class FastScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public FastScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}