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

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clr clear()
#define ins insert
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define rep(i, n) for (__typeof(n) i = 0; i < (n); i++)
#define mem(x, val) memset((x), (val), sizeof(x));
#define cusmem(x, val)                                                         \\
  for (auto &i : x)                                                            \\
    i = val;
#define all(x) x.begin(), x.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef vector<ll> vl;
typedef vector<pll> vpll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
string impossible;

mt19937
    mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());

// const int N=6;
const int N = 5e2 + 5;
const int M = 1e4 + 5;
const int L = 41;
int MOD = 1e9 + 7;
// ll n,m,k,i,j,d;
bool f0, f1, f2;
const int MAXN = 2e3;
ll dp[3000][3000][4];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio cout << fixed << setprecision(11);
  int tests = 1;
  // cin>>tests;
  for (int te = 1; te <= tests; te++) {
    ll r, c, items;
    cin >> r >> c >> items;
    int v[r][c];
    mem(v, 0);
    for (int i = 0; i < items; i++) {
      int x, y, val;
      cin >> x >> y >> val;
      v[x - 1][y - 1] = val;
    }

    mem(dp, 0LL);
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        for (int k = 0; k < 4; k++) {
          if (i - 1 >= 0)
            dp[i][j][k] = max(dp[i - 1][j][3], dp[i][j][k]);
          if (j - 1 >= 0)
            dp[i][j][k] = max(dp[i][j - 1][k], dp[i][j][k]);
          if (k > 0) {
            if (i - 1 >= 0)
              dp[i][j][k] = max(v[i][j] + dp[i - 1][j][3], dp[i][j][k]);
            if (j - 1 >= 0)
              dp[i][j][k] = max(v[i][j] + dp[i][j - 1][k - 1], dp[i][j][k]);
            dp[i][j][k] = max(dp[i][j][k], v[i][j] * 1LL);
          }
          // cout<<dp[i][j][k]<<" ";
        }
        // cout<<"|\\t|";
      }
      // cout<<"\\n";
    }
    cout << dp[r - 1][c - 1][3];
  }
  return 0;
}


Error: -11

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clr clear()
#define ins insert
#define tol(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define tou(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define rep(i, n) for (__typeof(n) i = 0; i < (n); i++)
#define mem(x, val) memset((x), (val), sizeof(x));
#define cusmem(x, val)                                                         \\
  for (auto &i : x)                                                            \\
    i = val;
#define all(x) x.begin(), x.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef vector<ll> vl;
typedef vector<pll> vpll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
string impossible;

mt19937
    mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());

// const int N=6;
const int N = 5e2 + 5;
const int M = 1e4 + 5;
const int L = 41;
int MOD = 1e9 + 7;
// ll n,m,k,i,j,d;
bool f0, f1, f2;
const int MAXN = 2e3;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio cout << fixed << setprecision(11);
  int tests = 1;
  // cin>>tests;
  for (int te = 1; te <= tests; te++) {
    ll r, c, items;
    cin >> r >> c >> items;
    int v[r][c];
    mem(v, 0);
    for (int i = 0; i < items; i++) {
      int x, y, val;
      cin >> x >> y >> val;
      v[x - 1][y - 1] = val;
    }
    ll dp[r][c][4];
    mem(dp, 0LL);
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        for (int k = 0; k < 4; k++) {
          if (i - 1 >= 0)
            dp[i][j][k] = max(dp[i - 1][j][3], dp[i][j][k]);
          if (j - 1 >= 0)
            dp[i][j][k] = max(dp[i][j - 1][k], dp[i][j][k]);
          if (k > 0) {
            if (i - 1 >= 0)
              dp[i][j][k] = max(v[i][j] + dp[i - 1][j][3], dp[i][j][k]);
            if (j - 1 >= 0)
              dp[i][j][k] = max(v[i][j] + dp[i][j - 1][k - 1], dp[i][j][k]);
            dp[i][j][k] = max(dp[i][j][k], v[i][j] * 1LL);
          }
          // cout<<dp[i][j][k]<<" ";
        }
        // cout<<"|\\t|";
      }
      // cout<<"\\n";
    }
    cout << dp[r - 1][c - 1][3];
  }
  return 0;
}