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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  ll K;
  cin >> K;
  ll ans = -mod;
  int P[N];
  rep(i, N) {
    int a;
    cin >> a;
    P[i] = a - 1;
  }
  ll C[N];
  rep(i, N) {
    ll c;
    cin >> c;
    C[i] = c;
    ans = max(ans, c);
  }

  if (K > N) {
    rep(i, N) {
      // 一回の周期で迎える最大スコアと周期の長さを求める
      int pos = P[i];
      ll len = 1;
      ll mxlen = 1;
      ll mx = C[pos];
      ll acc = C[pos];
      while (pos != i) {
        pos = P[pos];
        acc += C[pos]; // 行き先のスコアを足す
        // mx = max(mx, acc);
        len++;
        if (acc > mx) {
          mx = acc;
          mxlen = len;
        }
      }
      if (acc <= 0) {
        ans = max(ans, mx);
      } else {
        // K - n*len >= 0
        // n <= K/len
        ll num_loop = K / len - 1;
        ll rem = K - num_loop * len;
        ll ac2 = 0;
        int ps2 = i;
        ll mx2 = 0;
        rep(j, rem) {
          ps2 = P[ps2];
          ac2 += C[ps2];
          mx2 = max(mx2, ac2);
        }
        ans = max(ans, acc * num_loop + mx2);
        /*
                        ll K2 = K - mxlen;
                        ll num_loop2 = K2/len;
                        ll rem2 = K2 - num_loop2*len;
                        ll ac3 = 0;
                        int ps3 = i;
                        ll mx3 = 0;
                        rep(j, rem2){
                            ps3 = P[ps3];
                            ac3 += C[ac3];
                            mx3 = max(mx3, ac3);
                        }
                        ans = max(ans, acc*num_loop2 + mx3);
        */
      }
    }
    cout << ans << endl;
  } else {
    rep(i, N) {
      ll acc = 0;
      int pos = i;
      rep(j, K) {
        pos = P[pos];
        acc += C[pos];
        ans = max(ans, acc);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  ll K;
  cin >> K;
  ll ans = -mod;
  int P[N];
  rep(i, N) {
    int a;
    cin >> a;
    P[i] = a - 1;
  }
  ll C[N];
  rep(i, N) {
    ll c;
    cin >> c;
    C[i] = c;
    ans = max(ans, c);
  }

  if (K > N) {
    rep(i, N) {
      // 一回の周期で迎える最大スコアと周期の長さを求める
      int pos = P[i];
      ll len = 1;
      ll mxlen = 1;
      ll mx = C[pos];
      ll acc = C[pos];
      while (pos != i) {
        pos = P[pos];
        acc += C[pos]; // 行き先のスコアを足す
        // mx = max(mx, acc);
        len++;
        if (acc > mx) {
          mx = acc;
          mxlen = len;
        }
      }
      if (acc <= 0) {
        ans = max(ans, mx);
      } else {
        // K - n*len >= 0
        // n <= K/len
        ll num_loop = K / len;
        ll rem = K - num_loop * len;
        ll ac2 = 0;
        int ps2 = i;
        ll mx2 = 0;
        rep(j, rem) {
          ps2 = P[ps2];
          ac2 += C[ps2];
          mx2 = max(mx2, ac2);
        }
        ans = max(ans, acc * num_loop + mx2);

        ll K2 = K - mxlen;
        ll num_loop2 = K2 / len;
        ll rem2 = K2 - num_loop2 * len;
        ll ac3 = 0;
        int ps3 = i;
        ll mx3 = 0;
        rep(j, rem2) {
          ps3 = P[ps3];
          ac3 += C[ac3];
          mx3 = max(mx3, ac3);
        }
        ans = max(ans, acc * num_loop2 + mx3);
      }
    }
    cout << ans << endl;
  } else {
    rep(i, N) {
      ll acc = 0;
      int pos = i;
      rep(j, K) {
        pos = P[pos];
        acc += C[pos];
        ans = max(ans, acc);
      }
    }
    cout << ans << endl;
  }
  return 0;
}