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

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 5010;
int p[maxn], n, K, c[maxn];
bool vis[maxn];
LL add[maxn];
LL step[maxn];
LL work(int st) {
  // cout<<"st = "<<st<<endl;
  for (int i = 1; i <= n; i++) {
    vis[i] = false;
    add[i] = -1000000001;
    step[i] = 0;
  }
  LL ans = c[st];
  add[st] = 0;
  int k = K;
  while (k) {
    int nx = p[st];
    if (vis[nx]) {
      LL ADD = add[st] + c[nx] - add[nx];
      LL times = step[st] + 1 - step[nx];
      if (ADD > 0) {
        LL tt = (k - 1) / times;
        LL tmp = add[st] + tt * ADD;
        step[st] += tt * times;
        // cout<<"st = "<<st<<"\\tnx = "<<nx<<"\\tpre k = "<<k<<endl;
        k -= tt * times;
        if (tmp > ans)
          ans = tmp;
        for (int i = 1; i <= n; i++)
          if (i != st) {
            vis[i] = false;
            add[i] = -1000000001;
            step[i] = 0;
          }
        add[st] = tmp;
        // cout<<"st = "<<st<<"\\tADD = "<<ADD<<"\\ttimes = "<<times<<endl;
        // cout<<"add[st] = "<<add[st]<<endl;
        // cout<<"k = "<<k<<endl;
      } else
        break;
    } else {
      vis[nx] = true;
      step[nx] = step[st] + 1;
      add[nx] = add[st] + c[nx];
      if (add[nx] > ans)
        ans = add[nx];
      k--;
      st = nx;
      // cout<<"k = "<<k<<"--- st = "<<st<<"\\tadd = "<<add[st]<<"\\tans =
      // "<<ans<<endl;
    }
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  LL ans = c[1];
  for (int i = 1; i <= n; i++) {
    LL tmp = work(i);
    if (tmp > ans)
      ans = tmp;
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 3010;
int p[maxn], n, K, c[maxn];
bool vis[maxn];
LL add[maxn];
LL step[maxn];
LL work(int st) {
  // cout<<"st = "<<st<<endl;
  for (int i = 1; i <= n; i++) {
    vis[i] = false;
    add[i] = -1000000001;
    step[i] = 0;
  }
  LL ans = c[st];
  add[st] = 0;
  int k = K;
  while (k) {
    int nx = p[st];
    if (vis[nx]) {
      LL ADD = add[st] + c[nx] - add[nx];
      LL times = step[st] + 1 - step[nx];
      if (ADD > 0) {
        LL tmp = add[st] + (k / times) * ADD;
        k -= k / times * times;
        if (ADD > ans)
          ans = ADD;
        for (int i = 1; i <= n; i++)
          if (i != st) {
            vis[i] = false;
            add[i] = -1000000001;
            step[i] = 0;
          }
        add[st] = tmp;
        step[st] += k / times * times;
      } else
        break;
    } else {
      vis[nx] = true;
      step[nx] = step[st] + 1;
      add[nx] = add[st] + c[nx];
      if (add[nx] > ans)
        ans = add[nx];
      k--;
      st = nx;
      // cout<<"st = "<<st<<"\\tadd = "<<add[st]<<"\\tans = "<<ans<<endl;
    }
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  LL ans = c[1];
  for (int i = 1; i <= n; i++) {
    LL tmp = work(i);
    if (tmp > ans)
      ans = tmp;
  }
  printf("%lld\\n", ans);
  return 0;
}