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

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define all(A) A.begin(), A.end()
#define sz(A) (int)A.size()
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const int inf = 1e9;
// const ll inf = 1e18;
const int N = 2e5;
ll a[3000][3000], dp[3000][3000][4];

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  ll x, y, val;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> val;
    x--;
    y--;
    a[x][y] = val;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int p = 0; p < 4; p++)
        dp[i][j][p] = 0;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      for (int p = 1; p < 4; p++) {
        if (i == n - 1 && j == m - 1) {
          dp[i][j][p] = a[i][j];
        } else if (i == n - 1) {
          dp[i][j][p] = max(a[i][j] + dp[i][j + 1][p - 1], dp[i][j + 1][p]);
        } else if (j == m - 1) {
          dp[i][j][p] = a[i][j] + dp[i + 1][j][3];
        } else {
          dp[i][j][p] = max({a[i][j] + dp[i + 1][j][3],
                             a[i][j] + dp[i][j + 1][p - 1], dp[i][j + 1][p]});
        }
        for (int z = p - 1; z >= 0; z--) {
          dp[i][j][p] = max(dp[i][j][p], dp[i][j][z]);
        }
      }
    }
  }
  cout << dp[0][0][3] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define all(A) A.begin(), A.end()
#define sz(A) (int)A.size()
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const int inf = 1e9;
// const ll inf = 1e18;
const int N = 2e5;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  ll a[n][m], x, y, val;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> val;
    x--;
    y--;
    a[x][y] = val;
  }
  ll dp[n][m][4];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int p = 0; p < 4; p++)
        dp[i][j][p] = 0;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      for (int p = 1; p < 4; p++) {
        if (i == n - 1 && j == m - 1) {
          dp[i][j][p] = a[i][j];
        } else if (i == n - 1) {
          dp[i][j][p] = max(a[i][j] + dp[i][j + 1][p - 1], dp[i][j + 1][p]);
        } else if (j == m - 1) {
          dp[i][j][p] = a[i][j] + dp[i + 1][j][3];
        } else {
          dp[i][j][p] = max({a[i][j] + dp[i + 1][j][3],
                             a[i][j] + dp[i][j + 1][p - 1], dp[i][j + 1][p]});
        }
        for (int z = p - 1; z >= 0; z--) {
          dp[i][j][p] = max(dp[i][j][p], dp[i][j][z]);
        }
      }
    }
  }
  cout << dp[0][0][3] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}