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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifdef local
#include "debug/debug.h"
#endif

#ifndef local
#define deb(x...) ;
#endif

#define int long long
#define ll long long
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vi>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i > b; i--)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define M_PI 3.14159265358979323846

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
///*X.find_by_order("index")
/// X.order_of_key("key")

///----------------------------------------------------------------------------------

int n, k, p[5001], c[5001];
bool vis[5001];
vvi chains;

int f(vi &chain) {
  vi vals;
  int sum = 0;
  for (int index : chain)
    vals.pb(c[index]), sum += c[index];
  rep(i, 0, chain.size()) vals.pb(vals[i]);

  vi pref(2 * n);
  pref[0] = vals[0];
  rep(i, 1, 2 * chain.size()) pref[i] = pref[i - 1] + vals[i];

  int res = 0;
  bool started = false;
  if (sum >= 0)
    res += sum * (k / chain.size()), started = true;

  if (started) {
    int rem = k % chain.size();
    int best = LLONG_MIN;
    if (rem == 0) {
      rem = chain.size() - 1;
      res -= sum;
      best = max(best, sum);
    }

    rep(i, 1, rem + 1) {
      rep(j, 0, chain.size()) {
        best = max(best, pref[j + i - 1] - (j == 0 ? 0 : pref[j - 1]));
      }
    }
    res += max(0LL, best);
    return res;
  } else {
    int rem = min(k, (int)chain.size());
    int best = LLONG_MIN;
    if (rem == chain.size()) {
      rem = chain.size() - 1;
      best = max(best, sum);
    }

    rep(i, 1, rem + 1) {
      rep(j, 0, chain.size()) {
        best = max(best, pref[j + i - 1] - (j == 0 ? 0 : pref[j - 1]));
      }
    }
    res = best;
    return res;
  }
}
void solve() {
  cin >> n >> k;
  rep(i, 0, n) cin >> p[i], p[i]--;
  rep(i, 0, n) cin >> c[i];
  memset(vis, 0, sizeof vis);
  rep(i, 0, n) {
    if (!vis[i]) {
      int cur = i;
      vi chain;
      while (true) {
        vis[cur] = true;
        chain.pb(cur);
        if (vis[p[cur]])
          break;
        cur = p[cur];
      }
      chains.pb(chain);
    }
  }
  int ans = LLONG_MIN;
  for (auto chain : chains)
    ans = max(ans, f(chain));
  cout << ans << "\\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef local
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  clock_t TIMER = clock();
#endif

  int t = 1;
  // cin >> t;
  rep(a, 0, t) {
    // cout<<"Case #"<<a+1<<": ";
    solve();
#ifdef local
    cout << "\\n";
#endif
  }

#ifdef local
  {
    TIMER = clock() - TIMER;
    double time_taken = ((double)TIMER) / CLOCKS_PER_SEC;
    cerr << fixed << setprecision(5) << "\\n\\nTime Taken : " << time_taken
         << " seconds\\n";
  }
#endif
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifdef local
#include "debug/debug.h"
#endif

#ifndef local
#define deb(x...) ;
#endif

#define int long long
#define ll long long
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vi>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repD(i, a, b) for (int i = a; i > b; i--)
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define MOD 1000000007
#define M_PI 3.14159265358979323846

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
///*X.find_by_order("index")
/// X.order_of_key("key")

///----------------------------------------------------------------------------------

int n, k, p[5001], c[5001];
bool vis[5001];
vvi chains;

int f(vi &chain) {
  vi vals;
  int sum = 0;
  for (int index : chain)
    vals.pb(c[index]), sum += c[index];

  int res = 0;
  bool started = false;
  if (sum >= 0)
    res += sum * (k / chain.size()), started = true;

  int rem = k % chain.size();

  rep(i, 0, chain.size()) vals.pb(vals[i]);
  vi pref(2 * n);
  pref[0] = vals[0];

  rep(i, 1, 2 * chain.size()) pref[i] = pref[i - 1] + vals[i];
  int best = LLONG_MIN;
  if (rem == 0) {
    rem = k - 1;
    if (started)
      res -= sum;
    best = max(best, sum);
  }

  rep(i, 1, rem + 1) {
    rep(j, 0, chain.size()) {
      best = max(best, pref[j + i - 1] - (j == 0 ? 0 : pref[j - 1]));
    }
  }

  if (started)
    res += max(0LL, best);
  else
    res = best;
  return res;
}
void solve() {
  cin >> n >> k;
  rep(i, 0, n) cin >> p[i], p[i]--;
  rep(i, 0, n) cin >> c[i];
  memset(vis, 0, sizeof vis);
  rep(i, 0, n) {
    if (!vis[i]) {
      int cur = i;
      vi chain;
      while (true) {
        vis[cur] = true;
        chain.pb(cur);
        if (vis[p[cur]])
          break;
        cur = p[cur];
      }
      chains.pb(chain);
    }
  }
  int ans = LLONG_MIN;
  for (auto chain : chains)
    ans = max(ans, f(chain));
  cout << ans << "\\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef local
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  clock_t TIMER = clock();
#endif

  int t = 1;
  // cin >> t;
  rep(a, 0, t) {
    // cout<<"Case #"<<a+1<<": ";
    solve();
#ifdef local
    cout << "\\n";
#endif
  }

#ifdef local
  {
    TIMER = clock() - TIMER;
    double time_taken = ((double)TIMER) / CLOCKS_PER_SEC;
    cerr << fixed << setprecision(5) << "\\n\\nTime Taken : " << time_taken
         << " seconds\\n";
  }
#endif
}