Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repr(i, n) for (ll i = (n); i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define FORR(i, m, n) for (ll i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(25);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  map<ll, ll> mp;
  rep(i, n) mp[a[i]] = -1;
  vector<vector<ll>> dp;
  vector<ll> res(n);
  rep(i, 2 * n) {
    if (mp[a[i % n]] != -1 && mp[a[i % n]] < n)
      res[mp[a[i % n]]] = i - mp[a[i % n]] + 1;
    mp[a[i % n]] = i;
  }
  dp.push_back(res);
  while (res[0] < n * k) {
    vector<ll> nxt(n, 0);
    rep(i, n) nxt[i] = res[i] + res[(res[i] + i) % n];
    res = nxt;
    dp.push_back(res);
  }
  ll m = dp.size();
  ll pos = 0;
  repr(i, m - 1) {
    if (pos + dp[i][pos % n] < n * k)
      pos += dp[i][pos % n];
  }
  pos %= n;
  stack<ll> st;
  rep(i, n) mp[a[i]] = 0;
  FOR(i, pos, n) {
    // cout << i << ' ' << mp[a[i]] << '\n';
    if (mp[a[i]] == 1) {
      while (!st.empty() && st.top() != a[i]) {
        mp[st.top()] = 0;
        st.pop();
      }
      mp[a[i]] = 0;
      st.pop();
    } else {
      st.push(a[i]);
      mp[a[i]] = 1;
    }
  }
  if (st.empty()) {
    return 0;
  }
  vector<ll> ans;
  while (!st.empty()) {
    ans.push_back(st.top());
    st.pop();
  }
  reverse(ans.begin(), ans.end());
  rep(i, ans.size()) cout << ans[i] << ' ';
  cout << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repr(i, n) for (ll i = (n); i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define FORR(i, m, n) for (ll i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(25);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  if (n % 2 == 0) {
    bool f = true;
    rep(i, n / 2) {
      if (a[2 * i] != a[2 * i + 1]) {
        f = false;
      }
    }
    if (f)
      return 0;
  }
  map<ll, ll> mp;
  rep(i, n) mp[a[i]] = -1;
  vector<vector<ll>> dp;
  vector<ll> res(n);
  rep(i, 2 * n) {
    if (mp[a[i % n]] != -1 && mp[a[i % n]] < n)
      res[mp[a[i % n]]] = i - mp[a[i % n]] + 1;
    mp[a[i % n]] = i;
  }
  dp.push_back(res);
  while (res[0] < n * k) {
    vector<ll> nxt(n, 0);
    rep(i, n) nxt[i] = res[i] + res[(res[i] + i) % n];
    res = nxt;
    dp.push_back(res);
  }
  ll m = dp.size();
  ll pos = 0;
  repr(i, m - 1) {
    if (pos + dp[i][pos % n] < n * k)
      pos += dp[i][pos % n];
  }
  pos %= n;
  stack<ll> st;
  rep(i, n) mp[a[i]] = 0;
  FOR(i, pos, n) {
    // cout << i << ' ' << mp[a[i]] << '\n';
    if (mp[a[i]] == 1) {
      while (!st.empty() && st.top() != a[i]) {
        mp[st.top()] = 0;
        st.pop();
      }
      mp[a[i]] = 0;
      st.pop();
    } else {
      st.push(a[i]);
      mp[a[i]] = 1;
    }
  }
  if (st.empty()) {
    return 0;
  }
  vector<ll> ans;
  while (!st.empty()) {
    ans.push_back(st.top());
    st.pop();
  }
  reverse(ans.begin(), ans.end());
  rep(i, ans.size()) cout << ans[i] << ' ';
  cout << '\n';

  return 0;
}