Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

template <size_t N, typename T, typename U = T,
          std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, T t) {
  return std::vector<U>(sz, t);
}
template <size_t N, typename T,
          std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {
  return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args,
          std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {
  return make_vector<1>(a, make_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t> auto &at(T &t, Size_t i) {
  return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto &at(T &t, Size_t i, Args... args) {
  return at(t.at(i), args...);
}

int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  int n;
  long long k;
  std::cin >> n >> k;
  std::vector<int> a(n);
  for (auto &x : a) {
    std::cin >> x;
    x--;
  }
  constexpr int max = 2e5;
  auto m = make_vector<2, int>(max, 0);
  for (int i = 0; i < n; i++) {
    m.at(a.at(i)).push_back(i);
  }
  auto add = std::vector<int>(n);
  for (auto &v : m) {
    if (v.empty())
      continue;
    int sz = v.size();
    v.push_back(n + v.front());
    for (int i = 0; i < sz; i++) {
      auto crr = v.at(i), nxt = v.at(i + 1);
      assert(0 <= crr && crr < n && crr < nxt);
      add.at(crr) = nxt - crr + 1;
    }
  }
  auto moved = [&](auto x) { return x + add.at(x % n); };
  k %= [&] {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      ret = moved(ret);
      if (ret % n == 0)
        break;
    }
    assert(ret % n == 0);
    return ret / n;
  }();
  long long crr = 0, goal = n * k;
  std::vector<int> ans;
  while (crr < goal) {
    auto nxt = moved(crr);
    if (nxt <= goal) {
      crr = nxt;
      continue;
    }
    ans.push_back(a.at((crr++ % n)));
  }
  int sz = ans.size();
  for (int i = 0; i < sz; i++) {
    std::cout << (i > 0 ? " " : "") << ans.at(i) + 1;
  }
  std::cout << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

template <size_t N, typename T, typename U = T,
          std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, T t) {
  return std::vector<U>(sz, t);
}
template <size_t N, typename T,
          std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {
  return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args,
          std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {
  return make_vector<1>(a, make_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t> auto &at(T &t, Size_t i) {
  return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto &at(T &t, Size_t i, Args... args) {
  return at(t.at(i), args...);
}

int main() {
  std::cin.tie(0);
  std::cin.sync_with_stdio(false);
  int n;
  long long k;
  std::cin >> n >> k;
  std::vector<int> a(n);
  for (auto &x : a) {
    std::cin >> x;
    x--;
  }
  constexpr int max = 2e5;
  auto m = make_vector<2, int>(max, 0);
  for (int i = 0; i < n; i++) {
    m.at(a.at(i)).push_back(i);
  }
  auto add = std::vector<int>(n);
  for (auto &v : m) {
    if (v.empty())
      continue;
    int sz = v.size();
    v.push_back(n + v.front());
    for (int i = 0; i < sz; i++) {
      auto crr = v.at(i), nxt = v.at(i + 1);
      assert(0 <= crr && crr < n && crr < nxt);
      add.at(crr) = nxt - crr + 1;
    }
  }
  auto moved = [&](auto x) { return x + add.at(x % n); };
  k %= [&] {
    long long ret = 0;
    for (int i = 0; i < n; i++) {
      ret = moved(ret);
      if (ret % n == 0)
        break;
    }
    assert(ret % n == 0);
    return ret / n;
  }();
  long long crr = 0, goal = n * k;
  std::vector<int> ans;
  while (crr < goal) {
    auto nxt = moved(crr);
    if (nxt <= goal) {
      crr = nxt;
      continue;
    }
    ans.push_back(a.at((crr++ % n)));
  }
  int sz = ans.size();
  for (int i = 0; i < sz; i++) {
    std::cout << (i > 0 ? " " : "") << ans.at(i) + 1;
  }
  std::cout << std::endl;
  return 0;
}