Problem p02585 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 205;
const int MAXM = 50005;
const int MOD = 1e9 + 7;

#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync                                                              \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename T> inline T max(T a, T b, T c) { return max(max(a, b), c); }

int vis[MAXN];
int p[MAXN];
ll c[MAXN];
ll sumv[MAXN], preMax[MAXN], val[MAXN];
int n;
ll k;
inline ll go(int st) {
  int cnt = 0;
  vis[st] = 1;
  sumv[0] = 0;
  preMax[0] = -1e18;
  while (!vis[p[st]]) {
    st = p[st];
    ++cnt;
    sumv[cnt] = sumv[cnt - 1] + c[st];
    val[cnt] = c[st];
    // debug(sumv[cnt]);
    preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
    vis[st] = 1;
  }
  st = p[st];
  ++cnt;
  sumv[cnt] = sumv[cnt - 1] + c[st];
  // debug(sumv[cnt]);
  val[cnt] = st;
  preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
  int rd = cnt; // 循环节的长度
  // debug(rd);
  ll rdval = sumv[cnt]; // 循环节的值
  // debug(rdval);
  // debug(preMax[cnt]);
  preMax[0] = 0;
  ll ans = 0;
  // debug(cnt);
  rep(i, 1, rd) {
    sumv[rd + i] = sumv[rd + i - 1] + val[i];
    // debug(val[i]);
    // debug(sumv[rd+i]);
    preMax[rd + i] = max(preMax[rd + i - 1], sumv[rd + i]);
    // debug(preMax[rd+i]);
  }
  if (k > rd) {
    if (rdval > 0) {
      ll rm = k % rd + rd;
      // debug(ans);
      ans += rdval * ((k - rd) / rd);
      // debug(ans);

      ans += preMax[rm];
    } else
      ans = preMax[cnt];
  } else
    ans = preMax[k];
  return ans;
}
int main() {
  closeSync;
#ifdef DEBUG
  // freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
  // freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
  cin >> n >> k;
  rep(i, 1, n) cin >> p[i];
  rep(i, 1, n) cin >> c[i];
  ll mx = -1e18;
  rep(i, 1, n) {
    rep(i, 1, n) val[i] = sumv[i] = preMax[i] = vis[i] = 0;
    ll temp = go(i);
    // debug(temp);
    mx = max(mx, temp);
  }
  cout << mx << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 100005;
const int MAXM = 50005;
const int MOD = 1e9 + 7;

#define MP make_pair
#define INF 0x3f3f3f3f
#define closeSync                                                              \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define mst(X, Y) memset(X, Y, sizeof(X))
#define rep(i, a, n) for (int i = (a); i <= (n); i++)
#define per(i, a, n) for (int i = (a); i >= (n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <typename T> inline T max(T a, T b, T c) { return max(max(a, b), c); }

int vis[MAXN];
int p[MAXN];
ll c[MAXN];
ll sumv[MAXN], preMax[MAXN], val[MAXN];
int n;
ll k;
inline ll go(int st) {
  int cnt = 0;
  vis[st] = 1;
  sumv[0] = 0;
  preMax[0] = -1e18;
  while (!vis[p[st]]) {
    st = p[st];
    ++cnt;
    sumv[cnt] = sumv[cnt - 1] + c[st];
    val[cnt] = c[st];
    // debug(sumv[cnt]);
    preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
    vis[st] = 1;
  }
  st = p[st];
  ++cnt;
  sumv[cnt] = sumv[cnt - 1] + c[st];
  // debug(sumv[cnt]);
  val[cnt] = st;
  preMax[cnt] = max(preMax[cnt - 1], sumv[cnt]);
  int rd = cnt; // 循环节的长度
  // debug(rd);
  ll rdval = sumv[cnt]; // 循环节的值
  // debug(rdval);
  // debug(preMax[cnt]);
  preMax[0] = 0;
  ll ans = 0;
  // debug(cnt);
  rep(i, 1, rd) {
    sumv[rd + i] = sumv[rd + i - 1] + val[i];
    // debug(val[i]);
    // debug(sumv[rd+i]);
    preMax[rd + i] = max(preMax[rd + i - 1], sumv[rd + i]);
    // debug(preMax[rd+i]);
  }
  if (k > rd) {
    if (rdval > 0) {
      ll rm = k % rd + rd;
      // debug(ans);
      ans += rdval * ((k - rd) / rd);
      // debug(ans);

      ans += preMax[rm];
    } else
      ans = preMax[cnt];
  } else
    ans = preMax[k];
  return ans;
}
int main() {
  closeSync;
#ifdef DEBUG
  // freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
  // freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
  cin >> n >> k;
  rep(i, 1, n) cin >> p[i];
  rep(i, 1, n) cin >> c[i];
  ll mx = -1e18;
  rep(i, 1, n) {
    rep(i, 1, n) val[i] = sumv[i] = preMax[i] = vis[i] = 0;
    ll temp = go(i);
    // debug(temp);
    mx = max(mx, temp);
  }
  cout << mx << endl;
  return 0;
}