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

#include <bits/stdc++.h>
#define fint register int
#define h 5010
#define p h
#define int long long
using namespace std;
struct node {
  int to;
  int nxt;
  int val;
  int from;
} e[p];
int cnt;
int head[p];
int a[p], pt[p];
int st[p];
int f[h][h];
bool vis[p];
int n, k;
int tot;
int maxxs = -1e18;
inline int read();
inline void dfs(int x, int step);
inline void adds(int u, int v, int w);
signed main() {
  n = read(), k = read();
  for (fint i = 1; i <= n; i++)
    a[i] = read();
  for (fint i = 1; i <= n; i++)
    pt[i] = read();
  //	for(fint i=1;i<=n;i++)
  //	adds(i,a[i],pt[a[i]]),f[i][a[i]]=pt[a[i]];//建图
  //	for(fint i=1;i<=n;i++)
  //	for(fint j=1;j<=n;j++)
  //	cout<<f[i][j]<<" ";
  /*for(fint i=1;i<=n;i++)
  tot=0,dfs(i,0);
  cout<<maxxs;暴力做法，思路显然，复杂度O(k*n^2)*/
  int stp = 0;
  for (fint i = 1; i <= n; i++) {
    memset(vis, 0, sizeof(vis));
    int hea = a[i];
    while (!vis[hea])
      vis[hea] = 1, stp++, f[i][stp] = f[i][stp - 1] + pt[hea],
      hea = a[hea]; // 记录每一步从i开始每一步推进的路径
    st[i] = stp, stp = 0;
  }
  //	for(fint i=1;i<=n;i++)
  //	for(fint j=1;j<=n;j++)
  //	cout<<f[i][j]<<" ";
  for (fint i = 1; i <= n; i++)
    for (fint j = 1; j <= st[i]; j++) {
      if (j > k)
        break; // 到哪个点停
      int ans = f[i][j];
      if (f[i][st[i]] >= 0)
        ans += (k - j) / st[i] *
               f[i][st[i]]; // 更新答案，此时剩余可走步数/环步数*到环所需步数
      maxxs = max(maxxs, ans);
    }
  cout << maxxs;
  return 0;
}

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

inline void adds(int u, int v, int w) {
  e[++cnt].to = v;
  e[cnt].from = u;
  e[cnt].nxt = head[u];
  e[cnt].val = w;
  head[u] = cnt;
  return;
}
/*
inline void dfs(int x,int step)
{
        if(step==k)
        return ;
        for(fint i=head[x];i;i=e[i].nxt)
        {
                tot+=e[i].val;
                maxxs=max(tot,maxxs);
                dfs(e[i].to,step+1);
        }
        return ;
}暴力搜索求出最大权值 */

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define fint register int
#define h 5001
#define p 489573
#define int long long
using namespace std;
struct node {
  int to;
  int nxt;
  int val;
  int from;
} e[p];
int cnt;
int head[p];
int a[p], pt[p];
int f[h][h];
int n, k;
int tot;
int maxxs = -45893458938;
inline int read();
inline void dfs(int x, int step);
inline void adds(int u, int v, int w);
signed main() {
  n = read(), k = read();
  for (fint i = 1; i <= n; i++)
    a[i] = read();
  for (fint i = 1; i <= n; i++)
    pt[i] = read();
  for (fint i = 1; i <= n; i++)
    adds(i, a[i], pt[a[i]]); // 建图
  //	for(fint i=1;i<=n;i++)
  //	for(fint j=1;j<=n;j++)
  //	cout<<f[i][j]<<" ";
  for (fint i = 1; i <= n; i++)
    tot = 0, dfs(i, 0);
  cout << maxxs;
  return 0;
}

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

inline void adds(int u, int v, int w) {
  e[++cnt].to = v;
  e[cnt].from = u;
  e[cnt].nxt = head[u];
  e[cnt].val = w;
  head[u] = cnt;
  return;
}

inline void dfs(int x, int step) {
  if (step == k)
    return;
  for (fint i = head[x]; i; i = e[i].nxt) {
    tot += e[i].val;
    maxxs = max(tot, maxxs);
    dfs(e[i].to, step + 1);
  }
  return;
}