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

/*
 * Author : Andrew J
 */
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ll = long long;
typedef pair<ll, ll> ii;
void in(ll &x);
const ll N = 5005;
ll n, k, nxt[N], price[N], ps[2 * N];
bool vis[N];
vector<ll> v[N];

void dfs(ll u, ll cur) {
  if (vis[u])
    return;
  vis[u] = 1;
  v[cur].pb(price[u]);
  dfs(nxt[u], cur);
  return;
}

ll rsum(ll l, ll r) {
  if (l > r)
    return 0;
  if (l == 0)
    return ps[r];
  return ps[r] - ps[l - 1];
}

int main() {
  in(n);
  in(k);
  for (ll u = 1, nn; u <= n; u++) {
    in(nn);
    nxt[u] = nn;
  }
  for (ll i = 1; i <= n; i++) {
    in(price[i]);
  }
  ll idx = 0;
  for (ll u = 1; u <= n; u++) {
    if (!vis[u]) {
      dfs(u, idx++);
    }
  }
  for (ll i = 0; i < idx; i++) {
    ll vz = v[i].size();
    for (ll j = 0; j < vz; j++) {
      v[i].pb(v[i][j]);
    }
  }
  ll sol = LLONG_MIN;
  for (ll i = 0; i < idx; i++) {
    ll vz = v[i].size();
    ps[0] = v[i][0];
    for (ll j = 1; j < vz; j++) {
      ps[j] = ps[j - 1] + v[i][j];
    }
    for (ll j = 0; j < vz; j++) {
      for (ll len = 1; len <= min(vz / 2, k); len++) {
        if (j + len - 1 >= vz)
          continue;
        sol = max(sol, rsum(j, j + len - 1));
      }
      if (k > vz / 2) {
        if (k % (vz / 2) == 0) {
          for (int len = 0; len <= vz / 2; len++) {
            if (j + len - 1 >= vz)
              continue;
            if (j + (vz / 2) - 1 >= vz)
              continue;
            sol = max(sol, rsum(j, j + (vz / 2) - 1) * ((k / (vz / 2)) - 1) +
                               rsum(j, j + len - 1));
          }
        } else {
          for (ll len = 0; len <= k % (vz / 2); len++) {
            if (j + len - 1 >= vz)
              continue;
            if (j + (vz / 2) - 1 >= vz)
              continue;
            sol = max(sol, rsum(j, j + (vz / 2) - 1) * (k / (vz / 2)) +
                               rsum(j, j + len - 1));
          }
        }
      }
    }
  }
  printf("%lld\\n", sol);
  return 0;
}

void in(ll &x) {
  bool neg = false;
  register ll c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}


Error: -11

Fixed submission:

/*
 * Author : Andrew J
 */
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ll = long long;
typedef pair<ll, ll> ii;
void in(ll &x);
const ll N = 5005;
ll n, k, nxt[N], price[N], ps[2 * N], add[N][N];
bool vis[N];
vector<ll> v[N];

void dfs(ll u, ll cur) {
  if (vis[u])
    return;
  vis[u] = 1;
  v[cur].pb(price[u]);
  dfs(nxt[u], cur);
  return;
}

ll rsum(ll l, ll r) {
  if (l > r)
    return 0;
  if (l == 0)
    return ps[r];
  return ps[r] - ps[l - 1];
}

int main() {
  in(n);
  in(k);
  for (ll u = 1, v; u <= n; u++) {
    in(v);
    nxt[u] = v;
  }
  for (ll i = 1; i <= n; i++) {
    in(price[i]);
  }
  ll idx = 0;
  for (ll u = 1; u <= n; u++) {
    if (!vis[u]) {
      dfs(u, idx++);
    }
  }
  for (ll i = 0; i < idx; i++) {
    ll vz = v[i].size();
    for (ll j = 0; j < vz; j++) {
      v[i].pb(v[i][j]);
    }
  }
  ll sol = LLONG_MIN;
  for (ll i = 0; i < idx; i++) {
    ll vz = v[i].size();
    ps[0] = v[i][0];
    for (ll j = 1; j < vz; j++) {
      ps[j] = ps[j - 1] + v[i][j];
    }
    for (ll j = 0; j < vz; j++) {
      for (ll len = 1; len <= min(vz / 2, k); len++) {
        if (j + len - 1 >= vz)
          continue;
        ll cur = rsum(j, j + len - 1);
        add[j][len] = max(add[j][len], cur);
        sol = max(sol, rsum(j, j + len - 1));
      }
      if (k > vz / 2) {
        sol = max(sol, rsum(0, (vz / 2) - 1) * (k / (vz / 2)) +
                           add[j][k % (vz / 2)]);
      }
    }
  }
  printf("%lld\\n", sol);
  return 0;
}

void in(ll &x) {
  bool neg = false;
  register ll c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}