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

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mset(x, y) memset((x), (y), sizeof(x))
#define sz(x) ((ll)(x).size())
#define rep(i, a, b) for (i = a; i < b; i++)
#define per(i, a, b) for (i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define cerr cout
#define endl '\\n'
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "] :", debug_out(__VA_ARGS__)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ld eps = 1e-7;
const ll mod = 1000000007;
const ll N = 998244353;

ll binpow(ll b, ll p) {
  ll ans = 1;
  while (p > 0) {
    if (p & 1)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p >>= 1;
  }
  return ans;
}
ll modinv(ll x) { return binpow(x, mod - 2); }

const ll mxn = 5100;
ll ans = LLONG_MIN, n, k;
ll p[mxn], c[mxn];

void startat(ll ind) {
  vl dp;
  // mset(dp, 0);
  ll val = 0, pre = 0, moves = 0, x, i = ind;
  while (p[i] != ind && moves < k) {
    val = c[p[i]];
    i = p[i];
    pre += val;
    dp.eb(val);
    ans = max(ans, pre);
    moves++;
  }
  if (moves < k && p[i] == ind) {
    val = c[p[i]];
    i = p[i];
    pre += val;
    dp.eb(val);
    ans = max(ans, pre);
    moves++;
  }
  // assert(sz(dp) <= n);
  // pr(dp, moves);
  if (moves == k)
    assert(sz(dp) == k);
  if (pre <= 0 || moves == k)
    return;

  else if (sz(dp) < k) {
    x = k / sz(dp);
    if (k % sz(dp) == 0) {
      pre *= (x - 1LL);
      ans = max(ans, pre);
      for (i = 0; i < sz(dp); i++) {
        pre += dp[i];
        ans = max(ans, pre);
      }
      return;
    }
    pre *= x;
    ans = max(ans, pre);
    moves = k % sz(dp);
    for (i = 0; i < moves; i++) {
      pre += dp[i];
      ans = max(ans, pre);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll i;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> p[i];
  rep(i, 1, n + 1) cin >> c[i];

  rep(i, 1, n + 1) {
    // pr(i);
    startat(i);
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mset(x, y) memset((x), (y), sizeof(x))
#define sz(x) ((ll)(x).size())
#define rep(i, a, b) for (i = a; i < b; i++)
#define per(i, a, b) for (i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define cerr cout
#define endl '\\n'
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "] :", debug_out(__VA_ARGS__)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ld eps = 1e-7;
const ll mod = 1000000007;
const ll N = 998244353;

ll binpow(ll b, ll p) {
  ll ans = 1;
  while (p > 0) {
    if (p & 1)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p >>= 1;
  }
  return ans;
}
ll modinv(ll x) { return binpow(x, mod - 2); }

const ll mxn = 510;
ll ans = -1e17, n, k;
ll p[mxn], c[mxn];

void startat(ll ind) {
  vl dp;
  // mset(dp, 0);
  ll val = 0, pre = 0, moves = 0, x, i = ind;
  while (p[i] != ind && moves < k) {
    val = c[p[i]];
    i = p[i];
    pre += val;
    dp.eb(val);
    ans = max(ans, pre);
    moves++;
  }
  if (moves < k && p[i] == ind) {
    val = c[p[i]];
    i = p[i];
    pre += val;
    dp.eb(val);
    ans = max(ans, pre);
    moves++;
  }
  // pr(dp, moves);
  if (pre < 0 || moves == k)
    return;
  else {
    x = k / sz(dp);
    pre *= x;
    ans = max(ans, pre);
    moves = k - x * sz(dp);
    for (int i = 0; i < moves; i++) {
      pre += dp[i];
      ans = max(ans, pre);
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll i;
  cin >> n >> k;
  rep(i, 1, n + 1) cin >> p[i];
  rep(i, 1, n + 1) cin >> c[i];

  rep(i, 1, n + 1) {
    // pr(i);
    startat(i);
  }
  cout << ans;
}