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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
const int N = 100005;
ll dp[4][3002][3002] = {};
ll val[3002][3002] = {};
void solve() {
  ll n, k = 0, m = INT64_MIN, i = 0, j, x, y;
  ll ans = 0;
  cin >> n >> m >> k;
  while (k--) {
    cin >> x >> y;
    cin >> val[x][y];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      k = val[i][j];
      if (k) {
        dp[0][i][j] = max({dp[0][i - 1][j], dp[1][i - 1][j], dp[2][i - 1][j],
                           dp[3][i - 1][j], dp[0][i][j - 1]});
        dp[1][i][j] = max({dp[1][i][j - 1], dp[1][i - 1][j] + k,
                           dp[2][i - 1][j] + k, dp[3][i - 1][j] + k,
                           dp[0][i - 1][j] + k, dp[0][i][j - 1] + k});
        dp[2][i][j] = max(dp[2][i][j - 1], dp[1][i][j - 1] + k);
        dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + k);
      } else {
        dp[0][i][j] = max({dp[0][i][j - 1], dp[1][i - 1][j], dp[2][i - 1][j],
                           dp[3][i - 1][j], dp[0][i - 1][j]});
        dp[1][i][j] = dp[1][i][j - 1];
        dp[2][i][j] = dp[2][i][j - 1];
        dp[3][i][j] = dp[3][i][j - 1];
      }
    }
  }
  cout << max({dp[0][n][m], dp[1][n][m], dp[2][n][m], dp[3][n][m]});
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
const int N = 100005;
ll dp[3][3002][3002] = {};
ll val[3002][3002] = {};
void solve() {
  ll n, k = 0, m = INT64_MIN, i = 0, j, x, y;
  ll ans = 0;
  cin >> n >> m >> k;
  while (k--) {
    cin >> x >> y;
    cin >> val[x][y];
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      k = val[i][j];
      if (k) {
        dp[0][i][j] = max({dp[0][i - 1][j], dp[1][i - 1][j], dp[2][i - 1][j],
                           dp[3][i - 1][j], dp[0][i][j - 1]});
        dp[1][i][j] =
            max({dp[1][i][j - 1], dp[1][i - 1][j] + k, dp[2][i - 1][j] + k,
                 dp[3][i - 1][j] + k, dp[0][i][j - 1] + k});
        dp[2][i][j] = max({dp[2][i][j - 1], dp[1][i][j - 1] + k});
        dp[3][i][j] = max(dp[3][i][j - 1], dp[2][i][j - 1] + k);
      } else {
        dp[0][i][j] = max({dp[0][i][j - 1], dp[1][i - 1][j], dp[2][i - 1][j],
                           dp[3][i - 1][j], dp[0][i - 1][j]});
        dp[1][i][j] = dp[1][i][j - 1];
        dp[2][i][j] = dp[2][i][j - 1];
        dp[3][i][j] = dp[3][i][j - 1];
      }
    }
  }
  cout << max({dp[0][n][m], dp[1][n][m], dp[2][n][m], dp[3][n][m]});
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}