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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool read(T &x) {
  x = 0;
  register char c = getchar();
  register bool f = 0;
  while (!isdigit(c)) {
    if (c == EOF)
      return false;
    f ^= c == '-', c = getchar();
  }
  while (isdigit(c))
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return true;
}
template <class T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 ^ 48);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }
template <class T, class... S> inline bool read(T &x, S &...y) {
  return read(x) && read(y...);
}

ll temp, ans, b[5055], c[5055], cnt, k;
int p[5055], now, n;
int main() {
  read(n, k);
  for (int i = 1; i <= n; i++)
    read(p[i]);
  for (int i = 1; i <= n; i++)
    read(c[i]);
  ans = c[1];
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    now = i;
    while (cnt == 0 || now != i)
      cnt++, now = p[now], b[cnt] = b[cnt - 1] + c[now];
    if (cnt >= k) {
      temp = b[1];
      for (ll j = 1; j <= k; j++)
        temp = max(temp, b[j]);
    } else {
      if (b[cnt] > 0) {
        temp = b[1];
        for (ll j = 1; j <= k % cnt; j++)
          temp = max(temp, k / cnt * b[cnt] + b[j]);
        for (ll j = 1; j <= cnt; j++)
          temp = max(temp, (k / cnt - 1) * b[cnt] + b[j]);
      } else {
        temp = b[1];
        for (ll j = 1; j <= cnt; j++)
          temp = max(temp, b[j]);
      }
    }
    ans = max(ans, temp);
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool read(T &x) {
  x = 0;
  register char c = getchar();
  register bool f = 0;
  while (!isdigit(c)) {
    if (c == EOF)
      return false;
    f ^= c == '-', c = getchar();
  }
  while (isdigit(c))
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return true;
}
template <class T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 ^ 48);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }
template <class T, class... S> inline bool read(T &x, S &...y) {
  return read(x) && read(y...);
}

ll temp, ans, b[5055], c[5055], cnt, k;
int p[5055], now, n;
int main() {
  read(n, k);
  for (int i = 1; i <= n; i++)
    read(p[i]);
  for (int i = 1; i <= n; i++)
    read(c[i]);
  ans = c[1];
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    now = i;
    while (cnt == 0 || now != i)
      cnt++, now = p[now], b[cnt] = b[cnt - 1] + c[now];
    if (cnt >= k) {
      temp = b[1];
      for (int j = 1; j <= cnt; j++)
        temp = max(temp, b[j]);
    } else {
      if (b[cnt] > 0) {
        temp = k / cnt * b[cnt];
        for (ll j = 1; j <= k % cnt; j++)
          temp = max(temp, k / cnt * b[cnt] + b[j]);
      } else {
        temp = b[1];
        for (int j = 1; j <= k; j++)
          temp = max(temp, b[j]);
      }
    }
    ans = max(ans, temp);
  }
  printf("%lld\\n", ans);
  return 0;
}