Problem p02585 (C++)

Buggy submission (status: Runtime Error):
/*
        ID: Loxilante
        Time: 2020/08/16
        Prog: ABC175D
        Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
const int U = 5e3 + 50;
const int INF = 1e18;
int to[U], val[U], ct[U], t;
bool visited[U];
vector<int> loop;
inline void dfs(int e) {
  if (visited[e])
    return;
  visited[e] = 1;
  t += val[e];
  loop.pb(t);
  dfs(to[e]);
}
signed main(void) {
  clock_t Begin = clock();

#ifdef JOEON
  //		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
  //		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k, ans = -INF;
  cin >> n >> k;
  rep(i, 0, n) to[i] = next<int>() - 1;
  rep(i, 0, n) cin >> val[i];

  clock_t InputFinished = clock();

  rep(i, 0, n) {
    ms(visited, 0);
    loop.clear();
    t = 0;
    dfs(i);
    int times = k / loop.size();
    if (loop.back() > 0 && k >= loop.size()) {
      if (times >= 1) {
        int ans1 = -INF;
        rep(j, 0, loop.size()) ans1 = max(ans1, loop[j]);
        ans = max(loop.back() * (times - 1) + ans1, ans);
      }

      int rest = k % loop.size(), ans2 = 0;
      rep(j, 0, rest) ans2 = max(ans2, loop[j]);
      ans = max(loop.back() * times + ans2, ans);
    } else
      rep(j, 0, k) ans = max(ans, loop[j]);
  }

  cout << ans << endl;

  clock_t End = clock();

  D((double)(End - Begin) / CLOCKS_PER_SEC);
  D((double)(End - InputFinished) / CLOCKS_PER_SEC);

  return 0;
}
/*

 */

Fixed submission:
/*
        ID: Loxilante
        Time: 2020/08/16
        Prog: ABC175D
        Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i < r; i++)
#define hrp(i, l, r) for (register int i = l; i <= r; i++)
#define rev(i, r, l) for (register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
const int U = 5e3 + 50;
const int INF = 1e18;
int to[U], val[U], ct[U], t;
bool visited[U];
vector<int> loop;
inline void dfs(int e) {
  if (visited[e])
    return;
  visited[e] = 1;
  t += val[e];
  loop.pb(t);
  dfs(to[e]);
}
signed main(void) {
  clock_t Begin = clock();

#ifdef JOEON
  //		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
  //		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k, ans = -INF;
  cin >> n >> k;
  rep(i, 0, n) to[i] = next<int>() - 1;
  rep(i, 0, n) cin >> val[i];

  clock_t InputFinished = clock();

  rep(i, 0, n) {
    ms(visited, 0);
    loop.clear();
    t = 0;
    dfs(i);
    int times = k / loop.size();
    if (loop.back() > 0 && k >= loop.size()) {
      if (times >= 1) {
        int ans1 = -INF;
        rep(j, 0, loop.size()) ans1 = max(ans1, loop[j]);
        ans = max(loop.back() * (times - 1) + ans1, ans);
      }

      int rest = k % loop.size(), ans2 = 0;
      rep(j, 0, rest) ans2 = max(ans2, loop[j]);
      ans = max(loop.back() * times + ans2, ans);
    } else
      rep(j, 0, k) {
        if (j >= loop.size())
          break;
        ans = max(ans, loop[j]);
      }
  }

  cout << ans << endl;

  clock_t End = clock();

  D((double)(End - Begin) / CLOCKS_PER_SEC);
  D((double)(End - InputFinished) / CLOCKS_PER_SEC);

  return 0;
}
/*

 */