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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 2e18;
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 <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) cin >> p[i], p[i]--;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];

  ll ans = *max_element(all(c));
  vector<ll> score;
  rep(i, n) {
    score.clear();
    int to = i;
    do {
      to = p[to];
      score.push_back(c[to]);
    } while (to != i);

    ll d = (ll)(sz(score));
    rep(j, d - 1) score[j + 1] += score[j];

    if (k <= d) {
      rep(j, k) chmax(ans, score[j]);
    }

    else {
      if (score[d - 1] <= 0) {
        rep(j, d) chmax(ans, score[j]);
      }

      else {
        chmax(ans, score[d - 1] * (k / d));
        rep(j, k % d) chmax(ans, score[d - 1] * (k / d) + score[j]);
        if (k / d > 0)
          rep(j, d) chmax(ans, score[d - 1] * (k / d - 1) + score[j]);
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 2e18;
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 <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> p(n);
  rep(i, n) cin >> p[i], p[i]--;
  vector<ll> c(n);
  rep(i, n) cin >> c[i];

  ll ans = -LINF;
  vector<ll> score;
  rep(i, n) {
    score.clear();
    int to = i;
    do {
      to = p[to];
      if (sz(score))
        score.push_back(score.back() + c[to]);
      else
        score.push_back(c[to]);
    } while (to != i);

    ll d = (ll)(sz(score));

    if (k <= d) {
      rep(j, k) chmax(ans, score[j]);
    }

    else {
      if (score[d - 1] <= 0) {
        rep(j, d) chmax(ans, score[j]);
      }

      else {
        rep(j, k % d) chmax(ans, (k / d) * score[d - 1] + score[j]);
      }
    }
  }
  assert(ans > -LINF);
  cout << ans << endl;
}