Problem p02684 (C++) — buggy submission, verdict: Runtime Error

// JAI SHREE RAM
#pragma GCC optimize("O3")
typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define loop for (int i = 0; i < n; i++)
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max3(a, max(b, c), d)
#define max5(a, b, c, d, e) max4(a, max(b, c), d, e)
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min3(a, min(b, c), d)
#define min5(a, b, c, d, e) min4(a, min(b, c), d, e)
#define sec second
#define fir first
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
// #define all(a) (a).begin(),(a).end()
#define vec vector<int>
#define M_PI 3.14159265358979323846 /* pi */
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast ll n, k;
  cin >> n >> k;
  std::vector<ll> v(n);

  rep(i, n) {
    cin >> v[i];
    v[i]--;
  }
  if (k == 1) {
    cout << v[0] << endl;
    return 0;
  }
  ll arr[200005] = {0};
  arr[0] = 1;

  ll i = 0, index = 0, size;
  while (true) {
    k--;
    if (arr[v[i]] == 1) {
      i = v[i];
      index = i;
      break;
    }
    arr[v[i]] = 1;

    if (k == 0) {
      cout << v[i] + 1 << endl;
      return 0;
    }

    i = v[i];
  }

  if (k == 0) {
    cout << i + 1 << endl;
    return 0;
  }

  ll c = 0, x = 0;
  memset(arr, 0, sizeof(arr));
  arr[i] = 1;

  while (1) {
    c++;
    i = v[i];
    if (arr[i] == 1)
      break;
    arr[i] = 1;
  }

  // cout<<c<<"->"<<k<<endl;
  // cout<<k%c<<endl;
  ll rem = k % c;

  // cout<<rem<<endl;

  ll ans = index;
  while (rem--) {
    ans = v[index];
    index = v[index];
  }
  cout << ans + 1 << endl;
  return 0;
}


Error: -11

Fixed submission:

// JAI SHREE RAM
#pragma GCC optimize("O3")
typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define loop for (int i = 0; i < n; i++)
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max3(a, max(b, c), d)
#define max5(a, b, c, d, e) max4(a, max(b, c), d, e)
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min3(a, min(b, c), d)
#define min5(a, b, c, d, e) min4(a, min(b, c), d, e)
#define sec second
#define fir first
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
// #define all(a) (a).begin(),(a).end()
#define vec vector<int>
#define M_PI 3.14159265358979323846 /* pi */
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast ll n, k;
  cin >> n >> k;
  std::vector<ll> v(n);

  rep(i, n) {
    cin >> v[i];
    v[i]--;
  }
  if (k == 1) {
    cout << v[0] << endl;
    return 0;
  }
  ll arr[10005] = {0};
  arr[0] = 1;

  ll i = 0, index = 0, size;
  while (true) {
    k--;
    if (arr[v[i]] == 1) {
      i = v[i];
      index = i;
      break;
    }
    arr[v[i]] = 1;

    if (k == 0) {
      cout << v[i] + 1 << endl;
      return 0;
    }

    i = v[i];
  }

  if (k == 0) {
    cout << i + 1 << endl;
    return 0;
  }

  ll c = 0, x = 0;
  memset(arr, 0, sizeof(arr));
  arr[i] = 1;

  while (1) {
    c++;
    i = v[i];
    if (arr[i] == 1)
      break;
    arr[i] = 1;
  }

  // cout<<c<<"->"<<k<<endl;
  // cout<<k%c<<endl;
  ll rem = k % c;

  // cout<<rem<<endl;

  ll ans = index;
  while (rem--) {
    ans = v[index];
    index = v[index];
  }
  cout << ans + 1 << endl;
  return 0;
}