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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

vector<ll> P;
vector<ll> C;

set<ll> roop(ll x, ll &point) {
  set<ll> S;
  S.insert(x);
  ll k = x;
  x = P[x - 1];
  point += C[x - 1];

  while (x != k) {
    S.insert(x);
    x = P[x - 1];
    point += C[x - 1];
  }
  return S;
}

int main() {
  ll N, K;
  cin >> N >> K;
  P = vector<ll>(N);
  for (ll i = 0; i < N; i++) {
    cin >> P[i];
  }
  C = vector<ll>(N);
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }

  vector<set<ll>> A(0);
  vector<vector<ll>> B(2, vector<ll>(0));

  vector<ll> ans(0);
  for (ll i = 1; i <= N; i++) {
    ll index = -1;
    for (ll j = 0; j < A.size(); j++) {
      if (A[j].count(i)) {
        index = j;
      }
    }
    if (index < 0) {
      ll point = 0;
      A.push_back(roop(i, point));
      B[0].push_back(A.back().size());
      B[1].push_back(point);
      index = A.size() - 1;
    }

    ll place = P[i - 1];
    ll point = C[place - 1];
    ll m = point;
    if (K < B[0][index]) {
      for (ll j = 0; j < K - 1; j++) {
        place = P[place - 1];
        point += C[place - 1];
        m = max(m, point);
      }

      ans.push_back(m);
    }

    else if (B[1][index] <= 0) {
      for (ll j = 0; j < B[0][index] - 1; j++) {
        place = P[place - 1];
        point += C[place - 1];
        m = max(m, point);
      }
      ans.push_back(m);
    }

    else {
      ll maxroop = K / (B[0][index]) * B[1][index];
      place = i;
      point = 0;
      m = 0;
      for (ll j = 0; j < K % B[0][index]; j++) {
        place = P[place - 1];
        point += C[place - 1];
        m = max(point, m);
      }
      maxroop += m;

      ll minroop = ((K / (B[0][index])) - 1) * B[1][index];

      if ((K / (B[0][index])) - 1 == 0) {
        place = P[i - 1];
        point = C[place - 1];
        m = point;
        for (ll j = 0; j < B[0][index] - 2; j++) {
          place = P[place - 1];
          point += C[place - 1];
          m = max(point, m);
        }
        minroop += m;
      }

      else {
        place = i;
        point = 0;
        m = 0;
        for (ll j = 0; j < B[0][index]; j++) {
          place = P[place - 1];
          point += C[place - 1];
          m = max(point, m);
        }
        minroop += m;
      }

      ans.push_back(max(maxroop, minroop));
    }
  }
  sort(all(ans));
  cout << ans.back() << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

vector<ll> P;
vector<ll> C;

pair<ll, ll> roop(ll x) {
  ll count = 0;
  set<ll> S;
  ll p = x;
  for (ll i = 0; i < 5000; i++) {
    if (S.count(p)) {
      pair<ll, ll> k = make_pair(p, count);
      return k;
    }
    S.insert(p);
    p = P[p - 1];
    count++;
  }
}

ll front(ll x, ll p) {
  ll count = 0;
  for (ll i = 0; i <= 5000; i++) {
    if (x == p) {
      return count;
    }
    count++;
    x = P[x - 1];
  }
}

ll get(ll x, ll p) {
  x = P[x - 1];
  ll point = C[x - 1];

  for (ll i = 0; i < 5000; i++) {
    if (x == p) {
      return point;
    }
    x = P[x - 1];
    point += C[x - 1];
  }
}

int main() {
  vector<ll> an(0);
  ll ans;
  ll N, K;
  cin >> N >> K;
  P = vector<ll>(N);
  C = vector<ll>(N);
  for (ll i = 0; i < N; i++) {
    cin >> P[i];
  }

  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }

  for (ll i = 1; i <= N; i++) {
    pair<ll, ll> r;
    r = roop(i);
    ll al, index;
    tie(index, al) = r;
    ll length = front(i, index);
    if (K <= al - 1) {
      ll place = P[place - 1];
      ll ma = C[place - 1];
      ll point = ma;
      for (ll j = 2; j <= K; j++) {
        place = P[place - 1];
        point += C[place - 1];
        ma = max(ma, point);
      }

      an.push_back(ma);
    }

    else {
      if (get(index, index) <= 0) {
        ll place = P[i - 1];
        ll ma = C[place - 1];
        ll point = ma;
        for (ll j = 0; j < al - 2; j++) {
          place = P[place - 1];
          point += C[place - 1];
          ma = max(ma, point);
        }
        an.push_back(ma);
      }

      else {
        ll place = P[i - 1];
        ll point = C[place - 1];
        ll ma = point;

        for (ll i = 0; i < length - 1; i++) {
          place = P[place - 1];
          point += C[place - 1];
          ma = max(ma, point);
        }

        ll k = K;
        k -= length;
        ll orp = get(index, index);
        ll maxroop = k / (al - length) * orp;
        k %= (al - length);
        ll minroop = maxroop - orp;
        ll nokori = 0;
        ll p = 0;
        ll iti = index;
        while (k > 0) {
          iti = P[iti - 1];
          p += C[iti - 1];
          nokori = max(nokori, p);
          k--;
        }
        maxroop += nokori;
        p = 0;
        nokori = 0;
        iti = index;
        for (ll h = 0; h < al - length - 1; h++) {
          iti = P[iti - 1];
          p += C[iti - 1];
          nokori = max(nokori, p);
        }
        minroop += nokori;
        ans = max(minroop, maxroop);
        ans = max(ans, ma);
        an.push_back(ans);
      }
    }
  }

  sort(all(an));
  cout << an.back() << endl;
}