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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

long long v[3100][3100], dp[3100][3100][5];
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < K; ++i) {
    long long r, c, t;
    cin >> r >> c >> t;
    v[r][c] = t;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= R; ++i) {
    for (int j = 0; j <= C; ++j) {
      for (int k = 0; k <= 3; ++k) {
        // 上へ行く
        if (i - 1 >= 0) {
          chmax(dp[i][j][0], dp[i - 1][j][k]);
          chmax(dp[i][j][1], dp[i - 1][j][k] + v[i][j]);
        }

        // 右へ行く
        if (j - 1 >= 0) {
          chmax(dp[i][j][k], dp[i][j - 1][k]);
          if (k - 1 >= 0)
            chmax(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
        }
      }
    }
  }
  long long res = 0;
  for (int k = 0; k <= 3; ++k)
    chmax(res, dp[R][C][k]);
  cout << res << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
#define rep2(i, n) for (ll i = 2; i <= (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep0(i, n) for (ll i = 0; i <= (ll)(n); i++)

ll pow(ll a, ll n) { // compute a^n with o(logn)
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n >>= 1;
  }
  return res;
}

ll pow(ll a, ll n, ll mod) { // compute a^n (mod mod) with o(logn)
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

const int MAX = 199;
const int MOD = 1000000007;
ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 順列計算
ll PER(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[n - k] % MOD) % MOD;
}

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

int main() {
  const ll INF = 1e18;
  int MX = 3010;
  ll dp[3010][3010][4];
  ll v[3010][3010];
  int R, C, K;
  cin >> R >> C >> K;
  rep0(i, K - 1) {
    int r, c, val;
    cin >> r >> c >> val;
    --r, --c;
    v[r][c] = val;
  }
  rep0(i, MX - 1) rep0(j, MX - 1) rep0(k, 3) dp[i][j][k] = -INF;
  dp[0][0][0] = 0;
  ll ans = -INF;
  rep0(i, R - 1) {
    rep0(j, C - 1) {
      for (int k = 2; k >= 0; --k) {
        if (dp[i][j][k] >= 0)
          chmax(dp[i][j][k + 1], dp[i][j][k] + v[i][j]);
      }
      rep0(k, 3) {
        if (dp[i][j][k] >= 0) {
          if (i + 1 < R)
            chmax(dp[i + 1][j][0], dp[i][j][k]);
          if (j + 1 < C)
            chmax(dp[i][j + 1][k], dp[i][j][k]);
        }
      }
    }
  }
  rep0(k, 3) chmax(ans, dp[R - 1][C - 1][k]);
  cout << ans << endl;
  return 0;
}