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

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

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;
}

using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll dp[3005][3005][4];
bool seen[3005][3005][4];
ll R, C;
vector<vector<ll>> v;
ll f(ll x, ll y, ll z) {
  if (seen[x][y][z])
    return dp[x][y][z];
  seen[x][y][z] = true;
  if (x == R - 1 && y == C - 1) {
    if (v[x][y] == 0)
      dp[x][y][z] = 0;
    else if (z == 0)
      dp[x][y][z] = 0;
    else
      dp[x][y][z] = v[x][y];
    return dp[x][y][z];
  }
  if (z == 0) {
    if (x < R - 1)
      chmax(dp[x][y][z], f(x + 1, y, 3));
    if (y < C - 1)
      chmax(dp[x][y][z], f(x, y + 1, z));
  } else if (v[x][y] == 0LL) {
    if (x < R - 1)
      chmax(dp[x][y][z], f(x + 1, y, 3));
    if (y < C - 1)
      chmax(dp[x][y][z], f(x, y + 1, z));
  } else {
    if (x < R - 1) {
      chmax(dp[x][y][z], f(x + 1, y, 3) + v[x][y]);
    }
    if (y < C - 1) {
      chmax(dp[x][y][z], f(x, y + 1, z));
      chmax(dp[x][y][z], f(x, y + 1, z - 1) + v[x][y]);
    }
  }
  return dp[x][y][z];
}

int main() {
  ll K;
  cin >> R >> C >> K;
  v.assign(R, vector<ll>(C, 0LL));
  rep(i, K) {
    ll x, y, u;
    cin >> x >> y >> u;
    x--;
    y--;
    v[x][y] = u;
  }
  cout << f(0, 0, 3) << endl;
}

Error: -11

Fixed submission:

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

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;
}

using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll dp[3005][3005][3];
bool seen[3005][3005][3];
ll R, C;
vector<vector<ll>> v;
ll f(ll x, ll y, ll z) {
  if (seen[x][y][z])
    return dp[x][y][z];
  seen[x][y][z] = true;
  if (x == R - 1 && y == C - 1) {
    if (v[x][y] == 0)
      dp[x][y][z] = 0;
    else if (z == 0)
      dp[x][y][z] = 0;
    else
      dp[x][y][z] = v[x][y];
    return dp[x][y][z];
  }
  if (z == 0) {
    if (x < R - 1)
      chmax(dp[x][y][z], f(x + 1, y, 3));
    if (y < C - 1)
      chmax(dp[x][y][z], f(x, y + 1, z));
  } else if (v[x][y] == 0) {
    if (x < R - 1)
      chmax(dp[x][y][z], f(x, y + 1, 3));
    if (y < C - 1)
      chmax(dp[x][y][z], f(x + 1, y, z));
  } else {
    if (x < R - 1) {
      chmax(dp[x][y][z], f(x, y + 1, 3) + v[x][y]);
    }
    if (y < C - 1) {
      chmax(dp[x][y][z], f(x + 1, y, z));
      chmax(dp[x][y][z], f(x, y + 1, z - 1) + v[x][y]);
    }
  }
}

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  v.assign(R, vector<ll>(C));
  rep(i, K) {
    ll x, y, u;
    cin >> x >> y >> u;
    x--;
    y--;
    v[x][y] = u;
  }
  cout << f(0, 0, 3) << endl;
}