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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << (x) << '\\n'
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << ' ' << v[i];                                        \\
  cout << '\\n'
#define ALL(a) (a).begin(), (a).end()

template <typename T> inline void CHMAX(T &a, const T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void CHMIN(T &a, const T b) {
  if (a > b)
    a = b;
}

constexpr ll MOD = 1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-9;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

ll mp[3252][3252];
ll dp[3252][3252][4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  int R, C, K;
  cin >> R >> C >> K;
  --R;
  --C;
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    --r;
    --c;
    mp[r][c] = v;
  }
  REP(i, R + 1) {
    REP(j, C + 1) {
      REP(k, 4) {
        CHMAX(dp[i + 1][j + 1][k], dp[i + 1][j][k]);
        if (k < 3) {
          CHMAX(dp[i + 1][j + 1][k + 1], dp[i + 1][j][k] + mp[i][j]);
        }
        CHMAX(dp[i + 1][j + 1][0], dp[i][j + 1][k]);
        CHMAX(dp[i + 1][j + 1][1], dp[i][j + 1][k] + mp[i][j]);
      }
    }
  }
  ll ans = 0;
  REP(i, 4) { CHMAX(ans, dp[R + 1][C + 1][i]); }
  cout << ans << '\\n';
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << (x) << '\\n'
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << ' ' << v[i];                                        \\
  cout << '\\n'
#define ALL(a) (a).begin(), (a).end()

template <typename T> inline void CHMAX(T &a, const T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void CHMIN(T &a, const T b) {
  if (a > b)
    a = b;
}

constexpr ll MOD = 1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-9;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

ll mp[3252][3252];
ll dp[3252][3252][4][4];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  int R, C, K, r[3252], c[3252], v[3252];
  cin >> R >> C >> K;
  --R;
  --C;
  REP(i, K) {
    cin >> r[i] >> c[i] >> v[i];
    --r[i];
    --c[i];
    mp[r[i]][c[i]] = v[i];
  }
  REP(i, R + 1) {
    REP(j, C + 1) {
      REP(k, 4) {
        ll ma = 0;
        REP(l, 4) { CHMAX(ma, dp[i + 1][j][k][l]); }
        CHMAX(dp[i + 1][j + 1][k][0], ma);
        if (k < 3) {
          CHMAX(dp[i + 1][j + 1][k + 1][1], ma + mp[i][j]);
        }
      }
      REP(l, 4) {
        ll ma = 0;
        REP(k, 4) { CHMAX(ma, dp[i][j + 1][k][l]); }
        CHMAX(dp[i + 1][j + 1][0][l], ma);
        if (l < 3) {
          CHMAX(dp[i + 1][j + 1][1][l + 1], ma + mp[i][j]);
        }
      }
    }
  }
  ll ans = 0;
  REP(i, 4) {
    REP(j, 4) { CHMAX(ans, dp[R + 1][C + 1][i][j]); }
  }
  cout << ans << '\\n';
  return 0;
}