Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back

#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  speed int n, k;
  cin >> n >> k;
  vector<int> inp(n);
  map<int, vector<int>> mp;
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
    mp[inp[i]].pb(i);
  }
  vector<bool> status(n + 1, 0);
  status[n] = 1;
  map<int, int> next;
  next[-1] = -1;
  for (auto it : mp) {
    int len = (it.y).size();
    for (int i = 0; i < len - 1; ++i) {
      next[(it.y)[i]] = (it.y)[i + 1];
    }
    next[(it.y)[len - 1]] = (it.y)[0];
    status[(it.y)[len - 1]] = 1;
  }
  int pres = -1;
  vector<int> fin;
  do {
    pres = (next[pres] + 1);
    while (!status[pres]) {
      pres = (next[pres] + 1);
    }
    fin.pb(pres);
  } while (pres != n);
  int dis = fin.size();
  int r = (k % dis);
  if (r == 0) {
    return 0;
  }
  r = inp[fin[r - 1]];
  int six = mp[r].back();
  stack<int> st;
  map<int, bool> done;
  for (int i = six; i < n; ++i) {
    if (!done[inp[i]]) {
      st.push(inp[i]);
      done[inp[i]] = 1;
    } else {
      while (st.top() != inp[i]) {
        done[st.top()] = 0;
        st.pop();
      }
      done[inp[i]] = 0;
      st.pop();
    }
  }
  vector<int> sol;
  while (!st.empty()) {
    sol.pb(st.top());
    st.pop();
  }
  reverse(sol.begin(), sol.end());
  for (auto it : sol) {
    cout << it << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back

#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
signed main() {
  speed int n, k;
  cin >> n >> k;
  vector<int> inp(n);
  map<int, vector<int>> mp;
  for (int i = 0; i < n; ++i) {
    cin >> inp[i];
    mp[inp[i]].pb(i);
  }
  vector<bool> status(n + 1, 0);
  status[n] = 1;
  map<int, int> next;
  next[-1] = -1;
  for (auto it : mp) {
    int len = (it.y).size();
    for (int i = 0; i < len - 1; ++i) {
      next[(it.y)[i]] = (it.y)[i + 1];
    }
    next[(it.y)[len - 1]] = (it.y)[0];
    status[(it.y)[len - 1]] = 1;
  }
  int pres = -1;
  vector<int> fin;
  do {
    pres = (next[pres] + 1);
    while (!status[pres]) {
      pres = (next[pres] + 1);
    }
    fin.pb(pres);
  } while (pres != n);
  int dis = fin.size();
  int r = (k % dis);
  if (r == 0) {
    return 0;
  }
  r = inp[fin[r - 1]];
  int six = mp[r].back();
  stack<int> st;
  map<int, bool> done;
  for (int i = six; i < n; ++i) {
    if (!done[inp[i]]) {
      st.push(inp[i]);
      done[inp[i]] = 1;
    } else {
      while (st.top() != inp[i]) {
        done[st.top()] = 0;
        st.pop();
      }
      done[inp[i]] = 0;
      st.pop();
    }
  }
  vector<int> sol;
  while (!st.empty()) {
    sol.pb(st.top());
    st.pop();
  }
  reverse(sol.begin(), sol.end());
  for (auto it : sol) {
    cout << it << " ";
  }
}