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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long dp[3030][3030][4] = {0};
long long a[3030][3030] = {0};
int main() {
  int r, c, K;
  cin >> r >> c >> K;
  for (int i = 0; i < K; i++) {
    int R, C;
    long long v;
    cin >> R >> C >> v;
    a[R][C] = v;
  }
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      for (int k = 1; k < 4; k++) {
        dp[i][j][k] = -100000000000007;
      }
    }
  }
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      for (int k = 3; k > 0; k--) {
        dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1] + a[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (i + 1 <= r) {
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        }
        if (j + 1 <= c) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    if (ans < dp[r][c][i]) {
      ans = dp[r][c][i];
    }
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int num[5050];
vector<vector<int>> vec(5050);
int p[5050];
long long c[5050];
int main() {
#define int long long
  long long n, l;
  cin >> n >> l;
  for (int i = 1; i <= n; i++) {
    num[i] = 1000000007;
  }
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  long long ans = -100000000000007;
  int co = 0;
  for (int k = 1; k <= n; k++) {
    long long m = -10000000000007;
    long long score[5050];
    int now = k;
    long long count = 0;
    int j = 0;
    num[k] = co;
    if (l <= 10000) {
      while (j < l) {
        now = p[now];
        count += c[now];
        if (count > m) {
          m = count;
        }
        j++;
      }
      if (m > ans) {
        ans = m;
      }
      continue;
    }
    for (int i = 0; i < 5050; i++) {
      score[i] = 0;
    }
    while (1) {
      now = p[now];
      count += c[now];
      j++;
      score[j] = count;
      if (num[now] == co) {
        break;
      }
      num[now] = co;
    }
    if (count < 0) {
      for (int i = 1; i <= j; i++) {
        if (m < score[i]) {
          m = score[i];
        }
      }
      if (ans < m) {
        ans = m;
      }
    }
    if (l % j == 0) {
      for (int i = 1; i <= j; i++) {
        if (m < score[i]) {
          m = score[i];
        }
      }
    } else {
      for (int i = 1; i <= j; i++) {
        if (i <= l % j && m < score[i]) {
          m = score[i];
        }
      }
    }
    if (ans < count * (l / j) + m) {
      ans = count * (l / j) + m;
    }
    co++;
  }
  cout << ans << endl;
  return 0;
}