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

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
using T = ll;

const int MOD = 1e9 + 7;
const ll ZER = 0;

ll dp[5000][5000][4];
int main() {
  ll r, c, K;
  cin >> r >> c >> K;
  vector<vector<ll>> fld(r, vector<ll>(c, 0));
  rep(i, K) {
    ll a, b, v;
    cin >> a >> b >> v;
    a--;
    b--;
    fld[a][b] = v;
  }

  rep(i, r) {
    rep(j, c) {
      REP(k, 1, 3) {
        chmax(dp[i + 1][j + 1][k], dp[i + 1][j][k]);
        if (k > 0)
          chmax(dp[i + 1][j + 1][k], dp[i + 1][j][k - 1] + fld[i][j]);
        else
          dp[i + 1][j + 1][k] =
              max({dp[i + 1][j][k], dp[i][j + 1][3] + fld[i][j], fld[i][j]});
        chmax(dp[i + 1][j + 1][k], dp[i][j + 1][3] + fld[i][j]);
      }
    }
  }
  cout << dp[r][c][3] << endl;
  /*
  rep(i, r + 1){rep(j, c + 1)cout << dp[i][j][3] << " ";cout << endl;}
  cout << endl;
  rep(i, r){rep(j, c)cout << fld[i][j] << " " ;cout << endl;}
  */
}


Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define LINF 1e20
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
const ll ZER = 0;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> p(n), c(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) cin >> c[i];

  ll res = -LINF;
  ll tmp = k;
  rep(i, n) {
    k = tmp;
    int j = i;
    ll sum = 0, cnt = 0;
    while (1) {
      cnt++;
      j = p[j];
      sum += c[j];
      chmax(res, sum);
      if (j == i)
        break;
      if (cnt > k)
        break;
    }
    // cout << cnt << " " << sum << " ";
    if (sum <= 0) {
      continue;
    }
    if (sum > 0 && cnt > 0)
      sum = sum * (k / cnt);
    k %= cnt;
    cnt = 0;
    j = i;
    while (1) {
      cnt++;
      j = p[j];
      sum += c[j];
      chmax(res, sum);
      if (cnt >= k)
        break;
    }
    // cout << sum << endl;
  }
  cout << res << endl;
}