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

#include <bits/stdc++.h>
using namespace std;

#define INF 2000000000

long long int dp[5050][5050], dp_max[5050][5050];
int dp_size[5050];
int p[5050];
long long int c[5050];

int main() {

  int N, K;

  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 = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      dp_max[i][j] = -10000000000000000;
    }
  }

  for (int i = 1; i <= N; i++) {

    int now = i;
    int count = 1;
    while (true) {
      dp[i][count] = dp[i][count - 1] + c[p[now]];
      dp_max[i][count] = max(dp_max[i][count - 1], dp[i][count]);
      now = p[now];
      if (now == i) {
        dp_size[i] = count;
        break;
      }
      count++;
    }
  }
  long long int ans = -10000000000000000;
  for (int i = 1; i <= N; i++) {
    if (K <= dp_size[i]) {
      ans = max(ans, dp_max[i][K]);
    } else {
      if (dp[i][dp_size[i]] < 0) {
        ans = max(ans, dp_max[i][dp_size[i]]);
      } else {
        long long int tmp_ans;
        if (K % dp_size[i] == 0) {
          tmp_ans = dp[i][dp_size[i]] * (K / dp_size[i]);
          tmp_ans = max(tmp_ans, dp_max[i][dp_size[i]]);
          tmp_ans = max(tmp_ans, dp[i][dp_size[i]] * (K / dp_size[i] - 1) +
                                     dp_max[i][dp_size[i]]);

        } else {
          tmp_ans =
              dp[i][dp_size[i]] * (K / dp_size[i]) + dp_max[i][K % dp_size[i]];
          tmp_ans = max(tmp_ans, dp_max[i][dp_size[i]]);
          tmp_ans = max(tmp_ans, dp[i][dp_size[i]] * (K / dp_size[i] - 1) +
                                     dp_max[i][dp_size[i]]);
        }
        ans = max(ans, tmp_ans);
      }
    }
  }

  cout << ans << endl;

  /*
      for(int i = 1;i <= N;i++){
          for(int j = 1;j <= dp_size[i];j++){
              cout << dp[i][j] << " ";
          }
          cout << endl;
      }

      for(int i = 1;i <= N;i++){
          for(int j = 1;j <= dp_size[i];j++){
              cout << dp_max[i][j] << " ";
          }
          cout << endl;
      }

  */
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF 2000000000

long long int dp[5050][5050], dp_max[5050][5050];
int dp_size[5050];
int p[5050];
long long int c[5050];

int main() {

  int N, K;

  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 = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      dp_max[i][j] = -10000000000000000;
    }
  }

  for (int i = 1; i <= N; i++) {

    int now = i;
    int count = 1;
    while (true) {
      dp[i][count] = dp[i][count - 1] + c[p[now]];
      dp_max[i][count] = max(dp_max[i][count - 1], dp[i][count]);
      now = p[now];
      if (now == i) {
        dp_size[i] = count;
        break;
      }
      count++;
    }
  }
  long long int ans = -10000000000000000;
  for (int i = 1; i <= N; i++) {
    if (K <= dp_size[i]) {
      ans = max(ans, dp_max[i][K]);
    } else {
      if (dp[i][dp_size[i]] < 0) {
        ans = max(ans, dp_max[i][K]);
      } else {
        long long int tmp_ans;
        if (K % dp_size[i] == 0) {
          tmp_ans = dp[i][dp_size[i]] * (K / dp_size[i]);
        } else {
          tmp_ans =
              dp[i][dp_size[i]] * (K / dp_size[i]) + dp_max[i][K % dp_size[i]];
        }
        ans = max(ans, tmp_ans);
      }
    }
  }

  cout << ans << endl;

  /*
      for(int i = 1;i <= N;i++){
          for(int j = 1;j <= dp_size[i];j++){
              cout << dp[i][j] << " ";
          }
          cout << endl;
      }

      for(int i = 1;i <= N;i++){
          for(int j = 1;j <= dp_size[i];j++){
              cout << dp_max[i][j] << " ";
          }
          cout << endl;
      }

  */
  return 0;
}