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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll dp[3000][3000][4];

class EPickingGoods {
public:
  void solve(istream &cin, ostream &cout) {
    SPEED;
    int r, c, k;
    cin >> r >> c >> k;
    rep(i, r) rep(j, c) rep(K, 4) { dp[i][j][K] = 0; }

    ll val[r][c];
    rep(i, r) rep(j, c) { val[i][j] = 0; }

    rep(i, k) {
      int R, C, V;
      cin >> R >> C >> V;
      R--;
      C--;
      val[R][C] = V;
    }

    dp[0][0][1] = val[0][0];

    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        // move to right
        if (j + 1 < c) {
          rep(K, 4) {
            chmax(dp[i][j + 1][K], dp[i][j][K]);
            if (K < 3)
              chmax(dp[i][j + 1][K + 1], dp[i][j][K] + val[i][j + 1]);
          }
        }
        // down
        if (i + 1 < r) {
          rep(K, 4) {
            chmax(dp[i + 1][j][0], dp[i][j][K]);
            chmax(dp[i + 1][j][1], dp[i][j][K] + val[i + 1][j]);
          }
        }
      }
    }

    ll ret = 0;
    rep(K, 4) { chmax(ret, dp[r - 1][c - 1][K]); }
    cout << ret << endl;
  }
};

signed main() {
  EPickingGoods solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class EPickingGoods {
public:
  void solve(istream &cin, ostream &cout) {
    SPEED;
    int r, c, k;
    cin >> r >> c >> k;
    ll dp[r][c][4];
    rep(i, r) rep(j, c) rep(K, 4) { dp[i][j][K] = 0; }

    ll val[r][c];
    rep(i, r) rep(j, c) { val[i][j] = 0; }

    rep(i, k) {
      int R, C, V;
      cin >> R >> C >> V;
      R--;
      C--;
      val[R][C] = V;
    }

    dp[0][0][1] = val[0][0];

    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        // move to right
        if (j + 1 < c) {
          rep(K, 4) {
            chmax(dp[i][j + 1][K], dp[i][j][K]);
            if (K < 3)
              chmax(dp[i][j + 1][K + 1], dp[i][j][K] + val[i][j + 1]);
          }
        }
        // down
        if (i + 1 < r) {
          rep(K, 4) {
            chmax(dp[i + 1][j][0], dp[i][j][K]);
            chmax(dp[i + 1][j][1], dp[i][j][K] + val[i + 1][j]);
          }
        }
      }
    }

    ll ret = 0;
    rep(K, 4) { chmax(ret, dp[r - 1][c - 1][K]); }
    cout << ret << endl;
  }
};

signed main() {
  EPickingGoods solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}