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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> a(r + 1, vector<ll>(c + 1, -1));
  REP(i, k) {
    int x, y, v;
    cin >> x >> y >> v;
    x--, y--;
    a[x][y] = v;
  }
  vector<vector<vector<ll>>> dp(r + 1,
                                vector<vector<ll>>(c + 1, vector<ll>(5, -1)));
  dp[0][0][0] = 0;
  if (a[0][0] > 0)
    dp[0][0][1] = a[0][0];
  REP(i, r) {
    REP(j, c) {
      REP(l, 4) {
        if (dp[i][j][l] == -1)
          continue;
        chmax(dp[i + 1][j][0], dp[i][j][l]);
        chmax(dp[i][j + 1][l], dp[i][j][l]);
        if (a[i + 1][j] > 0) {
          chmax(dp[i + 1][j][1], dp[i][j][l] + a[i + 1][j]);
        }
        if (a[i][j + 1] > 0) {
          chmax(dp[i][j + 1][l + 1], dp[i][j][l] + a[i][j + 1]);
        }
      }
    }
  }
  ll ans = 0;
  REP(i, 4) { chmax(ans, dp[r - 1][c - 1][i]); }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> a(r + 1, vector<ll>(c + 1));
  REP(i, k) {
    int x, y, v;
    cin >> x >> y >> v;
    x--, y--;
    a[x][y] = v;
  }
  ll dp[r + 1][c + 1][5];
  REP(i, r + 1) REP(j, c + 1) REP(l, 5) dp[i][j][l] = 0;
  if (a[0][0] > 0) {
    dp[0][0][1] = a[0][0];
  }
  REP(i, r) {
    REP(j, c) {
      REP(l, 4) {
        chmax(dp[i + 1][j][0], dp[i][j][l]);
        chmax(dp[i][j + 1][l], dp[i][j][l]);
        if (a[i + 1][j] > 0) {
          chmax(dp[i + 1][j][1], dp[i][j][l] + a[i + 1][j]);
        }
        if (a[i][j + 1] > 0) {
          chmax(dp[i][j + 1][l + 1], dp[i][j][l] + a[i][j + 1]);
        }
      }
    }
  }
  ll ans = 0;
  REP(i, 4) { chmax(ans, dp[r - 1][c - 1][i]); }
  cout << ans << endl;
}