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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
long long h, w, k;
long long v[3000][3000] = {};
long long dp[3000][3000][4] = {};
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < k; i++) {
    long long r, c, V;
    cin >> r >> c >> V;
    r--;
    c--;
    v[r][c] = V;
  }
  if (v[0][0] > 0) {
    dp[0][0][1] = v[0][0];
  }
  for (int i = 1; i < h; i++) {
    dp[i][0][0] = max(dp[i - 1][0][0], dp[i - 1][0][1]);
    if (v[i][0] > 0) {
      dp[i][0][1] = max(dp[i - 1][0][0], dp[i - 1][0][1]) + v[i][0];
    }
  }
  for (int i = 1; i < w; i++) {
    dp[0][i][0] = dp[0][i - 1][0];
    dp[0][i][1] = dp[0][i - 1][1];
    dp[0][i][2] = dp[0][i - 1][2];
    dp[0][i][3] = dp[0][i - 1][3];
    if (v[0][i] > 0) {
      dp[0][i][1] = max(dp[0][i][1], dp[0][i - 1][0] + v[0][i]);
      dp[0][i][2] = max(dp[0][i][2], dp[0][i - 1][1] + v[0][i]);
      dp[0][i][3] = max(dp[0][i][3], dp[0][i - 1][2] + v[0][i]);
    }
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][1], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][2], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][3], dp[i][j][0]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
      dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
      if (v[i][j] > 0) {
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][2] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][3] + v[i][j]);
        //
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + v[i][j]);
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + v[i][j]);
      }
    }
  }
  long long ans = 0;
  ans = max(ans, dp[h - 1][w - 1][0]);
  ans = max(ans, dp[h - 1][w - 1][1]);
  ans = max(ans, dp[h - 1][w - 1][2]);
  ans = max(ans, dp[h - 1][w - 1][3]);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
int main() {
  int h, w, k;
  long long v[3000][3000] = {};
  cin >> h >> w >> k;
  for (int i = 0; i < k; i++) {
    int r, c, V;
    cin >> r >> c >> V;
    r--;
    c--;
    v[r][c] = V;
  }
  long long dp[3000][3000][4] = {};
  if (v[0][0] > 0) {
    dp[0][0][1] = v[0][0];
  }
  for (int i = 1; i < h; i++) {
    dp[i][0][0] = max(dp[i - 1][0][0], dp[i - 1][0][1]);
    if (v[i][0] > 0) {
      dp[i][0][1] = max(dp[i - 1][0][0], dp[i - 1][0][1]) + v[i][0];
    }
  }
  for (int i = 1; i < w; i++) {
    dp[0][i][0] = dp[0][i - 1][0];
    dp[0][i][1] = dp[0][i - 1][1];
    dp[0][i][2] = dp[0][i - 1][2];
    dp[0][i][3] = dp[0][i - 1][3];
    if (v[0][i] > 0) {
      dp[0][i][1] = max(dp[0][i][1], dp[0][i - 1][0] + v[0][i]);
      dp[0][i][2] = max(dp[0][i][2], dp[0][i - 1][1] + v[0][i]);
      dp[0][i][3] = max(dp[0][i][3], dp[0][i - 1][2] + v[0][i]);
    }
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][1], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][2], dp[i][j][0]);
      dp[i][j][0] = max(dp[i - 1][j][3], dp[i][j][0]);
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
      dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
      if (v[i][j] > 0) {
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][2] + v[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][3] + v[i][j]);
        //
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + v[i][j]);
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + v[i][j]);
      }
    }
  }
  long long ans = 0;
  ans = max(ans, dp[h - 1][w - 1][0]);
  ans = max(ans, dp[h - 1][w - 1][1]);
  ans = max(ans, dp[h - 1][w - 1][2]);
  ans = max(ans, dp[h - 1][w - 1][3]);
  cout << ans << endl;
  return 0;
}