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

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
int main() {
  ll N, K;
  cin >> N >> K;
  vll P(N), C(N);
  for (ll i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }
  ll ans = -INF;

  for (ll i = 0; i < N; i++) {
    vector<bool> seen(N);
    vll score;
    ll cur = i;
    ll curScore = 0;
    while (!seen[P[cur]]) {
      cur = P[cur];
      seen[cur] = true;
      curScore += C[cur];
      score.push_back(curScore);
    }
    // ループおわり
    ll loopLen = score.size();
    ll rem = K % loopLen;
    ll div = K / loopLen;
    if (score.back() >= 0 and loopLen <= K) {
      chmax(ans, div * score.back());
      for (ll j = 0; j < rem; j++) {
        chmax(ans, div * score.back() + score[j]);
      }
      if (rem == 0) {
        for (ll j = 0; j < loopLen and j < K; j++) {
          chmax(ans, (div - 1) * score.back() + score[j]);
        }
      }
    }
    for (ll j = 0; j < loopLen and j < K; j++) {
      chmax(ans, score[j]);
    }
  }
  cout << ans;
  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
int main() {
  ll N, K;
  cin >> N >> K;
  vll P(N), C(N), score;
  for (ll i = 0; i < N; i++) {
    cin >> P[i];
    P[i]--;
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }
  ll ans = -INF;
  vector<bool> seen(N);
  for (ll i = 0; i < N; i++) {
    score.clear();
    seen.clear();
    ll cur = i;
    ll curScore = 0;
    while (!seen[P[cur]]) {
      cur = P[cur];
      seen[cur] = true;
      curScore += C[cur];
      score.push_back(curScore);
    }
    // ループおわり
    ll loopLen = score.size();
    if (score.back() > 0 and loopLen < K) {
      ll rem = K % loopLen;
      ll div = K / loopLen;
      chmax(ans, div * score.back());
      for (ll j = 0; j < rem; j++) {
        chmax(ans, div * score.back() + score[j]);
      }
    }
    for (ll j = 0; j < score.size() and j < K; j++) {
      chmax(ans, score[j]);
    }
  }
  cout << ans;
  return 0;
}