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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> prl;
typedef vector<ll> vcl;
typedef map<ll, ll> mapl;
typedef unordered_map<ll, ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;

int main() {
  // your code goes here
  ll R, C, K, r, c, v;
  cin >> R >> C >> K;
  vector<vcl> vs(3005, vcl(3005));
  vector<vcl> dp0(3005, vcl(3005));
  vector<vcl> dp1(3005, vcl(3005));
  vector<vcl> dp2(3005, vcl(3005));
  vector<vcl> dp3(3005, vcl(3005));
  ll ans = 0;
  rep(i, 1, K) {
    cin >> r >> c >> v;
    vs[r][c] = v;
  }
  rep(i, 1, R) {
    rep(j, 1, C) {
      rep(k, 0, 3) {
        if (k == 0) {
          chmax(dp0[i][j], dp0[i][j - 1]);
          chmax(dp0[i][j], dp0[i - 1][j]);
          chmax(dp0[i][j], dp1[i - 1][j]);
          chmax(dp0[i][j], dp2[i - 1][j]);
          chmax(dp0[i][j], dp3[i - 1][j]);
        } else if (vs[i][j] == 0) {
          chmax(dp0[i][j], dp0[i][j - 1]);
          chmax(dp1[i][j], dp1[i][j - 1]);
          chmax(dp2[i][j], dp2[i][j - 1]);
          chmax(dp3[i][j], dp3[i][j - 1]);
        } else if (k != 1) {
          chmax(dp2[i][j], dp2[i][j - 1]);
          chmax(dp3[i][j], dp3[i][j - 1]);
          chmax(dp2[i][j], dp1[i][j - 1] + vs[i][j]);
          chmax(dp3[i][j], dp2[i][j - 1] + vs[i][j]);
        } else {
          chmax(dp1[i][j], dp1[i][j - 1]);
          chmax(dp1[i][j], dp0[i][j - 1] + vs[i][j]);
          chmax(dp1[i][j], dp0[i - 1][j] + vs[i][j]);
          chmax(dp1[i][j], dp1[i - 1][j] + vs[i][j]);
          chmax(dp1[i][j], dp2[i - 1][j] + vs[i][j]);
          chmax(dp1[i][j], dp3[i - 1][j] + vs[i][j]);
        }
      }
    }
  }
  chmax(ans, dp0[R][C]);
  chmax(ans, dp1[R][C]);
  chmax(ans, dp2[R][C]);
  chmax(ans, dp3[R][C]);
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll, ll> prl;
typedef vector<ll> vcl;
typedef map<ll, ll> mapl;
typedef unordered_map<ll, ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repi(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll mod = 1e9 + 7;

int main() {
  // your code goes here
  ll R, C, K, r, c, v;
  cin >> R >> C >> K;
  ll vs[3005][3005] = {0};
  ll dp0[3005][3005] = {0};
  ll dp1[3005][3005] = {0};
  ll dp2[3005][3005] = {0};
  ll dp3[3005][3005] = {0};
  ll ans = 0;
  rep(i, 1, K) {
    cin >> r >> c >> v;
    vs[r][c] = v;
  }
  rep(i, 1, R) {
    rep(j, 1, C) {
      rep(k, 0, 3) {
        if (k == 0) {
          chmax(dp0[i][j], dp0[i][j - 1]);
          chmax(dp0[i][j], dp0[i - 1][j]);
          chmax(dp0[i][j], dp1[i - 1][j]);
          chmax(dp0[i][j], dp2[i - 1][j]);
          chmax(dp0[i][j], dp3[i - 1][j]);
        } else if (vs[i][j] == 0) {
          chmax(dp0[i][j], dp0[i][j - 1]);
          chmax(dp1[i][j], dp1[i][j - 1]);
          chmax(dp2[i][j], dp2[i][j - 1]);
          chmax(dp3[i][j], dp3[i][j - 1]);
        } else if (k != 1) {
          chmax(dp2[i][j], dp2[i][j - 1]);
          chmax(dp3[i][j], dp3[i][j - 1]);
          chmax(dp2[i][j], dp1[i][j - 1] + vs[i][j]);
          chmax(dp3[i][j], dp2[i][j - 1] + vs[i][j]);
        } else {
          chmax(dp1[i][j], dp1[i][j - 1]);
          chmax(dp1[i][j], dp0[i][j - 1] + vs[i][j]);
          chmax(dp1[i][j], dp0[i - 1][j] + vs[i][j]);
          chmax(dp1[i][j], dp1[i - 1][j] + vs[i][j]);
          chmax(dp1[i][j], dp2[i - 1][j] + vs[i][j]);
          chmax(dp1[i][j], dp3[i - 1][j] + vs[i][j]);
        }
      }
    }
  }
  chmax(ans, dp0[R][C]);
  chmax(ans, dp1[R][C]);
  chmax(ans, dp2[R][C]);
  chmax(ans, dp3[R][C]);
  cout << ans << endl;
  return 0;
}