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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 10000000007

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N);
  vector<ll> C(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  vector<vector<ll>> score(N + 1, vector<ll>(N + 1, -INF));
  vector<pair<ll, ll>> loop(N + 1, make_pair(0, 0));
  for (int start = 1; start < N + 1; start++) {
    score[start][0] = 0;
    ll now = start;
    for (int i = 1; i < N + 1; i++) {
      score[start][i] = score[start][i - 1] + C[P[now - 1] - 1];
      now = P[now - 1];
      if (start == now) {
        loop[start].first = score[start][i];
        // cout << score[start][i] << endl;
        loop[start].second = i;
        break;
      }
    }
  }
  ll ans = -INF;
  for (int i = 1; i < N + 1; i++) {
    if (loop[i].first > 0) {
      ll time = K / loop[i].second;
      ll now = loop[i].first * time;
      ll last = K % loop[i].second;
      ll max_score = -INF;
      ll max_score1 = -INF;
      if (time == 0) {
        for (int j = 1; j <= last; j++) {
          max_score = max(max_score, score[i][j]);
        }
        now = now + max_score;
      } else {
        ll now1 = loop[i].first * (time - 1);
        for (int j = 1; j <= loop[i].second; j++) {
          max_score = max(max_score, score[i][j]);
        }
        now1 = now1 + max_score;
        for (int j = 0; j <= last; j++) {
          max_score1 = max(max_score1, score[i][j]);
        }
        now = now + max_score1;
        now = max(now1, now);
      }
      if (now > ans) {
        ans = now;
      }
    } else {
      ll now = -INF;
      for (int j = 1; j <= min(loop[i].second, K); j++) {
        now = max(now, score[i][j]);
      }
      if (now > ans) {
        ans = now;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 1000000007

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N);
  vector<ll> C(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  vector<vector<ll>> score(N + 1, vector<ll>(N, 0));
  vector<pair<ll, ll>> loop(N + 1, make_pair(0, 0));
  for (int start = 1; start < N + 1; start++) {
    score[start][0] = 0;
    int now = start;
    for (int i = 1; i < N + 1; i++) {
      score[start][i] = score[start][i - 1] + C[P[now - 1] - 1];
      now = P[now - 1];
      if (start == now) {
        loop[start].first = score[start][i];
        // cout << score[start][i] << endl;
        loop[start].second = i;
      }
    }
  }
  ll ans = -INF;
  for (int i = 1; i < N + 1; i++) {
    if (loop[i].first > 0) {

      ll time = K / loop[i].second;
      ll now = loop[i].first * time;
      ll last = K % loop[i].second;
      now = now + score[i][last];
      if (now > ans) {
        ans = now;
      }
    } else {
      ll now = -INF;
      for (int j = 1; j < min(loop[i].second, K); j++) {
        now = max(now, score[i][j]);
      }
      if (now > ans) {
        ans = now;
      }
    }
  }
  cout << ans << endl;
  return 0;
}