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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
bool vis[5009];
vector<pair<ll, ll>> vec;
ll pp[5009];
ll val[5009];
int main() {
  ll a, b, c, d, e, i, j, k, l, n, m, x, y, t, p;
  scanf("%lld%lld", &n, &k);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &pp[i]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%lld", &val[i]);
  }
  ll ans = -(5e18);
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      vis[j] = false;
    }
    vec.clear();
    a = i;
    m = 0;
    y = 0;
    while (1) {
      m++;
      if (m > k) {
        m--;
        break;
      }
      a = pp[a];

      if (vis[a] == true) {
        m--;
        break;
      } else {
        vis[a] = true;
        y += val[a];
        ans = max(ans, y);
        vec.push_back({a, val[a]});
      }
    }
    if (m == k) {
      continue;
    }

    d = (k - m);
    c = 0;
    x = 0;
    l = vec.size();
    e = m;
    ll ff = y;
    ll pp = 0;
    for (j = 0; j < l; j++) {
      if (vec[j].first == a) {
        pp = j;
        for (ll ii = j; ii < l; ii++) {
          if (e < k) {
            ff += vec[ii].second;
            e++;
            ans = max(ans, ff);
          }
          c++;
          x += vec[ii].second;
        }
        break;
      }
    }

    ll aa = (d / c);
    y += (aa * x);
    ans = max(ans, y);
    aa = d % c;
    l = min(l, pp + aa);
    ll xx = 0;
    for (j = pp; j < l; j++) {
      y += vec[j].second;
      xx += vec[j].second;
      ans = max(ans, y);
    }
    if ((d / c) > 0) {
      y -= x;
      y -= xx;
      ans = max(ans, x);
      l = vec.size();
      for (j = pp; j < l; j++) {
        y += vec[j].second;
        ans = max(ans, y);
      }
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
bool vis[50009];
vector<pair<ll, ll>> vec;
ll pp[50009];
ll val[50009];
int main() {
  ll a, b, c, d, e, i, j, k, l, n, m, x, y, t, p;
  scanf("%lld%lld", &n, &k);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &pp[i]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%lld", &val[i]);
  }
  ll ans = -(2e18);
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      vis[j] = false;
    }
    vec.clear();
    a = i;
    m = 0;
    y = 0;
    while (1) {
      m++;
      if (m > k) {
        m--;
        break;
      }
      a = pp[a];

      if (vis[a] == true) {
        m--;
        break;
      } else {
        y += val[a];
        ans = max(ans, y);
        vec.push_back({a, val[a]});
      }
    }

    d = (k - m);
    c = 0;
    x = 0;
    l = vec.size();
    e = m;
    ll ff = y;
    ll pp = 0;
    for (j = 0; j < l; j++) {
      if (vec[j].first == a) {
        pp = j;
        for (ll ii = j; ii < l; ii++) {
          if (e < k) {
            ff += vec[ii].second;
            e++;
            ans = max(ans, ff);
          }
          c++;
          x += vec[ii].second;
        }
        break;
      }
    }

    ll aa = (d / c);
    y += (aa * x);
    aa = d % c;
    l = min(l, pp + aa);
    for (j = pp; j < l; j++) {
      y += vec[j].second;
      ans = max(ans, y);
    }
  }

  cout << ans << endl;
}