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

// ABEY SAALE
// QUESTION KHUD KR SOLUTION KYU DEKH RHA H

// MAAF KIJIYEGA ME GUSSE ME IDHAR UDHAR NIKAL JATA HU
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define endl "\\n"
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

ll fac[2000005];
void pre() {
  fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 200005; i++)
    fac[i] = (i * fac[i - 1]) % mod;
}
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
ll ncr(ll n, ll r) {
  return ((fac[n] * power(fac[r], mod - 2)) % mod *
          power(fac[n - r], mod - 2)) %
         mod;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE

signed main() {

  senky_bansal IIIT_ALLAHABAD

      // pre();
      ll t;
  // cin>>t;
  t = 1;
  while (t--) {

    ll n, k;
    cin >> n >> k;
    ll a[n + 5];
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    set<ll> st;
    vector<ll> v1;
    st.insert(1);
    v1.pb(1);
    ll curr = 1;
    if (a[1] == 1) {
      cout << 1 << endl;
      return 0;
    }
    ll k1 = k;
    ll val = -1;
    while (k > 0) {
      curr = a[curr];
      if (st.find(curr) != st.end()) {
        val = curr;
        break;
      } else {
        st.insert(curr);
        v1.pb(curr);
      }
      k--;
    }
    // cout<<k<<endl;
    if (k > 0) {
      vector<ll> v;
      v = v1;
      ll ans = v.size();
      for (int i = 0; i < v.size(); i++) {
        if (v[i] == val) {
          k1 -= (i);
          // cout<<k1<<" "<<ans<<endl;
          ll rem = k1 % ans;
          cout << v[i + rem] << endl;
          return 0;
        } else {
          ans--;
        }
      }
    }
    cout << curr << endl;
  }
}


Error: 0

Fixed submission:

// ABEY SAALE
// QUESTION KHUD KR SOLUTION KYU DEKH RHA H

// MAAF KIJIYEGA ME GUSSE ME IDHAR UDHAR NIKAL JATA HU
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define endl "\\n"
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

ll fac[2000005];
void pre() {
  fac[0] = 1;
  fac[1] = 1;
  for (int i = 2; i < 200005; i++)
    fac[i] = (i * fac[i - 1]) % mod;
}
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b / 2;
  }
  return res % mod;
}
ll ncr(ll n, ll r) {
  return ((fac[n] * power(fac[r], mod - 2)) % mod *
          power(fac[n - r], mod - 2)) %
         mod;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE

signed main() {

  senky_bansal IIIT_ALLAHABAD

      // pre();
      ll t;
  // cin>>t;
  t = 1;
  while (t--) {

    ll n, k;
    cin >> n >> k;
    ll a[n];
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    set<ll> st;
    vector<ll> v1;
    st.insert(1);
    v1.pb(1);
    ll curr = 1;
    if (a[1] == 1) {
      cout << 1 << endl;
      return 0;
    }
    ll k1 = k;
    ll val = -1;
    while (k > 0) {
      curr = a[curr];
      if (st.find(curr) != st.end()) {
        val = curr;
        break;
      } else {
        st.insert(curr);
        v1.pb(curr);
      }
      k--;
    }
    // cout<<k<<endl;
    if (k > 0) {
      vector<ll> v;
      v = v1;
      ll ans = v.size();
      ll final = 0;
      for (int i = 0; i < v.size(); i++) {
        if (v[i] == val) {
          k1 -= (i);
          // cout<<k1<<" "<<ans<<endl;
          ll p2 = k1 / ans;
          ll rem = k1 % ans;
          cout << v[i + rem] << endl;
          return 0;
        } else {
          ans--;
        }
      }
    }
    cout << curr << endl;
  }
}