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

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k, i, j, ans;
  cin >> n >> k;

  vector<long long> p(n), c(n);
  for (i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (i = 0; i < n; i++) {
    cin >> c[i];
  }

  vector<long long> a(n, -1);
  long long beg, cur, cur2, res, m;

  beg = 0;
  ans = -1e10;
  res = 0;
  while (beg < n) {
    if (a[beg] == 1) { // checked?
      beg++;
    } else {
      // loop size check
      a[beg] = 1;
      vector<long long> root(0);
      res = p[beg];
      root.push_back(c[res]);
      while (a[res] == -1) {
        a[res] = 1;
        res = p[res];
        root.push_back(c[res]);
      }

      m = root.size(); // loop size
      /*
      for(i=0; i<m; i++){
        cout << root[i] << ' ';
      }
      cout << endl;
      */
      vector<long long> mid(m + 1, -1e10); // max of subloop
      mid[0] = 0;
      for (i = 0; i < m; i++) {
        cur = 0;
        for (j = 0; j < m; j++) {
          cur += root[(i + j) % m];
          mid[j + 1] = max(mid[j + 1], cur);
        }
      }
      for (i = 2; i < m; i++) {
        mid[i] = max(mid[i - 1], mid[i]);
      }
      /*
      for(i=0; i<m; i++){
        cout << mid[i] << ' ';
      }
      cout << endl;
      */
      cur2 = -1e10;
      if (mid[m] > 0) {
        for (i = 0; i <= min(m - 1, k); i++) {
          cur2 = max(cur2, mid[i] + ((k - i) / m) * mid[m]);
        }
      } else {
        i = min(m - 1, k);
        cur2 = max(cur2, mid[i]);
      }

      ans = max(ans, cur2);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, k, i, j, ans;
  cin >> n >> k;

  vector<long long> p(n), c(n);
  for (i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (i = 0; i < n; i++) {
    cin >> c[i];
  }

  vector<long long> a(n, -1);
  long long beg, cur, cur2, res, m;

  beg = 0;
  ans = -1e10;
  res = 0;
  while (beg < n) {
    if (a[beg] == 1) { // checked?
      beg++;
    } else {
      // loop size check
      a[beg] = 1;
      vector<long long> root(0);
      res = p[beg];
      root.push_back(c[res]);
      while (a[res] == -1) {
        a[res] = 1;
        res = p[res];
        root.push_back(c[res]);
      }

      m = root.size(); // loop size
      /*
      for(i=0; i<m; i++){
        cout << root[i] << ' ';
      }
      cout << endl;
      */
      vector<long long> mid(m, -1e10); // max of subloop
      for (i = 0; i < m; i++) {
        cur = 0;
        for (j = 0; j < m; j++) {
          cur += root[(i + j) % m];
          mid[j] = max(mid[j], cur);
        }
      }
      for (i = 1; i < m - 1; i++) {
        mid[i] = max(mid[i - 1], mid[i]);
      }
      /*
      for(i=0; i<m; i++){
        cout << mid[i] << ' ';
      }
      cout << endl;
      */
      cur2 = -1e10;
      if (mid[m - 1] > 0) {
        for (i = 0; i < min(m - 1, k - 1); i++) {
          cur2 = max(cur2, mid[i] + ((k - i - 1) / m) * mid[m - 1]);
        }
      } else {
        for (i = 0; i < max(m - 1, k - 1); i++) {
          cur2 = max(cur2, mid[i]);
        }
      }

      ans = max(ans, cur2);
    }
  }
  cout << ans << endl;
}