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

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  int P[N];
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  long long C[N];
  for (int i = 0; i < N; i++)
    cin >> C[i];
  const long long INF = 1ll << 33;
  long long ans = -INF;
  for (int i = 0; i < N; i++) {
    int M = K;
    long long sum_max = -INF;
    long long po[N];
    long long cyc_sum;
    int cyc_cnt = -1;
    int cnt[N];
    fill(po, po + N, -1);
    fill(cnt, cnt + N, 0);
    int cur = i;
    int next = P[i];
    po[i] = 0;
    cnt[i] = 1;
    for (; M; cur = next) {
      next = P[cur];
      if (cnt[next] > 0) {
        cyc_sum = po[cur] + C[next] - po[next];
        cyc_cnt = cnt[cur] + 1 - cnt[next];
        break;
      }
      po[next] = po[cur] + C[next];
      cnt[next] = cnt[cur] + 1;
      sum_max = max(sum_max, po[next]);
      M--;
    }
    if (M == 0) {
      ans = max(ans, sum_max);
      continue;
    }
    M--;
    int now = P[cur];
    long long a = po[cur] + C[now];
    if (cyc_sum > 0) {
      int b = M / cyc_cnt;
      b = max(0, b - 1);
      a += b * cyc_sum;
      sum_max = max(sum_max, a);
      M -= b * cyc_cnt;
      for (cur = now; M--; cur = P[cur]) {
        a += C[P[cur]];
        sum_max = max(sum_max, a);
      }
    } else {
      for (cur = now; M-- && P[cur] != now; cur = P[cur]) {
        a += C[P[cur]];
        sum_max = max(sum_max, a);
      }
    }
    ans = max(ans, sum_max);
  }
  cout << ans << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  int P[N];
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  long long C[N];
  for (int i = 0; i < N; i++)
    cin >> C[i];
  const long long INF = 1ll << 33;
  long long ans = -INF;
  for (int i = 0; i < N; i++) {
    int M = K;
    long long sum_max = -INF;
    long long po[N];
    long long cyc_sum;
    int cyc_cnt;
    int cnt[N];
    fill(po, po + N, -1);
    fill(cnt, cnt + N, 0);
    int cur = i;
    po[i] = 0;
    for (; M--; cur = P[cur]) {
      if (po[P[cur]] >= 0) {
        cyc_sum = po[cur] + C[P[cur]] - po[P[cur]];
        cyc_cnt = cnt[cur] + 1 - cnt[P[cur]];
        break;
      }
      po[P[cur]] = po[cur] + C[P[cur]];
      cnt[P[cur]] = cnt[cur] + 1;
      sum_max = max(sum_max, po[P[cur]]);
    }
    if (M == 0) {
      ans = max(ans, sum_max);
      continue;
    }
    int now = P[cur];
    long long a = po[cur] + C[now];
    if (cyc_sum > 0) {
      int b = M / cyc_cnt;
      b = max(0, b - 1);
      a += b * cyc_sum;
      M -= b * cyc_cnt;
      for (cur = now; M--; cur = P[cur]) {
        a += C[P[cur]];
        sum_max = max(sum_max, a);
      }
    } else {
      for (cur = now; M-- && P[cur] != now; cur = P[cur]) {
        a += C[P[cur]];
        sum_max = max(sum_max, a);
      }
    }
    ans = max(ans, sum_max);
  }
  cout << ans << endl;
  return 0;
}