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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
// #include<Eigen/Dense>
// using namespace Eigen;
using namespace std;
typedef long long ll;

ll a[5005], b[5005];
int main() {
  ll n, k;
  while (cin >> n >> k) {
    for (int i = 0; i < n; i++) {
      cin >> a[i + 1];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i + 1];
    }
    vector<vector<ll>> r(n + 1);
    unordered_map<ll, ll> mp;
    ll ans = (-2147483647L - 1);
    for (int i = 1; i <= n; i++) {

      ll s = b[i];
      vector<ll> t;
      t.push_back(s);
      for (int j = a[i]; j != i; j = a[j])
        s += b[j], t.push_back(s);
      // vector<ll> t;
      // ll temp = 0;
      // int j = i;
      // int ok = 1;
      // while (ok || (a[j] != a[i])) {
      //	ok = 0;
      //	temp += b[a[j]];
      //	t.push_back(temp);
      //	r[i].push_back(j);
      //	j = a[j];
      // }
      // mp[i] = temp;
      int len = t.size();
      for (int j = 0; j < t.size(); j++) {
        ll res = (k - j - 1);
        if (res < 0)
          break;
        ans = max(ans, t[j] + (res / len) * max(0LL, s));
      }
    }
    /*ll ans = (-2147483647L - 1);
    for (int i = 1; i <= n; i++) {
            ll temp1 = k / r[i].size();
            ll temp2 = k % r[i].size();
            ll temp3 = 0;
            ll temp4 = (-2147483647L - 1);
            ll temp5 = (-2147483647L - 1);
            for (int j = 0; j < temp2; j++) {
                    temp3 += b[a[r[i][j]]];
                    temp4 = max(temp4, temp3);
            }
            temp3 = 0;
            for (int j = 0; j < r[i].size(); j++) {
                    temp3 += b[a[r[i][j]]];
                    temp5 = max(temp5, temp3);
            }
            if (temp1 > 0)
                    ans = max(max(ans, max(max(temp1*mp[i] + temp4,
    temp1*mp[i]), *max_element(b + 1, b + 1 + n))), temp5); else ans =
    max(ans,max(temp4, *max_element(b + 1, b + 1 + n)));
    }*/
    cout << ans << endl;
  }
  return 0;
}

// ll n, k, vis[5005];
// ll p[5005], c[5005], sum[5005], ans = -1e18;
// int main() {
//	cin >> n >> k;
//	for (int i = 1; i <= n; i++) cin >> p[i];
//	for (int i = 1; i <= n; i++) cin >> c[i];
//	for (int i = 1; i <= n; i++) {
//		ll s = c[i];
//		vector<ll> t; t.push_back(s);
//		for (int j = p[i]; j != i; j = p[j]) s += c[j], t.push_back(s);
//		int len = t.size();
//		for (int j = 0; j < len; j++) {
//			ll rest = k - j - 1;
//			if (rest < 0) break;
//			ans = max(ans, t[j] + (rest / len)*max(s, 0LL));
//		}
//	}
//	cout << ans;
// }

Error: -11

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
// #include<Eigen/Dense>
// using namespace Eigen;
using namespace std;

typedef long long ll;

ll a[5005], b[5005];
int main() {
  int n, k;
  while (cin >> n >> k) {
    for (int i = 0; i < n; i++) {
      cin >> a[i + 1];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i + 1];
    }
    vector<vector<ll>> r(n + 1);
    unordered_map<ll, ll> mp;
    for (int i = 1; i <= n; i++) {
      ll temp = 0;
      int j = i;
      int ok = 1;
      while (ok || (a[j] != a[i])) {
        ok = 0;
        temp += b[a[j]];
        r[i].push_back(j);
        j = a[j];
      }
      mp[i] = temp;
    }
    ll ans = (-2147483647L - 1);
    for (int i = 1; i <= n; i++) {
      ll temp1 = k / r[i].size();
      ll temp2 = k % r[i].size();
      ll temp3 = 0;
      ll temp4 = (-2147483647L - 1);
      ll temp5 = (-2147483647L - 1);
      for (int j = 0; j < temp2; j++) {
        temp3 += b[a[r[i][j]]];
        temp4 = max(temp4, temp3);
      }
      for (int j = 0; j < n; j++) {
        temp3 += b[a[r[i][j]]];
        temp5 = max(temp5, temp3);
      }
      if (temp1 > 0)
        ans = max(max(ans, max(max(temp1 * mp[i] + temp4, temp1 * mp[i]),
                               *max_element(b + 1, b + 1 + n))),
                  temp5);
      else
        ans = max(ans, max(temp4, *max_element(b + 1, b + 1 + n)));
    }
    cout << ans << endl;
  }
  return 0;
}