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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
bool isPalin(string s) {
  if (s == string(s.rbegin(), s.rend()))
    return true;
  return false;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    int p[n + 1];
    int c[n + 1];
    for (int i = 1; i <= n; i++)
      cin >> p[i];
    for (int i = 1; i <= n; i++)
      cin >> c[i];
    vector<bool> visited(n + 1, false);
    ll ans = -1e18;
    ans--;
    for (int i = 1; i <= n; i++) {
      if (visited[i] == false) {
        int j = p[i];
        vector<ll> v;
        ll val = c[i];
        while (j != i) {
          val += c[j];
          visited[j] = true;
          v.push_back(c[j]);
          j = p[j];
        }
        visited[i] = true;
        v.push_back(c[i]);
        int s = v.size();
        for (j = 0; j < s; j++)
          v.push_back(v[j]);
        for (int i = 1; i < 2 * s; i++)
          v[i] += v[i - 1];
        ll l = 1;
        ll r = k;
        vector<ll> dp(s + 1, -1e18 + 1);
        for (int j = 1; j <= s; j++) {
          for (int i = 0; i + j < 2 * s; i++)
            dp[j] = max(dp[j], v[i + j] - v[i]);
        }
        for (int j = 1; j <= s; j++) {
          if (j <= k) {
            ll v1 = (k - j) / s;
            ans = max(ans, dp[j]);
            ans = max(ans, dp[j] + v1 * val);
          }
        }
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
bool isPalin(string s) {
  if (s == string(s.rbegin(), s.rend()))
    return true;
  return false;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ll t = 1;
  cin >> t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    int p[n + 1];
    int c[n + 1];
    for (int i = 1; i <= n; i++)
      cin >> p[i];
    for (int i = 1; i <= n; i++)
      cin >> c[i];
    vector<bool> visited(n + 1, false);
    ll ans = -1e18;
    ans--;
    for (int i = 1; i <= n; i++) {
      if (visited[i] == false) {
        int j = p[i];
        vector<ll> v;
        ll val = c[i];
        while (j != i) {
          val += c[j];
          visited[j] = true;
          v.push_back(c[j]);
          j = p[j];
        }
        visited[i] = true;
        v.push_back(c[i]);
        int s = v.size();
        for (j = 0; j < s; j++)
          v.push_back(v[j]);
        for (int i = 1; i < 2 * s; i++)
          v[i] += v[i - 1];
        ll l = 1;
        ll r = k;
        vector<ll> dp(s + 1, -1e18 + 1);
        for (int j = 1; j <= s; j++) {
          for (int i = 0; i + j < 2 * s; i++)
            dp[j] = max(dp[j], v[i + j] - v[i]);
        }
        for (int j = 1; j <= s; j++) {
          if (j <= k) {
            ll v1 = (k - j) / s;
            ans = max(ans, dp[j]);
            ans = max(ans, dp[j] + v1 * val);
          }
        }
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}