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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007
#define MAX 3000

signed main() {
  static int rcv[MAX + 1][MAX + 1] = {{0}};
  static int ans[4][MAX + 1][MAX + 1];
  int R, C, K;
  cin >> R >> C >> K;

  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    rcv[r][c] = v;
  }

  queue<int> qx, qy;

  qx.push(1);
  qy.push(1);

  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        ans[k][i][j] = -1;
      }
    }
  }

  ans[0][1][1] = 0;
  while (!qx.empty()) {
    int nx = qx.front();
    qx.pop();
    int ny = qy.front();
    qy.pop();
    for (int i = 0; i < 4; i++) {
      if (ans[i][ny][nx] == -1)
        continue;
      if (i <= 2) {
        if (nx != C &&
            (ans[i + 1][ny][nx + 1] < ans[i][ny][nx] + rcv[ny][nx])) {
          ans[i + 1][ny][nx + 1] = ans[i][ny][nx] + rcv[ny][nx];
          qx.push(nx + 1);
          qy.push(ny);
        }
        if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx] + rcv[ny][nx])) {
          ans[0][ny + 1][nx] = ans[i][ny][nx] + rcv[ny][nx];
          qx.push(nx);
          qy.push(ny + 1);
        }
      }
      if (nx != C && (ans[i][ny][nx + 1] < ans[i][ny][nx])) {
        ans[i][ny][nx + 1] = ans[i][ny][nx];
        qx.push(nx + 1);
        qy.push(ny);
      }
      if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx])) {
        ans[0][ny + 1][nx] = ans[i][ny][nx];
        qx.push(nx);
        qy.push(ny + 1);
      }
    }
  }
  cout << max(ans[0][R][C] + rcv[R][C],
              max(ans[1][R][C] + rcv[R][C],
                  max(ans[2][R][C] + rcv[R][C], ans[3][R][C])))
       << endl;
}


Error: -11

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007
#define MAX 3000

signed main() {
  int rcv[MAX + 1][MAX + 1] = {{0}};
  int ans[4][MAX + 1][MAX + 1];
  int R, C, K;
  cin >> R >> C >> K;

  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    rcv[r][c] = v;
  }

  queue<int> qx, qy;

  qx.push(1);
  qy.push(1);

  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        ans[k][i][j] = -1;
      }
    }
  }

  ans[0][1][1] = 0;
  while (!qx.empty()) {
    int nx = qx.front();
    qx.pop();
    int ny = qy.front();
    qy.pop();
    for (int i = 0; i < 4; i++) {
      if (ans[i][ny][nx] == -1)
        continue;
      if (i <= 2) {
        if (nx != C &&
            (ans[i + 1][ny][nx + 1] < ans[i][ny][nx] + rcv[ny][nx])) {
          ans[i + 1][ny][nx + 1] = ans[i][ny][nx] + rcv[ny][nx];
          qx.push(nx + 1);
          qy.push(ny);
        }
        if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx] + rcv[ny][nx])) {
          ans[0][ny + 1][nx] = ans[i][ny][nx] + rcv[ny][nx];
          qx.push(nx);
          qy.push(ny + 1);
        }
      }
      if (nx != C && (ans[i][ny][nx + 1] < ans[i][ny][nx])) {
        ans[i][ny][nx + 1] = ans[i][ny][nx];
        qx.push(nx + 1);
        qy.push(ny);
      }
      if (ny != R && (ans[0][ny + 1][nx] < ans[i][ny][nx])) {
        ans[0][ny + 1][nx] = ans[i][ny][nx];
        qx.push(nx);
        qy.push(ny + 1);
      }
    }
  }
  cout << max(ans[0][R][C] + rcv[R][C],
              max(ans[1][R][C] + rcv[R][C],
                  max(ans[2][R][C] + rcv[R][C], ans[3][R][C])))
       << endl;
}