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

#include <bits/stdc++.h>
using namespace std;
long long dp[3010][3010][4];
int R, C, K;
vector<vector<long long>> vec;
int main() {
  cin >> R >> C >> K;
  vec.resize(R, vector<long long>(C, 0));
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    vec.at(r - 1).at(c - 1) = v;
  }
  dp[0][0][1] = vec.at(0).at(0);
  for (int y = 0; y < R; y++) {
    for (int x = 0; x < C; x++) {
      if (x + 1 < C) {
        dp[y][x + 1][3] =
            max(dp[y][x][3],
                max(dp[y][x + 1][3], dp[y][x][2] + vec.at(y).at(x + 1)));
        dp[y][x + 1][2] =
            max(dp[y][x][2],
                max(dp[y][x + 1][2], dp[y][x][1] + vec.at(y).at(x + 1)));
        dp[y][x + 1][1] =
            max(dp[y][x][1],
                max(dp[y][x + 1][1], dp[y][x][0] + vec.at(y).at(x + 1)));
        dp[y][x + 1][0] = max(dp[y][x][0], max(dp[y][x + 1][0], dp[y][x][0]));
      }
      if (y + 1 < R) {
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][0]);
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][1]);
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][2]);
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][3]);
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][0] + vec.at(y + 1).at(x));
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][1] + vec.at(y + 1).at(x));
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][2] + vec.at(y + 1).at(x));
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][3] + vec.at(y + 1).at(x));
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[R - 1][C - 1][i]);
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  // #define int long long
  int R, C, K;
  cin >> R >> C >> K;
  vector<vector<long long>> vec(R, vector<long long>(C, 0));
  long long dp[3010][3010][4];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    vec.at(r - 1).at(c - 1) = v;
  }
  dp[0][0][1] = vec.at(0).at(0);
  for (int y = 0; y < R; y++) {
    for (int x = 0; x < C; x++) {
      if (x + 1 < C) {
        dp[y][x + 1][3] =
            max(dp[y][x][3],
                max(dp[y][x + 1][3], dp[y][x][2] + vec.at(y).at(x + 1)));
        dp[y][x + 1][2] =
            max(dp[y][x][2],
                max(dp[y][x + 1][2], dp[y][x][1] + vec.at(y).at(x + 1)));
        dp[y][x + 1][1] =
            max(dp[y][x][1],
                max(dp[y][x + 1][1], dp[y][x][0] + vec.at(y).at(x + 1)));
        dp[y][x + 1][0] = max(dp[y][x][0], max(dp[y][x + 1][0], dp[y][x][0]));
      }
      if (y + 1 < R) {
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][0]);
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][1]);
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][2]);
        dp[y + 1][x][0] = max(dp[y + 1][x][0], dp[y][x][3]);
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][0] + vec.at(y + 1).at(x));
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][1] + vec.at(y + 1).at(x));
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][2] + vec.at(y + 1).at(x));
        dp[y + 1][x][1] =
            max(dp[y + 1][x][1], dp[y][x][3] + vec.at(y + 1).at(x));
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[R - 1][C - 1][i]);
  }
  cout << ans << endl;
}