Problem p02585 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N);
  vector<ll> C(N);
  REP(i, N) cin >> P[i];
  REP(i, N) P[i]--;
  REP(i, N) cin >> C[i];
  ll max = -2000000000;
  REP(i, N) {
    if (P[i] == -1)
      continue;
    vector<ll> A(0);
    A.pb(C[i]);
    ll bef = P[i];
    P[i] = -1;
    while (1) {
      A.pb(C[bef]);
      ll befo = bef;
      bef = P[bef];
      P[befo] = -1;
      if (bef < 0)
        break;
      if (P[bef] == -1)
        break;
    }
    ll score = 0;
    ll nokori = K;
    ll allsum = 0;
    REP(l, A.size()) allsum += A[l];
    if (allsum <= 0) {
      nokori = min(K, (ll)A.size() - 1);
    } else {
      if (nokori / A.size() > 1) {
        score += (nokori / A.size() - 1) * allsum;
        nokori %= A.size();
        nokori += A.size();
      }
    }
    ll siz = A.size();
    REP(l, siz) { A.pb(A[l]); }
    REP(l, siz) { A.pb(A[l]); }
    vector<ll> rui(A.size() + 1);
    REP(i, A.size()) rui[i + 1] = A[i] + rui[i];
    ll maxpls = -2000000000;
    REP(l, A.size() * 2 / 3) {
      REPS(al, nokori) { chmax(maxpls, rui[l + al] - rui[l]); }
    }
    score += maxpls;
    chmax(max, score);
  }
  cout << max << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll N, K;
  cin >> N >> K;
  vector<ll> P(N);
  vector<ll> C(N);
  REP(i, N) cin >> P[i];
  REP(i, N) P[i]--;
  REP(i, N) cin >> C[i];
  ll max = -2000000000;
  REP(i, N) {
    if (P[i] == -1)
      continue;
    vector<ll> A(0);
    A.pb(C[i]);
    ll bef = P[i];
    P[i] = -1;
    while (1) {
      A.pb(C[bef]);
      ll befo = bef;
      bef = P[bef];
      P[befo] = -1;
      if (bef < 0)
        break;
      if (P[bef] == -1)
        break;
    }
    ll score = 0;
    ll nokori = K;
    ll allsum = 0;
    REP(l, A.size()) allsum += A[l];
    if (allsum <= 0) {
      nokori = min(K, (ll)A.size() - 1);
    } else {
      if (nokori / A.size() > 1) {
        score += (nokori / A.size() - 1) * allsum;
        nokori %= A.size();
        nokori += A.size();
      }
    }
    ll siz = A.size();
    REP(l, siz) { A.pb(A[l]); }
    REP(l, siz) { A.pb(A[l]); }
    vector<ll> rui(A.size() + 1);
    REP(i, A.size()) rui[i + 1] = A[i] + rui[i];
    ll maxpls = -2000000000;
    REP(l, A.size() * 2 / 3) {
      REPS(al, nokori) {
        if (l + al >= A.size())
          continue;
        chmax(maxpls, rui[l + al] - rui[l]);
      }
    }
    score += maxpls;
    chmax(max, score);
  }
  cout << max << "\n";
}