Language: C++

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define fst first
#define snd second
#define PI (acos(-1))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

#define MAXN 1123456
#define MOD 1000000007

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;

#include <bits/extc++.h> /** keep-include */
// To use most bits rather than just the lowest ones:
struct chash {
	const uint64_t C = ll(2e18 * M_PI) + 71; // large odd number
	ll operator()(ll x) const { return __builtin_bswap64(x*C); }
};
__gnu_pbds::gp_hash_table<ll,int,chash> back({},{},{},{},{1<<16});
__gnu_pbds::gp_hash_table<ll,int,chash> backinv({},{},{},{},{1<<16});


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, k;
    cin >> n >> k;

    vi cur;

    vi a(n);
    rep(i,0,n) cin >> a[i];

    ll p=-1;

    ll cyc = 0;

    rep(i,1,k+1) {
        rep(j,0,n) {
            ll l = a[j];
            //cerr << "l: " << l << endl;
            if (back.count(l) && back[l] <= p) {
                p=back[l]-1;
                back.erase(back.find(l));
                //cerr << "p = " << p << endl;
            } else {
                if (p+1 >= sz(cur)) cur.pb(l);
                else cur[p+1] = l;
                ++p;

                if (backinv.count(p) && back.count(backinv[p]) && back[backinv[p]] == p) {
                    //cerr << "erasing " << backinv[p] << endl;
                    back.erase(back.find(backinv[p]));
                }

                if (back.count(l) && backinv.count(back[l])) backinv.erase(backinv.find(back[l]));
                
                back[l]=p;
                //cerr << "back[" << l << "] = " << p << endl;
                backinv[p]=l;
            }
        }
        if (p == -1) {
            cyc = i;   
            break;
        }

        if (i == k) {
            rep(u,0,p+1) cout << cur[u] << " " ;
            cout << endl;
            return 0;
        }
        //getchar();
    }

    if (cyc)
    k %= cyc;

    rep(i,1,k+1) {
        rep(j,0,n) {
            ll l = a[j];
            //cerr << "l: " << l << endl;
            if (back.count(l) && back[l] <= p) {
                p=back[l]-1;
                back.erase(back.find(l));
                //cerr << "p = " << p << endl;
            } else {
                if (p+1 >= sz(cur)) cur.pb(l);
                else cur[p+1] = l;
                ++p;

                if (backinv.count(p) && back.count(backinv[p]) && back[backinv[p]] == p) {
                    //cerr << "erasing " << backinv[p] << endl;
                    back.erase(back.find(backinv[p]));
                }

                if (back.count(l) && backinv.count(back[l])) backinv.erase(backinv.find(back[l]));
                
                back[l]=p;
                //cerr << "back[" << l << "] = " << p << endl;
                backinv[p]=l;
            }
        }

        if (i == k) {
            rep(u,0,p+1) cout << cur[u] << " " ;
            cout << endl;
            return 0;
        }
        //getchar();
    }

}


Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/extc++.h:65,
                 from a.cc:25:
/usr/include/c++/14/ext/random:124:11: error: macro "min" requires 2 arguments, but only 1 given
  124 |       min()
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:128:11: error: macro "max" requires 2 arguments, but only 1 given
  128 |       max()
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:129:54: error: macro "max" requires 2 arguments, but only 1 given
  129 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:526:11: error: macro "min" requires 2 arguments, but only 1 given
  526 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:533:11: error: macro "max" requires 2 arguments, but only 1 given
  533 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:848:11: error: macro "min" requires 2 arguments, but only 1 given
  848 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:857:11: error: macro "max" requires 2 arguments, but only 1 given
  857 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:859:56: error: macro "max" requires 2 arguments, but only 1 given
  859 |         __res.fill(std::numeric_limits<_RealType>::max());
      |                                                        ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1100:11: error: macro "min" requires 2 arguments, but only 1 given
 1100 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1107:11: error: macro "max" requires 2 arguments, but only 1 given
 1107 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1108:54: error: macro "max" requires 2 arguments, but only 1 given
 1108 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1352:11: error: macro "min" requires 2 arguments, but only 1 given
 1352 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1359:11: error: macro "max" requires 2 arguments, but only 1 given
 1359 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1360:54: error: macro "max" requires 2 arguments, but only 1 given
 1360 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1602:11: error: macro "min" requires 2 arguments, but only 1 given
 1602 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1609:11: error: macro "max" requires 2 arguments, but only 1 given
 1609 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1610:54: error: macro "max" requires 2 arguments, but only 1 given
 1610 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1871:11: error: macro "min" requires 2 arguments, but only 1 given
 1871 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1878:11: error: macro "max" requires 2 arguments, but only 1 given
 1878 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:1879:54: error: macro "max" requires 2 arguments, but only 1 given
 1879 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2106:11: error: macro "min" requires 2 arguments, but only 1 given
 2106 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2113:11: error: macro "max" requires 2 arguments, but only 1 given
 2113 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2362:11: error: macro "min" requires 2 arguments, but only 1 given
 2362 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2369:11: error: macro "max" requires 2 arguments, but only 1 given
 2369 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2370:54: error: macro "max" requires 2 arguments, but only 1 given
 2370 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2624:11: error: macro "min" requires 2 arguments, but only 1 given
 2624 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2631:11: error: macro "max" requires 2 arguments, but only 1 given
 2631 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2875:11: error: macro "min" requires 2 arguments, but only 1 given
 2875 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:2884:11: error: macro "max" requires 2 arguments, but only 1 given
 2884 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3142:11: error: macro "min" requires 2 arguments, but only 1 given
 3142 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3154:11: error: macro "max" requires 2 arguments, but only 1 given
 3154 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3385:11: error: macro "min" requires 2 arguments, but only 1 given
 3385 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3386:55: error: macro "max" requires 2 arguments, but only 1 given
 3386 |       { return -std::numeric_limits<result_type>::max(); }
      |                                                       ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3392:11: error: macro "max" requires 2 arguments, but only 1 given
 3392 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3393:54: error: macro "max" requires 2 arguments, but only 1 given
 3393 |       { return std::numeric_limits<result_type>::max(); }
      |                                                      ^
a.cc:12:9: note: macro "max" defined here
   12 | #define max(a, b) ((a) > (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3565:11: error: macro "min" requires 2 arguments, but only 1 given
 3565 |       min() const
      |           ^
a.cc:13:9: note: macro "min" defined here
   13 | #define min(a, b) ((a) < (b) ? (a) : (b))
      |         ^~~
/usr/include/c++/14/ext/random:3577:11: error: macro "max" requires 2 arguments, but only 1 given
 3577 |       max() const
      |           ^
a.cc:12:9: note: macro "max" defined here