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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n, m) for (int i = (n); i < (int)(m); i++)
#define reps(i, n, m) for (int i = (n); i <= (int)(m); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
// #define mp make_pair
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a, x) lb(all(a), x) - a.begin()
#define UB(a, x) ub(all(a), x) - a.begin()
#define printfdouble(x) printf("%.12f\\n", (x))
#define chartoint(c) (int)((c) - '0')
#define chartoll(c) (long long)((c) - '0')
#define MOD 1000000007
#define itn int
#define enld endl
#define ednl endl
#define icn cin
#define Endl endl
#define stirng string
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
using Graph = vector<vector<int>>;
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;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll value[3005][3005];
ll dp[3005][3005][4];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll R, C, K;
  cin >> R >> C >> K;
  rep(i, 0, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    value[r][c] = v;
  }
  dp[1][1][1] = value[1][1];
  reps(i, 1, R) {
    reps(j, 1, C) {
      reps(k, 0, 3) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3)
          chmax(dp[i][j + 1][k + 1], dp[i][j][k] + value[i][j + 1]);
      }
      reps(k, 0, 3) {
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        chmax(dp[i + 1][j][1], dp[i][j][k] + value[i + 1][j]);
      }
    }
  }

  ll ans = dp[R][C][0];
  chmax(ans, dp[R][C][1]);
  chmax(ans, dp[R][C][2]);
  chmax(ans, dp[R][C][3]);

  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n, m) for (int i = (n); i < (int)(m); i++)
#define reps(i, n, m) for (int i = (n); i <= (int)(m); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
// #define mp make_pair
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a, x) lb(all(a), x) - a.begin()
#define UB(a, x) ub(all(a), x) - a.begin()
#define printfdouble(x) printf("%.12f\\n", (x))
#define chartoint(c) (int)((c) - '0')
#define chartoll(c) (long long)((c) - '0')
#define MOD 1000000007
#define itn int
#define enld endl
#define ednl endl
#define icn cin
#define Endl endl
#define stirng string
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
using Graph = vector<vector<int>>;
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;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll R, C, K;
  cin >> R >> C >> K;
  ll value[3005][3005];
  Fill(value, 0);
  rep(i, 0, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    value[r][c] = v;
  }
  ll dp[3005][3005][4];
  Fill(dp, 0);
  dp[1][1][1] = value[1][1];
  reps(i, 1, R) {
    reps(j, 1, C) {
      reps(k, 0, 3) {
        chmax(dp[i][j + 1][k], dp[i][j][k]);
        if (k < 3)
          chmax(dp[i][j + 1][k + 1], dp[i][j][k] + value[i][j + 1]);
      }
      reps(k, 0, 3) {
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        chmax(dp[i + 1][j][0], dp[i][j][k] + value[i + 1][j]);
      }
    }
  }

  ll ans = dp[R][C][0];
  chmax(ans, dp[R][C][1]);
  chmax(ans, dp[R][C][2]);
  chmax(ans, dp[R][C][3]);

  cout << ans << endl;
  return 0;
}