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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
int field[3010][3010];
ll dp[4000][4000][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  for (int i = 0; i < R + 1; i++) {
    for (int j = 0; j < C + 1; j++) {
      field[i][j] = 0;
    }
  }

  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    field[r][c] = v;
  }
  for (int i = 0; i < R + 1; i++) {
    for (int j = 0; j < C + 1; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  ll ans = 0;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        if (k + 1 < 4) {
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + field[i][j]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + field[i][j]);
        }
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        // cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
        ans = max(ans, dp[i][j][k]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
int field[3000][3000];
ll dp[4000][4000][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  for (int i = 0; i < R + 1; i++) {
    for (int j = 0; j < C + 1; j++) {
      field[i][j] = 0;
    }
  }

  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    field[r][c] = v;
  }
  for (int i = 0; i < R + 1; i++) {
    for (int j = 0; j < C + 1; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  ll ans = 0;
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        if (k + 1 < 4) {
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + field[i][j]);
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k] + field[i][j]);
        }
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++) {
        // cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
        ans = max(ans, dp[i][j][k]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}