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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 1e5 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int p[MAX];
int c[MAX];
int biao[MAX];

ll res = -INF;
ll tem = -INF;
ll dfs(ll u, ll fen) {
  if (biao[p[u]] != 0) {
    return biao[u];
  }
  biao[p[u]] = biao[u] + 1;
  tem = max(tem, fen + c[p[u]]);
  return dfs(p[u], fen + c[p[u]]);
}

ll dfs2(ll u, ll fen, ll ci, ll deep) {
  if (deep == ci) {
    return fen;
  }
  return dfs2(p[u], fen + c[p[u]], ci, deep + 1);
}

ll dfs3(ll u, ll fen, ll ci, ll deep) {
  if (ci == deep) {
    return fen;
  }
  tem = max(tem, fen + c[p[u]]);
  return dfs3(p[u], fen + c[p[u]], ci, deep + 1);
}
void solve() {
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (ll i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (ll i = 1; i <= n; i++) {
    memset(biao, 0, sizeof(biao));
    tem = -INF;
    biao[i] = 1;
    ll ci = dfs(i, 0);
    if (ci >= k) {
      tem = -INF;
      dfs3(i, 0, k, 0);
      res = max(res, tem);
    } else {
      ll shu = dfs3(i, 0, ci, 0);
      if (shu > 0) {
        ll jici = k / ci;
        ll sheng = k % ci;
        ll zhi = jici * shu;
        tem = -INF;
        if (sheng != 0) {
          dfs3(i, 0, sheng, 0);
        } else {
          zhi = zhi - shu;
          dfs3(i, 0, ci, 0);
        }
        zhi = zhi + (tem > 0 ? tem : 0);
        res = max(res, zhi);
      } else {
        res = max(res, tem);
      }
    }
    // cout<<"----------"<<ci<<" "<< tem<<" "<<dfs2(i,0,ci,0)<<'\\n';
  }
  cout << res;
}

int main() {
  int _ = 1;
  // cin >> _;
  while (_--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 1e5 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int p[MAX];
int c[MAX];
int biao[MAX];

ll res = -INF;
ll tem = -INF;
ll dfs(ll u, ll fen) {
  if (biao[p[u]] != 0) {
    return biao[u];
  }
  biao[p[u]] = biao[u] + 1;
  tem = max(tem, fen + c[p[u]]);
  dfs(p[u], fen + c[p[u]]);
}

ll dfs2(ll u, ll fen, ll ci, ll deep) {
  if (deep == ci) {
    return fen;
  }
  dfs2(p[u], fen + c[p[u]], ci, deep + 1);
}

ll dfs3(ll u, ll fen, ll ci, ll deep) {
  if (ci == deep) {
    return fen;
  }
  tem = max(tem, fen + c[p[u]]);
  dfs3(p[u], fen + c[p[u]], ci, deep + 1);
}
void solve() {
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (ll i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (ll i = 1; i <= n; i++) {
    memset(biao, 0, sizeof(biao));
    tem = -INF;
    biao[i] = 1;
    ll ci = dfs(i, 0);
    if (ci >= k) {
      tem = -INF;
      dfs3(i, 0, k, 0);
      res = max(res, tem);
    } else {
      ll shu = dfs2(i, 0, ci, 0);
      if (shu > 0) {
        ll jici = k / ci;
        ll sheng = k % ci;
        ll zhi = jici * shu;
        tem = -INF;
        dfs3(i, 0, sheng, 0);
        zhi = zhi + (tem > 0 ? tem : 0);
        res = max(res, zhi);
      } else {
        res = max(res, tem);
      }
    }
    // cout<<"----------"<<ci<<" "<< tem<<" "<<dfs2(i,0,ci,0)<<'\\n';
  }
  cout << res;
}

int main() {
  int _ = 1;
  // cin >> _;
  while (_--) {
    solve();
  }
  return 0;
}