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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = LLONG_MAX, mod = 1000000007, BS, k;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll arr[n], val[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  for (ll i = 0; i < n; i++)
    cin >> val[i];
  ll vis[n], dp[n][n + 1], curr, coun;
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j <= n; j++)
      dp[i][j] = 0;
  ll sum[n];
  for (ll i = 0; i < n; i++) {
    // for(ll j=0;j<n;j++)vis[j]=0;
    curr = i;
    coun = 0;
    while (1) {
      // if(vis[curr])break;
      // vis[curr]=1;
      dp[i][coun] += val[arr[curr] - 1];
      if (coun - 1 >= 0)
        dp[i][coun] += dp[i][coun - 1];
      curr = arr[curr] - 1;
      coun++;
      if (curr == i)
        break;
    }
    sum[i] = coun;
    // cout<<sum[i]<<" ";
  }
  ll ma = -inf, num, tem;
  // cout<<dp[1][0]<<" ";
  for (ll i = 0; i < n; i++) {
    num = -inf;
    curr = 0;
    tem = k;
    if (sum[i] < k && dp[i][sum[i] - 1] > 0) {
      curr = ((k - 1) / sum[i]) * (dp[i][sum[i] - 1]);
      // if(i==0)cout<<curr;
      k = (k - 1) % sum[i] + 1;
    }
    for (ll j = 0; j < min(sum[i], k); j++)
      num = max(num, dp[i][j]);
    // if(i==0)cout<<curr<<" ";
    if (num != -inf)
      curr += num;
    // if(i==2)cout<<curr<<" ";
    k = tem;
    ma = max(ma, curr);

    // curr=0;num=-inf;
    // for(ll j=0;j<min(k,sum[i]);j++)
    // num=max(num,dp[i][j]);
    // curr+=num;
    // ma=max(ma,curr);
  }
  cout << ma;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf = LLONG_MAX, mod = 1000000007, BS, k;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll arr[n], val[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  for (ll i = 0; i < n; i++)
    cin >> val[i];
  ll vis[n], dp[n][n + 1], curr, coun;
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j <= n; j++)
      dp[i][j] = 0;
  ll sum[n];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < n; j++)
      vis[j] = 0;
    curr = i;
    coun = 0;
    while (1) {
      if (vis[curr])
        break;
      vis[curr] = 1;
      dp[i][coun] += val[arr[curr] - 1];
      if (coun - 1 >= 0)
        dp[i][coun] += dp[i][coun - 1];
      curr = arr[curr] - 1;
      coun++;
    }
    sum[i] = coun;
    // cout<<sum[i]<<" ";
  }
  ll ma = -inf, num, tem;
  // cout<<dp[1][0]<<" ";
  for (ll i = 0; i < n; i++) {
    num = -inf;
    curr = 0;
    tem = k;
    if (k <= sum[i]) {
      for (ll j = 0; j < k; j++)
        num = max(num, dp[i][j]);
      curr += num;
    } else {
      // cout<<"Y";
      if (dp[i][sum[i] - 1] > 0) {
        curr += (k / sum[i]) * (dp[i][sum[i] - 1]);
        k %= sum[i];
        // if(i==0)break;
        for (ll j = 0; j < k; j++)
          num = max(num, dp[i][j]);
        curr += num;
      } else {
        for (ll j = 0; j < k; j++)
          num = max(num, dp[i][j]);
        curr += num;
      }
    }
    k = tem;
    ma = max(ma, curr);

    // curr=0;num=-inf;
    // for(ll j=0;j<min(k,sum[i]);j++)
    // num=max(num,dp[i][j]);
    // curr+=num;
    // ma=max(ma,curr);
  }
  cout << ma;
}