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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 3456;
int val[MAXN][MAXN];
LL dp[MAXN][MAXN][4];

int main() {
  int R, C, K;
  scanf("%d %d %d", &R, &C, &K);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    val[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k < 3)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
        // not take
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        // not take
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k) // take
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k - 1] + val[i][j]);
        // printf("i = %d j = %d k = %d dp = %lld\\n", i, j, k, dp[i][j][k]);
      }
    }
  }
  LL ans = 0;
  for (int k = 0; k <= 3; k++) {
    if (k != 3)
      dp[R][C][k] += val[R][C];
    ans = max(ans, dp[R][C][k]);
  }
  printf("%lld\\n", ans);

  return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
by Benq;
*/


Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 3456;
int val[MAXN][MAXN];
LL dp[MAXN][MAXN][3];

int main() {
  int R, C, K;
  scanf("%d %d %d", &R, &C, &K);
  for (int i = 0; i < K; i++) {
    int r, c, v;
    scanf("%d %d %d", &r, &c, &v);
    val[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      for (int k = 0; k <= 3; k++) {
        if (k < 3)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + val[i][j]);
        // not take
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        // not take
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k) // take
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k - 1] + val[i][j]);
        // printf("i = %d j = %d k = %d dp = %lld\\n", i, j, k, dp[i][j][k]);
      }
    }
  }
  LL ans = 0;
  for (int k = 0; k <= 3; k++) {
    if (k != 3)
      dp[R][C][k] += val[R][C];
    ans = max(ans, dp[R][C][k]);
  }
  printf("%lld\\n", ans);

  return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
by Benq;
*/